A Flickr notifier app

Using Ruby and Growl
9 May 2010 · ruby · flickr

I am a daily user of Flickr, owing mainly to my participation in a Project 365 (post a picture for everyday of 2010). Not having attained enlightment, I check my photostream several times a day for comments or view stats etc. It’s obvious then that I set myself the task of using the Flickr API to make a nifty notifier of activity on a Flickr photostream. Here are some of the main things I learned from the experience.

The following were the requirements that I set myself for this project:

  • Should notify the user of new comments
  • Provide a summary of activity on their photostream
  • Use Growl as the notification mechanism
  • Use Ruby

Initially I thought I would need the Growl API in its full glory. I had recently discovered macruby and noticed a sample XCode project that showed macruby working with Growl. So, I started out aiming to build the app using macruby. Having seen macruby and Growl playing well together, I set about locating a ruby library that implemented Flickr API calls. The Flickr API documentation mentions flickr-ruby rflickr and flickr.rb and so I set about checking these out.

I spent most of my time with flickr.rb and got very stuck with getting the authentication workflow going. The Flickr API uses an OAuth styled approach that never requires a username and password but does require some clever shunting back and forth of API keys, shared secrets and frobs before a token is issued. Because my app needed to make authenticated API calls, I had to find a library whose authentication methods would be accessible to this newbie. And this is how I found flickraw.

Here are some of the main features of flickraw:

  • Small single file: flickraw.rb is less than 300 lines
  • Uses introspection so that changes to the API will not require an update of flickraw
  • Ruby syntax similar to the flickr api
  • Flickr authentication
  • Photo upload
  • Proxy support
  • Delayed library loading (for rails users)
  • Flickr URL helpers

Flickraw has excellent documentation and I found it to be simple and fun to use. It uses JSON internally and so allows for dot.lookup syntax to access attributes. I spent some time with flickraw in irb before understanding all this … but that was fun too! The following is a snippet from my code that illustrates the use of flickraw – keep in mind that the user has been authenticated at this point.

recent_activity = flickr.activity.userPhotos(:timeframe => timeframe)

 recent_activity.each do |x|
      message = ''
      x.activity.event.each do |z|
        case
        when z.type == "comment" then
        begin
            unless @displayed_comments.include? z.commentid
              @displayed_comments.push z.commentid
              message << "Comment: " << z._content << "\n"
            end
        end
        when z.type == "fave" then
          message << "Added as a favorite\n"
        end
        unless (message == '')
          message << "User: " << z.username << "\n"
          time_added = Time.at(z.dateadded.to_i)
          message << time_added.to_s << "\n"
        end
  end

Now unfortunately, macruby 0.5 did not play well with flickraw. Loading up the flickraw gem resulted in a message about not supporting native extensions yet. Not knowing how to fix this, I looked around some more and came across the far less complex, command line version of Growl. growlnotify ships with the Growl disk image (for mac os x) and allows for a very simple way of popping up Growl bubbles. More information on growlnotify can be found here. The following is a snippet from my code that demonstrates how to use it (string interpolation being used here of course).

 `growlnotify #{(sticky)? '-s':''} --image #{icon_filename} -m "#{message}" "#{title}"`

Thanks mainly to flickraw, I had a lot of fun with this, my first project with ruby. Screenshots available from the Flickr App Garden here and the app itself is on github here. I hope you check them out and give me your feedback. Time permitting I will try again with macruby 0.6 – I’d like to make the installation and setup a little more user-friendly.

In my next post I expect to write about the second project I set myself – constructing this blog using a Ruby static site generator.

blog comments powered by Disqus