Wednesday, June 27, 2007

Blogging delicious links

I've really been enjoying Simon Phipp's delicious bookmarks. Its actually one of my favorite feeds.

I like this much better than the "daily links" approach, where I have to open the blog and read them all together. I usually don't bother to open "daily links" blog entries, because the majority of the time there's nothing there of interest for me. With a blog per link, I can quickly scan and pick the ones I like.

That got me thinking, wouldn't it be nice if I could simply tag a link with a quick comment, and have it show up automatically in my Blogger/Blogspot blog? I'm not a big linker, I don't do tens a day, so it shouldn't overwhelm my readers...

Well, delicious doesn't support this capability. But with a little work, I was able to use their APIs and the Google Blogger/GData APIs to accomplish this. And I thought I'd share the code with you. You could probably pretty easily convert this to using other blog platform APIs.

With this in place, now when I want to do a quick blog of an interesting link, just tag it with "blogthis" and run this program. I can run it once for each tag, once a day, or even less frequently. Once all the posts have been blogged, the tag is renamed to "blogged" so that I don't keep blogging the same posts.

There is some preparatory work needed before you can compile and run the code I show below.

First, you need to download the Java client libraries for both the Delicious API and the Google Data APIs. The Delicious API was incredibly simple to navigate; the GData APIs not so. Somehow their documentation is just lightweight and obtuse enough to leave you scratching your head. Maybe they want to use it for interview tests :). Anyway, I finally made my way through and got something working.

Next, you need to get all of the dependent jar files. Ye gods, there are a lot of them. Here they are:

The delicious Java client requires
Then the Google Data client library requires
Finally, you need to get the id for your Blogger blog. To do this, simply go to your blog, view the HTML source, and look for this line:

<rel="service.post" href="http://www.blogger.com/feeds/NNNNNNN/posts/default">

The href attribute is what you use as the postURL for Google Data.

I am using the ClientLogin mode of authentication with Google Data. This mode says it may reject the authentication request and ask for a Captcha response. I didn't want to deal with this, and you can register a particular machine to no longer need Captcha. Since this is for your personal use, this seems a reasonable thing to do. For more information on ClientLogin authentication, see this page.

OK, whew, with that taken care of, here is the fairly simple code to suck down delicious posts tagged with "blogthis" and posting them to my blog.

Make sure
you replace the placeholders for the delicious username and password, Blogger username and password, and the postURL.

import com.google.gdata.client.GoogleService;
import com.google.gdata.data.Category;

import com.google.gdata.data.Entry;
import com.google.gdata.data.PlainTextConstruct;
import del.icio.us.Delicious;
import del.icio.us.beans.Post;
import java.net.URL;
import java.util.List;

public class Main {

public static void main(String[] args) {
try {
Delicious deli = new Delicious("your-delicious-userid", "your-delicious-password");


List<Post> posts = deli.getPostsForTag("blogthis");

for ( Post post : posts ) {
System.out.println("Blogging post: " + post.getDescription());
blogPost(post);
}

deli.renameTag("blogthis", "blogged");
System.out.println("All delicious posts blogged");
} catch ( Throwable e ) {
e.printStackTrace();
}
}

private static void blogPost(Post post) throws Exception {
// authenticate

GoogleService service = new GoogleService("blogger",
"david.vancouvering.delicious-blog");
service.setUserCredentials("your.google.user.id", "your-google-password");

// build entry
URL postURL =
new URL("http://www.blogger.com/feeds/your-numeric-id/posts/default");

Entry entry = new Entry();
entry.setTitle(new PlainTextConstruct("Quick Link: " + post.getDescription()));

StringBuffer buf = new StringBuffer();
if ( post.getExtended() != null ) {
buf.append(post.getExtended());
buf.append("<p>");
}

buf.append("<a href=\"" + post.getHref() + "\">" + post.getHref() + "</a>");

entry.setContent(new PlainTextConstruct(buf.toString()));

// Set the label for the entry

Category category = new Category();
category.setScheme("http://www.blogger.com/atom/ns#");
category.setTerm("quicklink");
entry.getCategories().add(category);

// post entry
service.insert(postURL, entry);
}

}

No comments: