yoink@tumblr ~ % date

05th May 2006

yoink@tumblr ~ % less 1146801600.txt

Flickr is SO easy in Ruby

Thanks to Flickr.rb. Wow, thank you http://scottraymond.net/.

My Flickr account is about to expire. A friend of mine, Dan Phiffer tapped me last year with a freebie pro upgrade. This was really useful for somethings like dumping my E3 photos and making more than one photoset. But I’m not sure I’d like to renew.

I tend to use Flickr in spurts and the only compelling reason for me to renew is to keep my photostream. If I regress to a free user once more, then about 560 photos will disappear from view, comments and all. That’s fine, the most recent 200 is cool, but what if I want to check on an old photo? I can’t, unless I have the original url. That’s always rubbed me the wrong way.

Back before the paid account, there was a trick to grab the old urls by setting your photos to private page at a time and snagging the urls from the public feed. But I’m lazy, I should let the code do the heavy lifting for me. So, in the dying days of my account I decided to archive those ever important links. Here’s the first half of that effort.

#!/usr/local/bin/ruby
require 'rubygems'
require 'flickr' # insanely easy flickr library
require 'sqlite3'
require 'cgi'

db = SQLite3::Database.new('flickr.db')
flickr = Flickr.new
dedi = flickr.users('dedi')

# i have 77 pages of photos at the moment this should really be variable somehow
total_page_num = 77

1.upto(total_page_num) do |page| 
    puts "Page #{total_page_num - page + 1}"

    criteria = {
      'user_id' => dedi.id, 'sort' => 'date-posted-asc',
      'per_page' => '10', 'page' => page.to_s }

    begin
        for photo in flickr.photos(criteria)
        title = CGI::unescapeHTML(photo.title)
        db.execute("insert into photos('photo_id', 'title','url','source')
            values (?,?,?,?)",
            photo.id, photo.title, photo.url, photo.source)
        puts "Added #{photo.id}: #{title}\n"
      end
    rescue RuntimeError => err
        # some photos (with privacy settings) raise this exception
        print "Skipping, error was: ", err, "\n"
    rescue NoMethodError => err
        # sometimes the xml parsing mucks up
        # TODO: sort this out, and possibly retry
        print "Skipping, error was: ", err, "\n"
    end
end

There’s a lot I could improve on, but it didn’t take long to write or run and only choke on a few items. I went back and added the friends only and friends and family only photos myself (I only had about 6 of those). Next I’d like to get the photoset information.

yoink@tumblr ~ % notes -v $post