Hack help: Random smug thumb/url on my webpages

tcurtintcurtin Registered Users Posts: 5 Big grins
I recently set up a a smugmug site for a non profit I help run. I'd like to write a script that uses the APIs to insert random thumb img src= urls and their corresponding smugmug gallery hrefs on any page. I can use server-side includes as the mechanism to insert them into the webpages, but the real trick is - how do I get a listing of all the images in all of my galleries?

I've seen something close in php here: http://www.dgrin.com/showthread.php?t=6206

Since I'm not familiar with php, I'd like to do something similar in perl, but if anyone knows of a pre-existing app that does this or something close, I'd rather not reinvent the wheel.

My web server is running linux, so anything linuxable is an option as long as it doesn't involve changing the server. (ie I have a cgi directory and can run crontabs, etc but I won't be upgrading apache.)

Help is much appreciated - this seems like something a lot of people could use!

-Tim

Comments

  • tcurtintcurtin Registered Users Posts: 5 Big grins
    edited March 14, 2006
    Ok, I've solved my own problem after some effort and learning about REST and a few XML tricks... Turns out you don't even need an API key. Here's a perl script that will download a list of all image numbers in public galleries for any nickname, and generates a .js javascript library. Include that library and the tag <script type="text/javascript">smugPhoto();</script> to any webpage, and voila!

    There is one hackish thing I do - when creating the links to open the image in its smugmug gallery, I use links of the format http://gmara.smugmug.com/gallery/999/999/58701059. The "999/999" is bogus, after I discovered that it didn't matter what was in those positions, you'd still get the image in its gallery home. I don't know if this is always going to be the case, so caveat emptor!


    #!/usr/local/bin/perl
    
    # Copyright Tim Curtin, GMARA, 2006  tim@gmara.org
    # GMARA.org: Bringing Adventure to the Great Outdoors!
    # Interfaces with REST APIs on smugmug.com to get list of all albums and images for a particular smugmug nickname
    # Once obtained, spit out a .js javascript library.  Sample use in html:
    #&lt;script language="JavaScript" src="smugmugimages.js" type="text/javascript">&lt;/script>
    #&lt;script type="text/javascript">smugPhoto();&lt;/script>&lt;noscript>&lt;img src="images/pics/pigtails_rope.jpg">&lt;/noscript>
    
    # This solution has been tested on IE 6.0.28, Firefox 1.0.7, Opera 8.02, and Safari 2.0.3
    
    # ToDo:
    # Better error handling
    # Logging?
    
    use XML::Simple;
    use Data::Dumper;
    use LWP::Simple;
    
    my $gProxy = "http://api.smugmug.com/hack/rest/";
    my $gSmugNick = "gmara"; # smugmug nickname to grab images from
    my $jsFile = "/home/greenmt/public_html/smugmugimages.js"; # full local path to output file
    my $gImgSize = "Th"; #Th=thumb, L=large, Ti=Tiny, S=Small - the image size to see on your pages
    
    my $debug = 0;
    
    
    my $now = scalar(localtime());
    
      $ret = getSmugXML("albums.get");
    
      print "ret for albums: ",Dumper($ret),"\n" if ($debug > 0);
    
    # print out header to OUTFILE.  Waited until here because a failed get will die first, preventing us from killing the currently existing .js file
    system("cp $jsFile $jsFile.bak");  # make a backup to be safe!
    open(OUTFILE,">$jsFile") or die "Couldn't open $jsfile for write: $!";
    print OUTFILE &lt;&lt;EOF;
    // Created at $now
    // For smugmug nickname $gSmugNick
    
    var B = new Array();
    
    EOF
    
    
      my $i;
      foreach $i (keys %{${$ret}{'Album'}}) {
        my $title = ${$ret}{'Album'}{$i}{'Title'};
        my $category = ${$ret}{'Album'}{$i}{'Category'};
        my $catid = ${$ret}{'Album'}{$i}{'CategoryID'};
    
        my $ret = getSmugXML("images.get","AlbumID=$i","Heavy=1");
    
        # galleries with more than one image return a list of keys in a hash key "Image" - each key is the image number, but the key values are empty.  Should be filled in by the Heavy=1 param to images.get?
        my @images = keys %{${$ret}{'Image'}};
    
        print "ret for $category album '$title' ($i): " , join(',',@images) , "\n" if ($debug > 0);
        print OUTFILE "//Album Title '$title', AlbumID '$i'\n";
        print OUTFILE "//Category '$category', CategoryID '$catid'\n";
        foreach $img (@images) {
          print OUTFILE "B.push('$img');\n";
        }
        print OUTFILE "\n";
    
      }
    
    
    # Now print out a footer to the .js file
    print OUTFILE &lt;&lt;EOF;
    function choosePhoto(parray)
    {
        inx = Math.floor(Math.random()*parray.length);
        img = "http://${gSmugNick}.smugmug.com/photos/" + parray[inx] + "-${gImgSize}.jpg";
        img2= "http://${gSmugNick}.smugmug.com/gallery/999/999/" + parray[inx];
        document.write('&lt;a href="' + img2 + '" target="_new">&lt;img border="0" src="' + img + '" />');
    }
    
    function smugPhoto()
    {
        choosePhoto(B);
    }
    
    EOF
    
    # Close the output properly
    close(OUTFILE);
    
    # And exit stage left
    exit(0);
    
    
    
    
    
    
    #### Subroutines ####
    
    ## getSmugXML(method[,args])
    # call with string containing method, then sets of "arg=val" for any other args to pass to query
    # example, getSmugXML("login.anonymously","Version=1.1.0");
    # should return hash with parsed xml data
    sub getSmugXML {
      my $meth = shift;
      my @args = @_;
    
      my $url = "${gProxy}?method=smugmug.${meth}&NickName=${gSmugNick}&" . join('&',@args);
    
      print "url:$url\n" if ($debug > 0);
    
      my $content = get($url);
      print "content: $content\n" if ($debug > 1);
    
    # These can happen if the smugmug servers are down...
      if ($content =~ /\&lt;html\>/i) {
        die "Doh - something's not right, got html instead of xml?  --- content:\n$content\n--- end of html content\nDying at $now...";
      }
      if (!$content =~ /xml/i) {
        die "Doh - something's not right, didn't get xml?  --- content:\n$content\n--- end of unknown content\nDying at $now...";
      }
    
    
      my $h = XMLin($content);
    
      my $stat = ${$h}{'stat'};
      if ($stat eq "fail") {
        warn "Error in getSmugXML: code " . ${$h}{'err'}{'code'} . ": " . ${$h}{'err'}{'msg'} . "!\n";
      }
    
      return $h;
    }
    
    
    
  • tcurtintcurtin Registered Users Posts: 5 Big grins
    edited March 14, 2006
    And here is what the resulting javascript .js file looks like:
    // Created at Tue Mar 14 12:09:46 2006
    // For smugmug nickname gmara
    
    var B = new Array();
    
    //Album Title 'EMS', AlbumID '1243440'
    //Category 'Other', CategoryID '0'
    B.push('58283660');
    
    //Album Title '2006-01-13 Petra and Dinner', AlbumID '1243434'
    //Category 'Events', CategoryID '8'
    B.push('58317760');
    B.push('58317736');
    B.push('58283433');
    B.push('58317907');
    B.push('58317843');
    B.push('58317835');
    
    function choosePhoto(parray)
    {
        inx = Math.floor(Math.random()*parray.length);
        img = "http://gmara.smugmug.com/photos/" + parray[inx] + "-Th.jpg";
        img2= "http://gmara.smugmug.com/gallery/999/999/" + parray[inx];
        document.write('&lt;a href="' + img2 + '" target="_new">&lt;img border="0" src="' + img + '" />');
    }
    
    function smugPhoto()
    {
        choosePhoto(B);
    }
    
    
    
Sign In or Register to comment.