Arkiv for september, 2009

Posting Flickr images to Web Boards

2009-09-28

I post images from Flickr on misc Web Boards regularly, and today I wrote a small Perl script to help me along:

The script is available here: kvisberg.net/scripts/flickr2bbc.pl

#!/usr/bin/perl -w
#
#  This script was developed by Ole Bendik Kvisberg (olekvi at kvisberg.net),
#
#  This script is free software; you can redistribute it and/or modify it
#  under the same terms as Perl itself.

use strict;

use Getopt::Long;
use File::Basename;

my $help        = 0;
my $flickrname  = 'olekvi';

# From Flickr.com:
# Flickr Community Guidelines specify that if you post a Flickr photo on
# an external website, the photo must link back to its photo page

my $nolink      = 0;

GetOptions ("flickrname=s"      => \$flickrname,
            "nolink"            => \$nolink,
            "help"              => \$help);

my $url = shift;

if ($help or !$url) {
        usage();
}

createbbc($url);

sub usage {
        printf ("Usage: %s [OPTION] [PHOTOURL]\n", basename($0));
        print "Creates BBCode to insert Flickr photo on web forums\n\n";
        print "  -f  --flickrname   your flickr username (i.e 'olekvi')\n";
        print "  -n, --nolink       skip link (Flickr says you shouldn't!)\n";
        print "  -h, --help         display this help and exit\n";
        exit;
}

sub createbbc {
        my $photourl = shift;
        unless ($photourl =~ m!^http://farm\d+\.static\.flickr\.com/\d+/(\d+)_\w+.jpg$!) {
                printf ("Invalid Flickr photo url:\n%s\n", $photourl);
                exit;
        }
        my $linkurl     = sprintf("http://www.flickr.com/photos/%s/%s/", $flickrname, $1);
        my $photobbc    = sprintf("[IMG]%s[/IMG]", $photourl);

        if ($nolink) {
                printf ("%s\n", $photobbc);
        }
        else {
                printf ("[URL=%s]%s[/URL]\n", $linkurl, $photobbc);
        }
}