Coredump

Work, play, and everything in-between.

Archive for the ‘python’ tag

Downloading Flickr photos, part 2

without comments

I made several improvements in the previous script:

#!/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. ;)

Written by Ian Dexter

March 26th, 2007 at 3:54 pm

Posted in Play

Tagged with , , , , ,

Downloading Flickr photos

with 2 comments

Flickr leech

Flickr Leech is a cool web app. It displays all Flickr photos, including those already rendered invisible because of the 200-photo limit in free accounts. But I need more: I want to download these photos for backup.

After looking around, I found several tools that does the job. They didn’t quite work for me, so I decided to hack my own.

Using the Flickr API and a Python wrapper, I came up with the following:

  1. Get flickr.py and manually add it to the host Python library.
  2. Get an API key from Flickr.
  3. Write the script:
    #!/usr/bin/python
     
    import flickr
    import urllib
     
    flickr.API_KEY = 'API key goes here'
    user = flickr.people_findByUsername(u'username')
    photos = flickr.people_getPublicPhotos(user.id, 500)
    total = 0
    for photo in photos:
       photoURL = "http://static.flickr.com/%s/%s_%s_o.jpg" % (photo.server, photo.id, photo.secret)
       photoFile = "%s_%s.jpg" % (photo.title, photo.id)
       data = urllib.urlretrieve(photoURL, photoFile)
       total = total + 1
       print "Downloading %s" % photoFile
    print "Done with %s photos." % total
  4. Make the script executable, create a directory where to download files, and execute the script from there.
  5. Rinse, dry, press.

I know, the script is very rudimentary. It’s my first Python script, and it needs a lot of work: there’s no exception-handling for one. But it worked perfectly for me.

Written by Ian Dexter

March 21st, 2007 at 2:33 pm

Posted in Play

Tagged with , , , , ,