-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (97 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2018 denis4net
// Copyright (C) 2019 Evgeny Kuznetsov ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//go:generate go run version_generate.go
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"os"
"time"
)
// keys from alpendmilch
var (
googleClientId string = "REDACTED.apps.googleusercontent.com"
googleClientSecret string = "REDACTED"
version string = "custom-build"
)
const defaultRedirectURL = "http://127.0.0.1:8088"
func main() {
localLibArg := flag.String("lib", "", "local library path")
dedup := flag.String("strategy", "unixhex", "filename deduplication strategy: [unixhex|id]")
flag.Parse()
lib := Library{
Path: *localLibArg,
Deduplicator: dedupUnixHex,
}
switch *dedup {
case "id":
lib.Deduplicator = dedupID
case "unixhex":
lib.Deduplicator = dedupUnixHex
}
fmt.Printf("gphotosync version %s\n", version)
// if .client_secret.json exists in local lib path, use those credentials
cred := credentials{
ID: googleClientId,
Secret: googleClientSecret,
RedirectURL: defaultRedirectURL,
}
if cred.ID == "" || cred.Secret == "" {
log.Fatal("no credentials available, can not continue")
}
// need to sleep for random number of milliseconds so as not to overload Google API
// but will not do it if no token exists
if _, err := os.Stat(lib.GetTokenPath()); !os.IsNotExist(err) {
// will sleep between 0 and 1 minute
rand.Seed(time.Now().UnixNano())
n := rand.Intn(1000)
fmt.Printf("Waiting for about %d seconds...\n", (n / 1000))
time.Sleep(time.Duration(n) * time.Millisecond)
// fmt.Println("Ready to start now")
}
if err := lib.Sync(cred); err != nil {
log.Fatal(err)
}
}
type credentials struct {
ID string
Secret string
RedirectURL string
}
// readSecretJSON reads the saved credentials from a JSON file
func readSecretJSON(file string, cr *credentials) error {
data, err := os.ReadFile(file)
if err != nil {
return err
}
var f map[string]interface{}
err = json.Unmarshal(data, &f)
if err != nil {
return err
}
cred := f["web"].(map[string]interface{})
cr.ID = cred["client_id"].(string)
cr.Secret = cred["client_secret"].(string)
uris := cred["redirect_uris"].([]interface{})
if len(uris) == 1 {
uri := uris[0].(string)
cr.RedirectURL = uri
}
return nil
}