Sunday, October 05, 2008

Picasa Web Album Command Line Upload

I wrote this code some time ago before Picasa Web Albums had a good uploading solution for Linux users. This code uses the the Google Data APIs. While there is a better upload solution for Linux now (Picasa for Linux) some of you, like me, might prefer a command line driven upload method. This allows you to upload a batch of photos by issuing a command like this:

picasa user password album delay photos*
view raw gistfile1.sh hosted with ❤ by GitHub


So, for example, you might issue a command like this:

picasa dburger password Honolulu 3000 *.jpg
view raw gistfile1.sh hosted with ❤ by GitHub


This would initiate an upload of all the jpg files in the current directory to my Picasa Web Albums under an album name of Honolulu with a throttling delay of 3 seconds. The picasa script, which calls my java code, is a simple script as follows:

#!/usr/bin/env bash
java -cp "/home/dburger/jars/*:/home/dburger/bin/" PicasaUploader "$@"
view raw gistfile1.sh hosted with ❤ by GitHub


And finally, the java code that does all the magic, note that you will need to compile / run against some Google Data API jar files as indicated in the comments:

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.appsforyourdomain.provisioning.UserFeed;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import java.io.File;
import java.io.IOException;
import java.net.URL;
// compile: javac -classpath "/home/dburger/jars/*" PicasaUploader.java
// execute: java -classpath "/home/dburger/jars/*:." PicasaUploader
public class PicasaUploader {
public static final String APP = "loosedog@gmail.com-picasa-uploader";
public static final String BASE_URL =
"http://picasaweb.google.com/data/feed/api/user/";
public static void main(String[] args) throws Exception {
if (args.length < 5) bail();
String user = args[0];
String pass = args[1];
String album = args[2];
int delay = 0;
try {
delay = Integer.parseInt(args[3]);
} catch (NumberFormatException exc) {
bail();
}
PicasawebService service = new PicasawebService(APP);
service.setUserCredentials(user, pass);
if (!albumExists(album, user, service)) createAlbum(album, user, service);
URL albumUrl = new URL(BASE_URL + user + "/album/" + album);
for (int i = 4; i < args.length; i++) {
String file = args[i];
MediaFileSource media = new MediaFileSource(new File(file), "image/jpeg");
System.out.print("file: " + file);
PhotoEntry retPhoto = service.insert(albumUrl, PhotoEntry.class, media);
System.out.println(" complete.");
Thread.sleep(delay);
}
}
private static boolean albumExists(String album,
String user,PicasawebService service) throws Exception {
URL albumsUrl = new URL(BASE_URL + user + "?kind=album");
UserFeed userFeed = service.getFeed(albumsUrl, UserFeed.class);
for (UserEntry ue : userFeed.getEntries()) {
if (ue.getTitle().getPlainText().equals(album)) return true;
}
return false;
}
private static AlbumEntry createAlbum(String album, String user,
PicasawebService service) throws Exception {
URL postUrl = new URL(BASE_URL + user);
AlbumEntry entry = new AlbumEntry();
entry.setTitle(new PlainTextConstruct(album));
return service.insert(postUrl, entry);
}
public static void bail() {
System.err.println(usage());
System.exit(1);
}
public static String usage() {
return "provide arguments: user password album delay photos*";
}
}
view raw gistfile1.java hosted with ❤ by GitHub

7 comments:

  1. Would you mind if I ported this to a different language and released that under an Open Source license?

    ReplyDelete
  2. This sounds like EXACTLY what I need, but I don't know where to begin to get this to work. Are there more detailed installation instructions somewhere?

    ReplyDelete
  3. Sure - email me direct if you don't know how to set this up with Java - I'll help you out.

    ReplyDelete
  4. inspired by your post I've tried to implement the same in Python (I feel it is more suitable for writing command line scripts than Java) and it works nice. There are also nice Getting Started guides for google-data bindings.

    It is such a joy compared to the last attempt to build command line uploader for facebook.

    ReplyDelete
  5. I want to do it under windows, but I don't know how to do it. Do you have this code compiled?
    it's possible to have all the necessary into one only .jar?

    Thanks!!!

    Juan

    ReplyDelete
  6. I've taken a broadly similar approach in Python which (should be) cross platform compatible. It certainly works on Linux

    https://github.com/leocrawford/picasawebsync

    The upload works fairly well, and the download and sync are well on their way

    ReplyDelete