Monday, January 18, 2010

Using Java to Download Links to Picasa Web Albums

I mentioned a couple of weeks ago that I moved all of my digital photos to Picasa Web Albums. It turns out that my wife would like access to these photos. I uploaded the albums as "Unlisted", so the web interface provides no way to get links for sharing in bulk. I have somewhere around 150 albums, so going into each one to grab a sharing URL would be a really tedious way to spend the afternoon on my vacation day. I would rather write a program. Picasa Web Albums Data API to the rescue.

Since this was my first program using Google Data APIs, it took me longer to set up a working environment than it did to actually write the Java code. The getting-started guide is here. For my case, I needed as external dependencies 1) JavaMail and 2) the JavaBeansActivation Framework, both of which most open-source-Java developers already have sitting around somewhere convenient. Then I grabbed the latest Google Data Java Client libraries, unzipped, and put all of the jars in gdata/java/lib and gdata/java/deps on the classpath of my project.

From there, the code was very straightforward. Note the placeholders for Google username and password.

import java.net.URL;
import java.util.List;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.UserFeed;

/**
 * Short script to download links to all of my Picasa
 * Web Albums.  This includes private/public/protected
 * access levels.
 * 
 * @author Scott McMaster (smcmaster@acm.org)
 *
 */
public class DownloadPicasaWebAlbumLinks {
 private static final String USERNAME = "NOTMYREALUSERNAME";
 private static final String PASSWORD = "NOTMYREALPASSWORD";

 @SuppressWarnings("unchecked")
 public static void main(String[] args) throws Exception {
  PicasawebService myService = new PicasawebService(
    "Download Album Links");
  myService.setUserCredentials(USERNAME, PASSWORD);

  URL albumFeedUrl = new URL(
    "http://picasaweb.google.com/data/feed/api/user/" + USERNAME
      + "?kind=album");
  UserFeed albumFeed = myService.getFeed(albumFeedUrl, UserFeed.class);
  List albums = albumFeed.getEntries();
  for (GphotoEntry album : albums) {
   System.out.println(album.getHtmlLink().getHref());
  }
 }
}
For "unlisted" albums, this will print out, for each album, the same link you get if you click "Link to this album" in the UI.

No comments: