Mon, 10 Aug 2009

Flickr Activity Posts

Facebook has been getting some bad press over the past few months over their rather onerous terms of service. I'm a proponent of open source, and open licenses, and have made my photographs available on Flickr with a specific-- and permissive-- licensing model.

Posting photos on Facebook, even if those photos were displayed via an automated feed from Flickr, would seem to give Facebook the ability to sub-license and transfer rights to those photos that might be more liberal than the ones I applied to them originally. (This is my interpretation anyway, and the language is vague enough to allow this to happen).

For that reason, I severed my Flickr feed with Facebook, and decided that I already have the power to publish my content to those who have any interest in following me (you're reading it!).

Which brings me to this post. :) I've just completed my latest programming project, a simple perl script that processes my Flickr feed and posts the results here to my blog.

It runs hourly and submits any Flickr uploads I've provided in that span of time to my blog. The entry right before this one used the same script (but expanded to pull from a greater amount of time). For those of you who run a blosxom blog (or one which accepts plain text posts) the script is available for you to use as well. It's in Perl and requires some module installations.
#!/usr/bin/perl # Author: Khan Klatt # Released under the GNU GPL. (http://www.gnu.org/copyleft/gpl.html) # http://www.khan.org/blog/cronflickr use strict; use HTTP::Request; use LWP::UserAgent; use PHP::Serialization; # Flickr Constants my $flickr_id = '43546914@N00'; # REPLACE ME WITH YOUR OWN FLICKR ID my $flickr_format = 'php_serial'; # Date Constants my $global_date = time() - 36000000; # (to kickstart first post when no photos recently posted) my $global_date = time() - 3600; # One Hour Ago # Post Constants my $path = '/PATH/TO/YOUR/BLOG/DIRECTORY'; my $post = qq[My Recent Flickr Activity\n<div class="flickrphotos">\n]; my $photosposted = 0; my $filename = $path . "/flickr" . $global_date . ".txt"; # Get the feed from Flickr my $uri = "http://api.flickr.com/services/feeds/photos_public.gne?format=$flickr_format&id=" . $flickr_id; my $request = HTTP::Request->new(GET => $uri); my $ua = LWP::UserAgent->new; my $response = $ua->request($request); my $data = $response->{_content}; # Process the feed my $feed = PHP::Serialization::unserialize($data); foreach my $entry ($feed->{items}) { foreach my $item (@$entry) { next if $$item{date} <= $global_date; # Don't post photos older than an hour ago $post .= <<EOF; <span class="flickrphoto"> <a href="$$item{l_url}" target="_new"><img src="$$item{thumb_url}" alt="$title" /></a> </span> EOF $photosposted++; } } $post .= "</div>\n"; if ($photosposted) { umask(002); open(outfile, ">$filename") or die "Couldn't open $filename for writing."; print outfile $post; close(outfile); }
The CSS classes of flickrphotos and flickrphoto are included to help with visual presentation.

Finally, you'll need to automate this script every hour (or on whatever interval you prefer). Here's my crontab:
5 * * * * /path/to/your/flickrcron.pl # Run 5 after the hour every hour of every day
There might be a bug lurking in the logic that compares the feed's photo timestamps with the local time of the server you're on. If these are in different timezones, then photos may be missed in the pathological case (where the timestamp on the photo is much older than the timestamp on the unix box, which would be true generally if you take pictures in Hawaii and have your hosting in Japan, for example). Adding timezone support is left as an exercise for the reader. ;)




Khan Klatt

Khan Klatt's photo