Downloading Flickr photos, part 2
I made several improvements in the previous script:
Python is pretty simple, once you get the hang of it. It also helps that the source for flickr.py is open -- I got to fetch some more info regarding the photos I'm downloading, thus incorporating them in the code.
The code could use some more improvement, though: I should have opted for the Flickr error code (
This should be elementary to Python devs out there, but for me, it's just a hobby. ;)
#!/usr/bin/python
import flickr
import urllib
import time
import re
flickr.API_KEY = 'Flick API goes here'
print "Registered using API key"
user = flickr.people_findByUsername(u'username')
print "Found user %s - username" % user.id
try:
photos = flickr.people_getPublicPhotos(user.id, 500)
print "Found the photos"
total = 0
for photo in photos:
p = flickr.Photo(photo.id)
title = re.sub('\s+', '-', p.title)
title = re.sub('[^-\w]', '', title)
title = "%s_%s" % (title, p.datetaken.split()[0])
for s in p.getSizes():
url = s['source']
photoFile = "%s_%s" % (title, url.split("/")[4])
data = urllib.urlretrieve(url, photoFile)
total = total + 1
print "Retrieving %s ..." % photoFile
time.sleep(1)
except AttributeError:
exit
print "%s photos retrieved." % total
Python is pretty simple, once you get the hang of it. It also helps that the source for flickr.py is open -- I got to fetch some more info regarding the photos I'm downloading, thus incorporating them in the code.
The code could use some more improvement, though: I should have opted for the Flickr error code (
flickr.FlickrError) instead of AttributeError. The regex can certainly use more trimming. Perhaps I can save the photo information in a database instead of glomming them in the filename (so I can include tags, group and set memberships, etc.),This should be elementary to Python devs out there, but for me, it's just a hobby. ;)
Comments
Post a Comment