Monthly Archive for March, 2007

Downloading Flickr photos, part 2

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. ;)

Downloading Flickr photos

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.

Wii baby

Looking forward to hearing our new baby laugh like this (even without a Wii).

Typography stars in own film

Helvetica film

Love it or hate it, Helvetica looms large in visual culture. Helvetica is a feature-length film exploring the proliferation of one typeface as part of a larger conversation about the way type affects our lives.

Twitter updates, via CLI

TwitterI’ve just started using Twitter, an up-and-coming web service that posts user status on the web, in IM and in SMS. The service also exposes its API, so it’s possible to build apps using it.

I found a nifty way of updating my Twitter status through the command line:

curl -u username:password -d status='status_goes_here' -s http://twitter.com/statuses/update.xml > /dev/null 2>&1