Friday, 15 January 2010

flickrpy + 30 lines of code = poor man's Flickr Backup solution

It uses somewhat buggy but excellent flickrpy module by James Clarke — works well for me after some small adjustments:


def main():
flickr.API_KEY = constants.API_KEY
flickr.API_SECRET = constants.API_SECRET

u = flickr.User(id = '92002612@N00')
sets = u.getPhotosets()

for ps in sets:
name = ps.title
name = name.replace('"', "'").replace("/", "_")

folderName = "/Users/kirillov/Pictures/Flickr/%s" % name
if os.path.exists(folderName):
print("Path exists, skipping: %s" % folderName )
continue

photos = ps.getPhotos()

os.mkdir(folderName)
i = 0
for i,p in enumerate(photos):
src = p.getSizes()[-1]['source']
photoname = p.getTitle().replace("/", "_")

fname = '/Users/kirillov/Pictures/Flickr/%s/%s-%d.jpg' % (name, photoname, i)
data = u2.urlopen(src).read()
open(fname, 'w').write(data)

print('%s => "/Users/kirillov/Pictures/Flickr/%s/%s-%d.jpg"' % (src,name, photoname, i))

Upd: fixed and updated version posted.