When you download photos from flickr, the photo ID is actually stored in the filename, and you can use this to get back to the original photo. I decided to make any photos that are uploaded with the flickr filename automatically link to the original via perl code that I first posted here.

However, I have since realized that some pictures I post are actually a combination of several original pictures. Thus, I am now updating my code to properly deal with giving the link to all original images it is comprised of. The hardest part was dealing with the english rules for commas and “and”. “1 and 2″ vs “1, 2, and 3″ vs “1, 2, 3, and 4″.

Anyway, here is the revised code for auto-flickr-linking-to-original-pic:

my $FLICKR_PHOTOID_URL = "http://flickr.com/photo.gne?id=";
my @matches = $file =~ /([0-9]{10})_[0-9a-f]{10}[_ ][a-z]/ig;
my $matchNum = 0;
foreach $tmpmatch (@matches) {
  $tmpid = $tmpmatch;
  $matchNum++;
  if ($matchNum == 1) {
    $caption .= "Originally posted at ";
    $caption .= "$FLICKR_PHOTOID_TO_URL_PREFIX$tmpid";
  } elsif ($matchNum == @matches) {
    if (@matches > 2) { $s .= "	, ";	}
    $caption .= " and $FLICKR_PHOTOID_TO_URL_PREFIX$tmpid";
  } else {
    $caption .= " , $FLICKR_PHOTOID_TO_URL_PREFIX$tmpid";
  }
}
if (@matches > 0) { $s .= "\n"; }

This is definitely a bit more complex than the original version:

my $FLICKR_PHOTOID_URL = "http://flickr.com/photo.gne?id=";
if ($file =~ /([0-9]{10})_[0-9a-f]{10}_[a-z]/i) {
    $caption .= "Originally posted at $FLICKR_PHOTOID_URL$1\n";
}


Mood: moodlessness
Music: KMFDM – Every Day’s A Good Day

Advertisement