A Twitpic Script

I am impressed with the sheer volume of pictures from all over the world that get sent to twitpic.com. I use them and so do many Twitter applications.  My only complaint with twitpic is that they don’t let you grab random photo’s out of the API.  Not even your own; you have to specify which photo you want to get and it will return that.  I was interested in the idea of a screen saver of sorts that pull random photos straight from twitpic, so I sat down and whipped this together.  This little Perl script grabs the first twitpic from the Public Timeline page and returns the web address to that picture and then the thumbnail address and exits.  This could easily be altered to grab more than one, but this is all I wanted it to do.  Anyway here is the code, feel free it use it if you want:

#!/usr/bin/perl
#######################################################################
## Written by: Josh Reeley (5/8/2010) reeley AT reeleysoft DOT com
##
## This just scrapes the public timeline from twitpic.com and returns
## the address to the first (most recent) picture on the page. Since
## they don't provide API access to getting the public stream, this
## will do for now.
#######################################################################
# includes
use LWP::Simple;
# Variables that might change over time.
$websiteAddress = "http://twitpic.com";
$apiAddress = "http://twitpic.com/public_timeline";
$picturePattern = '(\s+)\<a href\=\"(\/.*)\" title.*';
$thumbPattern = '.*(http\:\/\/web\d+\.twitpic\.com\/img\/.*\.jpg).*';
# Get the Page
my $webpage = get($apiAddress);
# Split the source code into line
my @lines = split('\n', $webpage);
# Go line by line
foreach (@lines) {
# Look for the pattern above
if ($_ =~ m/$picturePattern/) {
# Print the picture address
print "$websiteAddress". $2 . "\n";
} elsif ($_ =~ m/$thumbPattern/) {
# Print the thumbnail address and exit
print $1 . "\n";
exit;
}
}

Hopefully twitpic will implement this sort of thing into their API and I can be done with it.

Be Sociable, Share!

Leave a Reply