-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.go
501 lines (434 loc) · 12 KB
/
backup.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/premshree/go-flickr"
"github.com/sethgrid/pester"
)
const (
API_KEY = "YOUR-API-KEY"
API_SECRET = "YOU-API-SECRET"
PHOTO_SIZE_ORIGINAL = "Original"
BACKUP_DIR = "/path/to/backup-dir"
CONFIG_PATH = "/path/to/config"
NOT_IN_SET = "not-in-set"
)
type PhotoSetsJson struct {
Photosets PhotoSets
}
type PhotoSets struct {
Photoset []PhotoSet
}
type PhotoSet struct {
Id string
Title Meta
Description Meta
}
type Meta map[string]string
type PhotosJson struct {
Photoset PhotosPhotoSet
}
type PhotosNotInSetJson struct {
Photos PhotosPhotoSet
}
type PhotosPhotoSet struct {
Id string
Photo []Photo
}
type Photo struct {
Id string
Title string
Description string
}
type PhotoSizesJson struct {
Sizes PhotoSizes
}
type PhotoSizes struct {
Size []PhotoSize
}
type PhotoSize struct {
Label string
Source string
}
type UserJson struct {
User User
}
type User struct {
Id string
Username map[string]string
}
type OAuthConfig struct {
OAuthToken string
OAuthTokenSecret string
}
type PhotosChannelMessage struct {
Photo Photo
Ok bool
PhotoSetId string
Counts map[string]int
}
var (
oauthConfig *OAuthConfig
pageNum *int
photoSetsCount *int
perPage *int
notInSet *bool
req *flickr.Request
wg sync.WaitGroup
)
func main() {
pageNum = flag.Int("page", 1, "Page number of photosets")
photoSetsCount = flag.Int("photosets", 10, "Number of photoset per run")
perPage = flag.Int("per-page", 100, "Number of results per page")
notInSet = flag.Bool("notinset", false, "Download photos not in sets?")
flag.Parse()
req = NewRequest()
oauthConfig = GetOAuthConfig()
if oauthConfig != nil {
req.OAuth.OAuthToken = oauthConfig.OAuthToken
req.OAuth.OAuthTokenSecret = oauthConfig.OAuthTokenSecret
} else {
token, _ := req.RequestToken()
// we'll use these later, after user authorization, to exchange for an access token
oauth_token := token["oauth_token"]
oauth_token_secret := token["oauth_token_secret"]
authorizeUrl := req.AuthorizeUrl(token, "read")
fmt.Println("** Authorize this application at Flickr using the following URL:")
fmt.Println(authorizeUrl)
fmt.Println("\n** Enter the oauth_verifier code from the callback URL:")
reader := bufio.NewReader(os.Stdin)
oauth_verifier, _ := reader.ReadString('\n')
oauth_verifier = strings.TrimSpace(oauth_verifier)
access_token, _ := req.AccessToken(oauth_token, oauth_verifier, oauth_token_secret)
req.OAuth.OAuthToken = access_token["oauth_token"]
req.OAuth.OAuthTokenSecret = access_token["oauth_token_secret"]
// save our token to config.json
oauthConfig = &OAuthConfig{
OAuthToken: req.OAuth.OAuthToken,
OAuthTokenSecret: req.OAuth.OAuthTokenSecret,
}
SaveOAuthConfig(oauthConfig)
}
// test login
req.Method = "flickr.test.login"
resp, err := req.ExecuteAuthenticated()
if err != nil {
fmt.Println("** Error executing method: ", err)
os.Exit(0)
}
var userJson UserJson
err = json.Unmarshal([]byte(resp), &userJson)
if err != nil {
fmt.Println("Error unmarshaling json: ", err)
}
msg := fmt.Sprintf("\n** Logged in as %s [%s]", userJson.User.Id, userJson.User.Username["_content"])
fmt.Println(msg)
// download all photo sets
wg.Add(1)
if *notInSet {
go DownloadNotInPhotoSets(userJson.User.Id, &wg)
} else {
go DownloadPhotoSets(userJson.User.Id, &wg)
}
wg.Wait()
}
func DownloadPhotoSets(userId string, wg *sync.WaitGroup) {
msg := fmt.Sprintf("** Backing up %d photosets, page %d\n", *photoSetsCount, *pageNum)
fmt.Println(msg)
start := time.Now()
req.Method = "flickr.photosets.getList"
req.Args["user_id"] = userId
req.Args["page"] = strconv.Itoa(*pageNum)
req.Args["per_page"] = strconv.Itoa(*photoSetsCount)
resp, err := req.ExecuteAuthenticated()
if err != nil {
fmt.Println("** Error executing method: ", err)
os.Exit(0)
}
var photoSetsJson PhotoSetsJson
err = json.Unmarshal([]byte(resp), &photoSetsJson)
if err != nil {
fmt.Println("Error unmarshaling json: ", err)
}
if reflect.DeepEqual(photoSetsJson.Photosets.Photoset, []PhotoSet{}) {
msg = fmt.Sprintf("No photoset found! Are you sure you have %d photosets?", *pageNum**photoSetsCount)
fmt.Println(msg)
os.Exit(0)
}
photoSetsChan, photosChan := processPhotoSets(photoSetsJson.Photosets.Photoset)
processedSetsCount := 0
photoSetsCount := len(photoSetsJson.Photosets.Photoset)
totalErrors := 0
for {
select {
case set := <-photoSetsChan:
msg := fmt.Sprintf("-> Processing photoset %s (count: %s)", set[0], set[1])
fmt.Println(msg)
case photoMsg := <-photosChan:
status := "OK"
if !photoMsg.Ok {
status = "FAIL"
}
msg = fmt.Sprintf("--> Processed photo %s (%d/%d) [%s] ... %s", photoMsg.Photo.Id, photoMsg.Counts["PhotoCount"], photoMsg.Counts["PhotoSetCount"], photoMsg.Photo.Title, status)
fmt.Println(msg)
// are all photos for a set processed?
if photoMsg.Counts["PhotoCount"] == photoMsg.Counts["PhotoSetCount"] {
msg = fmt.Sprintf("\n++ Finished processing all photos (%d) for set %s [error: %d]\n", photoMsg.Counts["PhotoCount"], photoMsg.PhotoSetId, photoMsg.Counts["Errors"])
fmt.Println(msg)
processedSetsCount++
totalErrors += photoMsg.Counts["Errors"]
}
// are all photosets processed?
if processedSetsCount == photoSetsCount {
elapsed := time.Since(start)
msg = fmt.Sprintf("ALL DONE (elaspsed time: %s; total errors: %d)", elapsed, totalErrors)
fmt.Println(msg)
wg.Done()
}
}
}
}
func DownloadNotInPhotoSets(userId string, wg *sync.WaitGroup) {
var msg string
start := time.Now()
req := NewRequest()
req.OAuth.OAuthToken = oauthConfig.OAuthToken
req.OAuth.OAuthTokenSecret = oauthConfig.OAuthTokenSecret
req.Method = "flickr.photos.getNotInSet"
req.Args["page"] = strconv.Itoa(*pageNum)
req.Args["per_page"] = strconv.Itoa(*perPage)
resp, err := req.ExecuteAuthenticated()
if err != nil {
fmt.Println("** Error executing method: ", err)
os.Exit(0)
}
var photosJson PhotosNotInSetJson
err = json.Unmarshal([]byte(resp), &photosJson)
if err != nil {
fmt.Println("Error unmarshaling json: ", err)
}
photosChan := make(chan *PhotosChannelMessage)
processPhotos(photosJson.Photos, photosChan)
for {
select {
case photoMsg := <-photosChan:
status := "OK"
if !photoMsg.Ok {
status = "FAIL"
}
msg = fmt.Sprintf("--> Processed photo %s (%d/%d) [%s] ... %s", photoMsg.Photo.Id, photoMsg.Counts["PhotoCount"], photoMsg.Counts["PhotoSetCount"], photoMsg.Photo.Title, status)
fmt.Println(msg)
// are all photos processed?
if photoMsg.Counts["PhotoCount"] == photoMsg.Counts["PhotoSetCount"] {
msg = fmt.Sprintf("\n++ Finished processing all photos (%d) in this run [error: %d]\n", photoMsg.Counts["PhotoCount"], photoMsg.Counts["Errors"])
fmt.Println(msg)
elapsed := time.Since(start)
msg = fmt.Sprintf("ALL DONE (elaspsed time: %s)", elapsed)
fmt.Println(msg)
wg.Done()
}
}
}
}
func processPhotoSets(photosets []PhotoSet) (<-chan []string, <-chan *PhotosChannelMessage) {
photoSetsChan := make(chan []string)
photosChan := make(chan *PhotosChannelMessage)
for _, el := range photosets {
go func(el PhotoSet) {
req := NewRequest()
req.OAuth.OAuthToken = oauthConfig.OAuthToken
req.OAuth.OAuthTokenSecret = oauthConfig.OAuthTokenSecret
req.Method = "flickr.photosets.getPhotos"
req.Args["photoset_id"] = el.Id
resp, err := req.ExecuteAuthenticated()
if err != nil {
fmt.Println("** Error executing method: ", err)
}
var photosJson PhotosJson
err = json.Unmarshal([]byte(resp), &photosJson)
if err != nil {
fmt.Println("Error unmarshaling json: ", err)
}
count := len(photosJson.Photoset.Photo)
photoSetsChan <- []string{el.Id, strconv.Itoa(count)}
processPhotos(photosJson.Photoset, photosChan)
}(el)
}
return photoSetsChan, photosChan
}
func processPhotos(photoSet PhotosPhotoSet, photosChan chan *PhotosChannelMessage) {
photoSetId := photoSet.Id
if photoSetId == "" {
photoSetId = NOT_IN_SET
}
photoCount := 0
errorCount := 0
photoSetCount := len(photoSet.Photo)
for _, photo := range photoSet.Photo {
go func(photo Photo) {
req := NewRequest()
req.OAuth.OAuthToken = oauthConfig.OAuthToken
req.OAuth.OAuthTokenSecret = oauthConfig.OAuthTokenSecret
req.Method = "flickr.photos.getSizes"
req.Args["photo_id"] = photo.Id
resp, err := req.ExecuteAuthenticated()
if err != nil {
fmt.Println("** Error executing method: ", err)
}
var photoSizesJson PhotoSizesJson
err = json.Unmarshal([]byte(resp), &photoSizesJson)
if err != nil {
fmt.Println("Error unmarshaling json: ", err)
}
photoSetPath := BACKUP_DIR + "/" + photoSetId
filepath := photoSetPath + "/" + photo.Id + ".jpg"
photoUrl := getOriginalSize(photoSizesJson)
err = os.Chdir(BACKUP_DIR)
if err != nil {
err = os.Mkdir(BACKUP_DIR, 0755)
if err != nil {
fmt.Println("Error creating backup dir ", err)
}
}
err = os.Chdir(photoSetPath)
if err != nil {
err = os.Mkdir(photoSetPath, 0755)
if err != nil {
fmt.Println("Error creating photoset dir ", err)
}
}
err = retry(5, func() error {
var err error
err = downloadFile(filepath, photoUrl)
return err
})
ok := true
if err != nil {
msg := fmt.Sprintf("*** Error downloading file id %s, %s: %s", photo.Id, photoUrl, err)
fmt.Println(msg)
errorCount++
ok = false
}
photoCount++
update := &PhotosChannelMessage{
Photo: photo,
Ok: ok,
PhotoSetId: photoSetId,
Counts: map[string]int{
"PhotoCount": photoCount,
"Errors": errorCount,
"PhotoSetCount": photoSetCount,
},
}
photosChan <- update
}(photo)
}
}
func getOriginalSize(photoSizesJson PhotoSizesJson) string {
sizes := photoSizesJson.Sizes
var source string
for _, v := range sizes.Size {
if v.Label == PHOTO_SIZE_ORIGINAL {
source = v.Source
break
}
}
return source
}
func downloadFile(filepath string, url string) error {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := getPester().Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func NewRequest() *flickr.Request {
req := &flickr.Request{
ApiKey: API_KEY,
Args: map[string]string{
"format": "json",
"nojsoncallback": "1",
},
}
req.OAuth = &flickr.OAuth{
ConsumerSecret: API_SECRET,
Callback: "https://go-flickr-backup.herokuapp.com/",
}
return req
}
func GetOAuthConfig() *OAuthConfig {
err := os.Chdir(BACKUP_DIR)
if err != nil {
err = os.Mkdir(BACKUP_DIR, 0755)
if err != nil {
fmt.Println("Error creating backup dir ", err)
return nil
}
}
file, err := os.Open(CONFIG_PATH)
if err != nil {
fmt.Println("error opening config file", err)
return nil
}
decoder := json.NewDecoder(file)
config := &OAuthConfig{}
err = decoder.Decode(&config)
if err != nil {
fmt.Println("error decoding config file", err)
return nil
}
return config
}
func SaveOAuthConfig(oauthConfig *OAuthConfig) {
json, err := json.Marshal(oauthConfig)
if err != nil {
fmt.Println(err)
return
}
out, err := os.Create(CONFIG_PATH)
_, err = io.Copy(out, bytes.NewBuffer(json))
if err != nil {
fmt.Println("Error saving config ", err)
}
}
func getPester() *pester.Client {
client := pester.New()
client.Concurrency = 1
client.Backoff = pester.ExponentialBackoff
client.MaxRetries = 5
client.Timeout = time.Duration(60 * 8 * time.Second)
return client
}
// thanks! https://blog.abourget.net/en/2016/01/04/my-favorite-golang-retry-function/
func retry(attempts int, callback func() error) (err error) {
for i := 0; ; i++ {
err = callback()
if err == nil {
return nil
}
if i >= (attempts - 1) {
break
}
}
return fmt.Errorf("*** after %d attempts, last error: %s", attempts, err)
}