Add an API to Your Yahoo! 360 Blog



Add an API to Your Yahoo! 360 Blog

Start using third-party services with your Yahoo! 360 blog by adding your own API.

Yahoo! 360 is a site designed to help you keep in touch with friends, family, and coworkers. In addition to sharing reviews, photos, and lists of your favorite music and movies, Yahoo! 360 lets you post messages in a blog. Of course, Yahoo! isn't the only place you can keep a blog. Services such as Blogger (http://www.blogger.com) and TypePad (http://www.typepad.com) will also host a blog for you. But if your friends are all chatting away on Yahoo! 360, you might want to keep a journal there, where you and your friends can all connect.

At the time of this writing, Yahoo! 360 is in beta testing, and its developers are working on the features they intend to offer for a wider release. Unfortunately, this means the blog portion of Yahoo! 360 isn't as robust as some of the other weblog services, and Yahoo! 360 doesn't yet offer a way to post to your weblog from third-party services. For example, if I keep a weblog on Blogger, I can post photos to that weblog from Flickr (http://www.flickr.com), a photo-sharing application recently acquired by Yahoo!. That's because Blogger offers an application programming interface (API) that lets other services access data programmatically.

Even though Yahoo! 360 doesn't offer an API, with a bit of scripting you can mimic a weblog API and start posting through third-party tools.

What You Need

To implement this hack, you'll need access to a publicly available web server that can run Perl scripts, and several external modules. Here's a look at the modules you need, what they provide, and where you can find them:


XMLRPC::Transport::HTTP

This module handles all of the formatting of XML-RPC requests and responses required for the metaWeblog API interface. The module is part of a larger package called SOAP::Lite. To read the documentation, go to http://search.cpan.org/~byrne/SOAP-Lite-0.60a/lib/XMLRPC/Transport/HTTP.pm.


LWP::Simple

This module allows you to download the contents of pages from the Web. It is available at http://search.cpan.org/~gaas/libwww-perl-5.803/lib/LWP/Simple.pm.


WWW::Mechanize

Also known as Mech, this module can automate interactions with a website. This hack uses Mech to log in to Yahoo! and post items to a Yahoo! 360 weblog. To download the module and read its documentation, go to http://search.cpan.org/~petdance/WWW-Mechanize-1.12/lib/WWW/Mechanize.pm.


WWW::Yahoo::Login

This module is an extension of Mech that handles logging in to Yahoo! sites. Find it ar http://search.cpan.org/~struan/WWW-Yahoo-Login-0.10/lib/WWW/Yahoo/Login.pm.


Image::Size

This module can find the dimensions in pixels of any image. It's used in this hack to find the dimension of images posted from Flickr. You can read more at http://search.cpan.org/~rjray/Image-Size-2.992/Size.pm.

If you're missing any of these modules, you can install each of them with CPAN, like this:

	perl MCPAN e shell 
	cpan> install  insert package name 

With the modules in place, you're ready to move on to the script.

The Code

This code doesn't fully implement all of the functions you'll find in the metaWeblog API. Instead, it implements two methods: getUsersBlogs and newPost. These two methods are the bare minimum needed to interface with other systems and add new weblog posts.

As you'd expect, getUsersBlogs returns a list of weblogs that a particular user can post to at a weblog service. Because Yahoo! 360 users are limited to one blog per Yahoo! ID, this function simply logs in to Yahoo! and fetches the name of the user's Yahoo! 360 blog. The newPost function also logs in to Yahoo!, changes the incoming text of a post if necessary, and adds the text as a new post to the Yahoo! 360 weblog.

Save the following code to a file called Y360_api.pl:

	#!/usr/bin/perl
	# Y360_api.pl
	# Implements a minimalist metaWeblog API for Yahoo! 360 blogs.
	# You can read more about the metaWeblog API here:
	# http://www.xmlrpc.com/metaWeblogApi
	#
	# Usage: send metaWeblog API requests for methods:
	#
	#		getUsersBlogs
	#			Returns the name and URL of a Yahoo! 360

	#			blog for the given user.
	#		newPost
	#			Adds a post with the incoming text to
	#		a Yahoo! 360 blog for the given user.

	use strict;
	use XMLRPC::Transport::HTTP;

	XMLRPC::Transport::HTTP::CGI
		-> dispatch_to('metaWeblog')
		-> handle
	;

	package metaWeblog;
	use WWW::Yahoo::Login qw( login logout );
	use WWW::Mechanize;
	use Image::Size 'html_imgsize';
	use LWP::Simple;

	my $mech = WWW::Mechanize->new(autocheck => 1);

	sub getUsersBlogs {
		my($app, $msg, $user, $pass) = @_;

		# Set some defaults
		my $blog_url = "http://blog.360.yahoo.com/blog/";
		my $blog_name = "My Yahoo! 360 Blog";

		# Log into Yahoo! 360
		my $mech = WWW::Mechanize->new();
		my $login = login(
			mech => $mech,
			uri  => 'http://blog.360.yahoo.com/blog/',
			user => $user,
			pass => $pass,
		);
	
		# Get weblog URL and title
		if ($login) {
			my $html = $mech->response()->content( );
			if ($html =~ m!<h2>(.*?)</h2>!mgis) {
				$blog_name = $1;
			}
			if ($html =~ m!<li><a href="(.*?)">View Blog</a></li>!mgis) {
				$blog_url = $1;
			}
		}
	
		# Send the response
		my @res;
		push @res, { url => SOAP::Data->type(string => $blog_url),
			blogid => SOAP::Data->type(string => "1"),
			blogName => SOAP::Data->type(string=> $blog_name) };
		\@res;
	}

	sub newPost {
		shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
		my($blog_id, $user, $pass, $item, $publish) = @_;

		# Log into Yahoo! 360
		my $mech = WWW::Mechanize->new();
		my $login = login(
			mech => $mech,
			uri  => 'http://blog.360.yahoo.com/blog/compose.html',
			user => $user,
			pass => $pass,
		);

		# Add width/height to image tags (for Flickr posts)
		if ($item->{description} =~ m!(.*?<img src="(.*?)" .*?)( )(/>.*)!mgis) {
			my $file = get($2);
			my $size = html_imgsize(\$file);
			$item->{description} = "$1 $size $4";
		}

		# Write post description to a file
		open(FILE, ">> post.txt") or die("Couldn't open post.txt\n");
		print FILE $item->{description} . "\n\n";

		# Remove initial div tag from del.icio.us posts
		$item->{description} =~ s!<div class="delicious-link">(.*?)</div>
!$1!gis; # Strip line breaks $item->{description} =~ s!\n!!gis; # If login succeeded, add post and send a successful # response with the generic 1000 as the post ID if ($login && $item->{title}) { my $form = $mech->form_name("blog_compose"); $mech->field("title", $item->{title}); $mech->field("contents", $item->{description}); $mech->click("post"); SOAP::Data->type(string => "1000"); } else { return error $WWW::Yahoo::Login::ERROR; } }

This script is built specifically to work with the API-related services at Flickr [Hack #67] and del.icio.usa bookmark manager available at http://del.icio.us. The newPost subroutine reformats incoming text based on the text that these services provide. For example, Flickr doesn't provide height and width attributes for image tags in the HTML it produces, and Yahoo! 360 requires those attributes in weblog posts. So this script uses the Image::Size module to add those attributes to the text.

Running the Hack

To run the hack, upload Y360_api.pl to a publicly available web server and note the URL. It should look something like this:

	http://example.com/Y360_api.pl

Be aware that this URL is sometimes referred to as an API Endpoint, so don't let the jargon throw you off. Whenever a service asks for a URL, this is the one you should use.

The final step is to test out your new API at a third-party service. Since the script was built specifically for Flickr, that's a good place to start. Follow the steps to set up a weblog at Flickr [Hack #98] and choose MetaWeblogAPI Enabled Blog as your weblog type, enter your script's URL as the Endpoint, and include your Yahoo! ID and password.

Flickr will contact your script behind the scenes with a getUsersBlogs request, and your script will send back the title and URL of your Yahoo! 360 weblog. Once your weblog is set up in Flickr, you can click the Blog This button above any photo and choose your Yahoo! 360 weblog (see Figure).

The Blog This button at Flickr


From there you can compose your post at Flickr and see the finished product as a post on your Yahoo! 360 blog like that shown in Figure.

A Flickr image posted to a Yahoo! 360 blog


The code takes a bit of time to set up, but once you wire this shortcut for yourself, you may find that you use both Yahoo! 360 and Flickr more often.