Thursday, December 29, 2011

Welcome Back Random Photo

Expert means that you know more than anyone you need to talk to about the subject. - Timothy Ferriss
In the before time, the long, long ago, my website was statically put together on my computer and then uploaded to Lycos for hosting. To make my life easier I moved it the main story handling to blogger and all the hosting over to GoDaddy. As part of the transition I knew there would be a loss of some functionality, but that generally everything would be easier, faster and all around better.
One of the losses in the transition was the "random photo" widget I had running in the header of my website. When I clicked "published" in my old system, it would run a perl file that would find all pictures and build a giant static javascript file that could randomly select and load one. With the most, all of my photos are now hosted in Picasa and I can't do that. So I just let it go. Or so I thought.
But one particular friend (hi Timmy!) told me that I was not allowed to do that! He asked (demanded?) that I fix the random photo display on my website. Since he might name his firstborn child after me, I felt like I needed to figure it out. I have! It works! Let me tell you how!
I am using JQuery and Picasa Web APIs to do it and it wasn't that hard! Wasn't that hard at all with a grand total of 30-lines of javascript that would be a lot more compact if I wanted it to be.
One call to get total number of photos in the album:
https://picasaweb.google.com/data/feed/api/user/username/albumid/albumId?alt=json&max-results=0&fields=openSearch:totalResults
Generate a random number between 1 and the result and then one more call to pull back the URLs for that image.
https://picasaweb.google.com/data/feed/api/user/username/albumid/albumId?alt=json&max-results=1&start-index="+showPhoto
Then you get a random photo!