Home - Download Source
    0 #!/opt/bin/perl
    1 
    2 # Picasa Badge
    3 #
    4 #	Author: Alessandro Arrichiello <alezzandro@gmail.com>
    5 #	version: 0.6
    6 #	date: 26/06/2008
    7 #
    8 #	Dependencies: LWP::Simple perl module (namely also: perl-libwww)
    9 #
   10 #	NOTE: Picasa is a registered trademark of Google Inc. Picasa Badge is not a partner, affiliate, or licensee of Google Inc., nor is it in any other way formally associated with Google Inc.
   11 #
   12 #	Use this software at your own RISK!
   13 #	The author declines every responsability about this software.
   14 #
   15 #	Picasa Badge is a perl script that lets you display on your website a badge containing picasa random photos or albums.
   16 #	You can edit many settings and the style of your badge (it will be displayed on a <div id="picasabadge"> </div> element that you can edit with you css style).
   17 #
   18 #	Usage:
   19 #		Example:
   20 #				Include in your webpage the script by this way:
   21 #					<script type="text/javascript" src="http://your.host.name/cgi-bin/picasabadge.pl?account=YOUR_ACCOUNT_NAME&kind=photo&thumbsize=72&max-results=4&total-items=275&vertical=1&display-logo=1"></script>
   22 #		As you can see, you can set various options,
   23 #		Allowed options are:
   24 #			* account = "Your Account Name"
   25 #			* kind = kind of display objects: "photo" or "album"
   26 #			* thumbsize = the site of thumbnails: 32, 48, 64, 72, 144, 160
   27 #			* max-results = how many photos picasa badge have to display.
   28 #			* total-items =  total photos stored on your album or total albums (You can obtain it on picasa account or reading the rss code of your account)
   29 #			* vertical or horizontal = set it to "1" to see picasa badge vertical or horizontal
   30 #			* display-logo = set it to "1" to see Picasa Badge logo on your badge
   31 #
   32 #
   33 #
   34 #	NOTE: I've added a little security fix to avoid overloading of my webserver:
   35 #	I've added my account name directly into source file to avoid that anyone could use it from my webserver,
   36 #	you can feel free to remove/edit it when you have downloaded the source file, you can find it some lines down.
   37 #
   38 #
   39 
   40 
   41 use LWP::Simple;
   42 use CGI;
   43 
   44 $q=new CGI;
   45 
   46 print "Content-Type: text/html\n\n";
   47 
   48 my $url = "http://picasaweb.google.com/data/feed/api/user/";
   49 if (defined($q->param('account'))){
   50 
   51 	# This is a protection to not overload my little webserver
   52 	# To make it work properly with your account name:
   53 	$url=$url . "alezzandro"; # delete this line or edit with your username
   54 	#$url=$url . $q->param('account'); #uncomment this line
   55 
   56 }else {print "document.write(\"Error: account not defined!\");"; die("Error: account not defined!\n");}
   57 $kind=$q->param('kind');
   58 if (defined($kind) and (grep {m|^$kind?$|} ('photo','album'))){
   59 	$url=$url . "?kind=" . $q->param('kind');
   60 }else {print "document.write(\"Error: kind not defined or not allowed!\");"; die("Error: kind not defined or not allowed!\n");}
   61 
   62 $url=$url . "&alt=rss";
   63 
   64 $url=$url . "&access=public";
   65 
   66 if (defined($q->param('vertical')) || defined($q->param('horizontal'))){
   67 	if (defined($q->param('vertical'))){
   68 		$separator="<br />";
   69 	}elsif (defined($q->param('horizontal'))){
   70 		$separator="";
   71 	}
   72 }else {print "document.write(\"Error: vertical or horizontal not defined!\");"; die("Error: vertical or horizontal not defined!\n");}
   73 
   74 
   75 $thumbsize=$q->param('thumbsize');
   76 if (defined($thumbsize) and (grep {m|^$thumbsize?$|} (32, 48, 64, 72, 144, 160))){
   77 	$url=$url . "&thumbsize=" . $q->param('thumbsize')."c";
   78 }else {print "document.write(\"Error: thumbsize not defined or not allowed!\");"; die("Error: thumbsize not defined or now allowed!\n");}
   79 if (defined($q->param('max-results'))){
   80 	$url=$url . "&max-results=" . $q->param('max-results');
   81 }else {print "document.write(\"Error: max-results not defined!\");"; die("Error: max-results not defined!\n");}
   82 if (defined($q->param('total-items'))){
   83 	if ($q->param('total-items') > $q->param('max-results')){
   84 		$random_number = int(rand($q->param('total-items') - $q->param('max-results'))) + 1;
   85 	}else {print "document.write(\"Error: Total photos are lower than max-results!\");"; die("Error: Total photos are lower than max-results!\n");}
   86 }else {print "document.write(\"Error: total-items not defined!\");"; die("Error: total-items not defined!\n");}
   87 $url=$url . "&start-index=" . $random_number;
   88 
   89 
   90 my $content = get($url);
   91 if ($kind eq "photo"){
   92 	@images = $content =~ /<title>.*?<\/title><description>.*?<\/description><enclosure type='.*?' url='.*?' length='0'\/><link>(.*?)<\/link>.*?<media:thumbnail url='(.*?)' height='.*?' width='.*?'\/>/g;
   93 }
   94 if ($kind eq "album"){
   95 	@images = $content =~ /album<\/category><title>.*?<\/title><description>.*?<\/description><link>(.*?)<\/link><author>.*?<\/author>.*?<media:thumbnail url='(.*?)' height='.*?' width='.*?'\/>/g;
   96 }
   97 print "document.write(\"<div id='picasa'>\");";
   98 $i=0;
   99 while ($i<($q->param('max-results')*2)){
  100 	print "document.write(\"<a href='". $images[$i] ."' target='_blank'><img src='". $images[$i+1] ."'></a>".$separator."\");";
  101 	$i+=2;
  102 }
  103 if (defined($q->param('display-logo'))){
  104 	print "document.write(\"<a href='http://blog.alexworld.it/post/39922197/picasa-badge'><img src='http://lh6.ggpht.com/Alezzandro/SGOIV2mnmoI/AAAAAAAAAso/WwCBjdK6UUc/s".$q->param('thumbsize')."-c/picasabadge.jpg'></a>".$separator."\");";
  105 }
  106 print "document.write(\"</div>\");";