-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofflickr.py
698 lines (584 loc) · 20.1 KB
/
offlickr.py
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
# Offlickr
# Hugo Haas -- mailto:[email protected] -- http://larve.net/people/hugo/
# Homepage: http://larve.net/people/hugo/2005/12/offlickr/
# License: GPLv2
#
# Daniel Drucker <[email protected]> contributed:
# * wget patch
# * backup of videos as well
# * updated to Beej's Flickr API version 1.2 (required)
# added by seth Vidal - [email protected] - 2013-05-22
# * optparse it
# * dateutil parsing for time
# * fetch flickrid by fapi - not ask for it
# * applied patches from upstream
import sys
import libxml2
import urllib
from optparse import OptionParser
import time
import dateutil
import dateutil.parser
import os
import threading
# Beej's Python Flickr API
# http://beej.us/flickr/flickrapi/
from flickrapi import FlickrAPI
import logging
__version__ = '0.22 - 2009-03-20'
maxTime = '9999999999'
# Gotten from Flickr
flickrAPIKey = '1391fcd0a9780b247cd6a101272acf71'
flickrSecret = 'fd221d0336de3b6d'
class Offlickr:
def __init__(
self,
key,
secret,
httplib=None,
dryrun=False,
verbose=False,
):
"""Instantiates an Offlickr object
An API key is needed, as well as an API secret"""
self.__flickrAPIKey = key
self.__flickrSecret = secret
self.__httplib = httplib
# Get authentication token
# note we must explicitly select the xmlnode parser to be compatible with FlickrAPI 1.2
self.fapi = FlickrAPI(self.__flickrAPIKey, self.__flickrSecret,
format='xmlnode')
(token, frob) = self.fapi.get_token_part_one()
if not token:
raw_input('Press ENTER after you authorized this program')
self.fapi.get_token_part_two((token, frob))
self.token = token
test_login = self.fapi.test_login()
uid = test_login.user[0]['id']
self.flickrUserId = uid
self.dryrun = dryrun
self.verbose = verbose
def __testFailure(self, rsp):
"""Returns whether the previous call was successful"""
if rsp['stat'] == 'fail':
print 'Error!'
return True
else:
return False
def getPhotoList(self, dateLo, dateHi):
"""Returns a list of photo given a time frame"""
n = 0
flickr_max = 500
photos = []
print 'Retrieving list of photos'
while True:
if self.verbose:
print 'Requesting a page...'
n = n + 1
rsp = self.fapi.photos_search(
api_key=self.__flickrAPIKey,
auth_token=self.token,
user_id=self.flickrUserId,
per_page=str(flickr_max),
page=str(n),
min_upload_date=dateLo,
max_upload_date=dateHi,
)
if self.__testFailure(rsp):
return None
if rsp.photos[0]['total'] == '0':
return None
photos += rsp.photos[0].photo
if self.verbose:
print ' %d photos so far' % len(photos)
if len(photos) >= int(rsp.photos[0]['total']):
break
return photos
def getGeotaggedPhotoList(self, dateLo, dateHi):
"""Returns a list of photo given a time frame"""
n = 0
flickr_max = 500
photos = []
print 'Retrieving list of photos'
while True:
if self.verbose:
print 'Requesting a page...'
n = n + 1
rsp = \
self.fapi.photos_getWithGeoData(api_key=self.__flickrAPIKey,
auth_token=self.token, user_id=self.flickrUserId,
per_page=str(flickr_max), page=str(n))
if self.__testFailure(rsp):
return None
if rsp.photos[0]['total'] == '0':
return None
photos += rsp.photos[0].photo
if self.verbose:
print ' %d photos so far' % len(photos)
if len(photos) >= int(rsp.photos[0]['total']):
break
return photos
def getPhotoLocation(self, pid):
"""Returns a string containing location of a photo (in XML)"""
rsp = \
self.fapi.photos_geo_getLocation(api_key=self.__flickrAPIKey,
auth_token=self.token, photo_id=pid)
if self.__testFailure(rsp):
return None
doc = libxml2.parseDoc(rsp.xml)
info = doc.xpathEval('/rsp/photo')[0].serialize()
doc.freeDoc()
return info
def getPhotoLocationPermission(self, pid):
"""Returns a string containing location permision for a photo (in XML)"""
rsp = \
self.fapi.photos_geo_getPerms(api_key=self.__flickrAPIKey,
auth_token=self.token, photo_id=pid)
if self.__testFailure(rsp):
return None
doc = libxml2.parseDoc(rsp.xml)
info = doc.xpathEval('/rsp/perms')[0].serialize()
doc.freeDoc()
return info
def getPhotosetList(self):
"""Returns a list of photosets for a user"""
rsp = self.fapi.photosets_getList(api_key=self.__flickrAPIKey,
auth_token=self.token, user_id=self.flickrUserId)
if self.__testFailure(rsp):
return None
return rsp.photosets[0].photoset
def getPhotosetInfo(self, pid, method):
"""Returns a string containing information about a photoset (in XML)"""
rsp = method(api_key=self.__flickrAPIKey,
auth_token=self.token, photoset_id=pid)
if self.__testFailure(rsp):
return None
doc = libxml2.parseDoc(rsp.xml)
info = doc.xpathEval('/rsp/photoset')[0].serialize()
doc.freeDoc()
return info
def getPhotoMetadata(self, pid):
"""Returns an array containing containing the photo metadata (as a string), and the format of the photo"""
if self.verbose:
print 'Requesting metadata for photo %s' % pid
rsp = self.fapi.photos_getInfo(api_key=self.__flickrAPIKey,
auth_token=self.token, photo_id=pid)
if self.__testFailure(rsp):
return None
doc = libxml2.parseDoc(rsp.xml)
metadata = doc.xpathEval('/rsp/photo')[0].serialize()
doc.freeDoc()
return [metadata, rsp.photo[0]['originalformat']]
def getPhotoComments(self, pid):
"""Returns an XML string containing the photo comments"""
if self.verbose:
print 'Requesting comments for photo %s' % pid
rsp = \
self.fapi.photos_comments_getList(api_key=self.__flickrAPIKey,
auth_token=self.token, photo_id=pid)
if self.__testFailure(rsp):
return None
doc = libxml2.parseDoc(rsp.xml)
comments = doc.xpathEval('/rsp/comments')[0].serialize()
doc.freeDoc()
return comments
def getPhotoSizes(self, pid):
"""Returns a string with is a list of available sizes for a photo"""
rsp = self.fapi.photos_getSizes(api_key=self.__flickrAPIKey,
auth_token=self.token, photo_id=pid)
if self.__testFailure(rsp):
return None
return rsp
def getOriginalPhoto(self, pid):
"""Returns a URL which is the original photo, if it exists"""
source = None
rsp = self.getPhotoSizes(pid)
if rsp == None:
return None
for s in rsp.sizes[0].size:
if s['label'] == 'Original':
source = s['source']
for s in rsp.sizes[0].size:
if s['label'] == 'Video Original':
source = s['source']
return [source, s['label'] == 'Video Original']
def __downloadReportHook(
self,
count,
blockSize,
totalSize,
):
if not self.__verbose:
return
p = ((100 * count) * blockSize) / totalSize
if p > 100:
p = 100
print '\r %3d %%' % p,
sys.stdout.flush()
def downloadURL(
self,
url,
target,
filename,
verbose=False,
):
"""Saves a photo in a file"""
if self.dryrun:
return
self.__verbose = verbose
tmpfile = '%s/%s.TMP' % (target, filename)
if self.__httplib == 'wget':
cmd = 'wget -q -t 0 -T 120 -w 10 -c -O %s %s' % (tmpfile,
url)
os.system(cmd)
else:
urllib.urlretrieve(url, tmpfile,
reporthook=self.__downloadReportHook)
os.rename(tmpfile, '%s/%s' % (target, filename))
def fileWrite(
dryrun,
directory,
filename,
string,
):
"""Write a string into a file"""
if dryrun:
return
if not os.access(directory, os.F_OK):
os.makedirs(directory)
f = open(directory + '/' + filename, 'w')
f.write(string)
f.close()
print 'Written as', filename
class photoBackupThread(threading.Thread):
def __init__(
self,
sem,
i,
total,
id,
title,
offlickr,
target,
hash_level,
getPhotos,
doNotRedownload,
overwritePhotos,
):
self.sem = sem
self.i = i
self.total = total
self.id = id
self.title = title
self.offlickr = offlickr
self.target = target
self.hash_level = hash_level
self.getPhotos = getPhotos
self.doNotRedownload = doNotRedownload
self.overwritePhotos = overwritePhotos
threading.Thread.__init__(self)
def run(self):
backupPhoto(
self.i,
self.total,
self.id,
self.title,
self.target,
self.hash_level,
self.offlickr,
self.doNotRedownload,
self.getPhotos,
self.overwritePhotos,
)
self.sem.release()
def backupPhoto(
i,
total,
id,
title,
target,
hash_level,
offlickr,
doNotRedownload,
getPhotos,
overwritePhotos,
):
print str(i) + '/' + str(total) + ': ' + id + ': '\
+ title.encode('utf-8')
td = target_dir(target, hash_level, id)
if doNotRedownload and os.path.isfile(td + '/' + id + '.xml')\
and os.path.isfile(td + '/' + id + '-comments.xml')\
and (not getPhotos or getPhotos and os.path.isfile(td + '/'
+ id + '.jpg')):
print 'Photo %s already downloaded; continuing' % id
return
# Get Metadata
metadataResults = offlickr.getPhotoMetadata(id)
if metadataResults == None:
print 'Failed!'
sys.exit(2)
metadata = metadataResults[0]
format = metadataResults[1]
t_dir = target_dir(target, hash_level, id)
# Write metadata
fileWrite(offlickr.dryrun, t_dir, id + '.xml', metadata)
# Get comments
photoComments = offlickr.getPhotoComments(id)
fileWrite(offlickr.dryrun, t_dir, id + '-comments.xml',
photoComments)
# Do we want the picture too?
if not getPhotos:
return
[source, isVideo] = offlickr.getOriginalPhoto(id)
if source == None:
print 'Oopsie, no photo found'
return
# if it's a Video, we cannot trust the format that getInfo told us.
# we have to make an extra round trip to grab the Content-Disposition
isPrivateFailure = False
if isVideo:
sourceconnection = urllib.urlopen(source)
try:
format = sourceconnection.headers['Content-Disposition'].split('.')[-1].rstrip('"')
except:
print 'warning: private videos cannot be backed up due to a Flickr bug'
format = 'privateVideofailure'
isPrivateFailure = True
filename = id + '.' + format
if os.path.isfile('%s/%s' % (t_dir, filename))\
and not overwritePhotos:
print '%s already downloaded... continuing' % filename
return
if not isPrivateFailure:
print 'Retrieving ' + source + ' as ' + filename
offlickr.downloadURL(source, t_dir, filename, verbose=True)
print 'Done downloading %s' % filename
def backupPhotos(
threads,
offlickr,
target,
hash_level,
dateLo,
dateHi,
getPhotos,
doNotRedownload,
overwritePhotos,
):
"""Back photos up for a particular time range"""
if dateHi == maxTime:
t = time.time()
print 'For incremental backups, the current time is %.0f' % t
print "You can rerun the program with '-f %.0f'" % t
photos = offlickr.getPhotoList(dateLo, dateHi)
if photos == None:
print 'No photos found'
sys.exit(1)
total = len(photos)
print 'Backing up', total, 'photos'
if threads > 1:
concurrentThreads = threading.Semaphore(threads)
i = 0
for p in photos:
i = i + 1
pid = str(int(p['id'])) # Making sure we don't have weird things here
if threads > 1:
concurrentThreads.acquire()
downloader = photoBackupThread(
concurrentThreads,
i,
total,
pid,
p['title'],
offlickr,
target,
hash_level,
getPhotos,
doNotRedownload,
overwritePhotos,
)
downloader.start()
else:
backupPhoto(
i,
total,
pid,
p['title'],
target,
hash_level,
offlickr,
doNotRedownload,
getPhotos,
overwritePhotos,
)
def backupLocation(
threads,
offlickr,
target,
hash_level,
dateLo,
dateHi,
doNotRedownload,
):
"""Back photo locations up for a particular time range"""
if dateHi == maxTime:
t = time.time()
print 'For incremental backups, the current time is %.0f' % t
print "You can rerun the program with '-f %.0f'" % t
photos = offlickr.getGeotaggedPhotoList(dateLo, dateHi)
if photos == None:
print 'No photos found'
sys.exit(1)
total = len(photos)
print 'Backing up', total, 'photo locations'
i = 0
for p in photos:
i = i + 1
pid = str(int(p['id'])) # Making sure we don't have weird things here
td = target_dir(target, hash_level, pid) + '/'
if doNotRedownload and os.path.isfile(td + pid + '-location.xml'
) and os.path.isfile(td + pid
+ '-location-permissions.xml'):
print pid + ': Already there'
continue
location = offlickr.getPhotoLocation(pid)
if location == None:
print 'Failed!'
else:
fileWrite(offlickr.dryrun, target_dir(target, hash_level,
pid), pid + '-location.xml', location)
locationPermission = offlickr.getPhotoLocationPermission(pid)
if locationPermission == None:
print 'Failed!'
else:
fileWrite(offlickr.dryrun, target_dir(target, hash_level,
pid), pid + '-location-permissions.xml',
locationPermission)
def backupPhotosets(offlickr, target, hash_level):
"""Back photosets up"""
photosets = offlickr.getPhotosetList()
if photosets == None:
print 'No photosets found'
sys.exit(0)
total = len(photosets)
print 'Backing up', total, 'photosets'
i = 0
for p in photosets:
i = i + 1
pid = str(int(p['id'])) # Making sure we don't have weird things here
print str(i) + '/' + str(total) + ': ' + pid + ': '\
+ p.title[0].text.encode('utf-8')
# Get Metadata
info = offlickr.getPhotosetInfo(pid,
offlickr.fapi.photosets_getInfo)
if info == None:
print 'Failed!'
else:
fileWrite(offlickr.dryrun, target_dir(target, hash_level,
pid), 'set_' + pid + '_info.xml', info)
photos = offlickr.getPhotosetInfo(pid,
offlickr.fapi.photosets_getPhotos)
if photos == None:
print 'Failed!'
else:
fileWrite(offlickr.dryrun, target_dir(target, hash_level,
pid), 'set_' + pid + '_photos.xml', photos)
# Do we want the picture too?
def target_dir(target, hash_level, id):
dir = target
i = 1
while i <= hash_level:
dir = dir + '/' + id[len(id) - i]
i = i + 1
return dir
def main(args):
"""Command-line interface"""
# Parse command line
parser = OptionParser(version = "1.0")
parser.add_option('-c', dest='threads', default=1,
help="number of threads to run to backup photos")
parser.add_option('-f', dest='start', default='0',
help="start of date to range, most date strings accepted or seconds from the epoch")
parser.add_option('-t', dest='end', default=maxTime,
help="end of date to range, most date strings accepted or seconds from the epoch")
parser.add_option('-d', dest='dest', default='dst',
help="directory for saving files")
parser.add_option('-l', dest='hash_level', default=0,
help="levels of directory hashes")
parser.add_option('-p', dest='get_photos', default=False, action='store_true',
help="back up photos in addition to photo metadata")
parser.add_option('-n', dest='do_not_redownload', default=False, action='store_true',
help="do not redownload anything which has already been downloaded")
parser.add_option('-o', dest='overwrite_photos', default=False, action='store_true',
help="overwrite photo, even if it already exists")
parser.add_option('-L', dest='photo_locations', default=False, action='store_true',
help="back up human-readable photo locations and permissions to separate files")
parser.add_option('-s', dest='photosets', default=False, action='store_true',
help="back up all photosets (time range is ignored")
parser.add_option('-w', dest='use_wget', default=False, action='store_true',
help="use wget instead of internal Python HTTP library - preserves picture timestamps")
parser.add_option('-v', dest='verbose', default=False, action='store_true',
help="verbose output")
parser.add_option('-N', dest='dry_run', default=False, action='store_true',
help='dry run')
opts,args = parser.parse_args(args)
if not os.path.isdir(opts.dest):
print opts.dest + ' is not a directory; please fix that.'
sys.exit(1)
# make hash_level an int
opts.hash_level = int(opts.hash_level)
# thread count is an int
opts.threads = int(opts.threads)
if opts.use_wget:
httplib = 'wget'
else:
httplib = None
# parse out the time strings to seconds from the epoch
try:
start = time.mktime(dateutil.parser.parse(opts.start).timetuple())
except (ValueError, OverflowError), e:
start = float(opts.start)
except:
print 'Could not parse time string of %s - try something simpler' % opts.start
sys.exit(1)
try:
end = time.mktime(dateutil.parser.parse(opts.end).timetuple())
except (ValueError, OverflowError), e:
end = float(opts.end)
except:
print 'Could not parse time string of %s - try something simpler' % opts.end
sys.exit(1)
offlickr = Offlickr(
flickrAPIKey,
flickrSecret,
httplib,
opts.dry_run,
opts.verbose,
)
if opts.photosets:
backupPhotosets(offlickr, opts.dest, opts.hash_level)
elif opts.photo_locations:
backupLocation(
opts.threads,
offlickr,
opts.dest,
opts.hash_level,
start,
end,
opts.do_not_redownload,
)
else:
backupPhotos(
opts.threads,
offlickr,
opts.dest,
opts.hash_level,
start,
end,
opts.get_photos,
opts.do_not_redownload,
opts.overwrite_photos,
)
if __name__ == '__main__':
main(sys.argv[1:])