Import an Existing Blogroll to Yahoo! 360



Import an Existing Blogroll to Yahoo! 360

Moving your blog to Yahoo! 360 and already have a blogroll? Automate adding blogroll links with OPML and Perl.

A prominent feature of most weblogs is a list of links to other weblogs running down the side of the page. This list is called a blogroll, and they're so popular that a service called Blogrolling (http://www.blogrolling.com) is there to help people maintain large lists of links and easily include them on their own weblog.

Yahoo! 360 [Hack #61] also features a blogroll and a blogroll manager (http://blog.360.yahoo.com/blog/blogroll.html). To add a link to your blogroll, add the site name and URL into the form. If you'd like to add more links, click the Add Another button at the top of the page to reveal more fields. Adding a handful of links this way is fine, but if you already have a list of 10 or more sites you'd like to include, this can get tedious quickly. Luckily, there's a standard way of exchanging links that can make the job faster.

As more and more services offer the ability to create blogrolls or lists of links, the way to exchange these lists is an XML format called Outline Processor Markup Language (OPML). OPML can be used to syndicate lists of just about anything. Sites such as Blogrolling and the newsreader Bloglines (http://www.bloglines.com) use OPML to import and export long lists of web sites. For example, at Bloglines, you can subscribe to your favorite web sites to read their posts. Using Bloglines on a regular basis, you can accumulate a list of hundreds of sites. If you'd like to use this same list of accumulated sites with another service, you can use Bloglines's export function to get an OPML list of the site names and URLs.

Each individual entry in an OPML file exported from Bloglines looks like this:

	<outline title="O'Reilly Radar" htmlUrl="http://radar.oreilly.com/"
	type="rss" xmlUrl="http://radar.oreilly.com/index.rdf"/>

As you can see, the OPML file contains the title and URL of the site, and the location of the site's RSS feed. Having your list of favorite sites in this structured way can help you automate adding the sites to your Yahoo! 360 blogroll. Figure shows a Yahoo! 360 blogroll that's been imported with the code in this hack.

Instead of typing in each entry by hand into the Yahoo! 360 form, you can let Perl do the heavy lifting for you.

The Code

This code relies on a nonstandard component called WWW::Mechanize to handle the automation. Among other things, WWW::Mechanize can log in to web sites and fill out formsperfect for adding a batch of entries to the Yahoo! 360 blogroll editor. You'll also need to install the Yahoo!-specific component WWW::Yahoo::Login, which works with WWW::Mechanize to log in to your Yahoo! account.

Once the components are installed, save the following code to a file called import_blogroll_360.pl and be sure to add your Yahoo! ID and password to the script. Also, take a look at your current blogroll form (available at http://blog.360.yahoo.com/blog/blogroll.html) and count the number of rows you see. If you're starting with a blank blogroll, the number should be 3. Add this number to the code at the line my $i= n. This will tell the script where to begin adding links and will ensure that any current links you have at your Yahoo! 360 blogroll won't be overwritten.

A Yahoo! 360 imported blogroll


	
	#!/usr/bin/perl
	# import_blogroll_360.pl
	# Imports links from an OPML file into Yahoo! 360 Bookmarks
	# Usage: import_blogroll_360.pl <OPML FILE>

	use strict;
	use WWW::Yahoo::Login qw( login logout );
	use WWW::Mechanize;

	# Open the incoming file
	open(OPML, "@ARGV") || die "usage: import_blogroll_360.pl <OPML >";
	my @opml = reverse <OPML>;

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

	# Log into Yahoo! 360
	my $resp = login(
		mechq	=>$mech,
		uri		=>	'http://blog.360.yahoo.com/blog/blogroll.html',
		user	=>	'insert Yahoo! ID' ,
		pass	=>	'insert Yahoo! Password' ,
	);

	# Set the beginning field value
	my $i =  n; 
	my $form = $mech->current_form();

	# If login succeeded, loop through the OPML

	if ($resp) {
		# Parse the OPML
		foreach my $link (@opml) {
			# Depending on the flavor of OPML you're parsing
			# you may need to edit this regex
			  while ($link =~ /<outline title="(.*?)" htmlUrl="(.*?)"[^>]. */gi) {
 				$i++;
				my $title = $1;
				my $url = $2;
				# Set title field
				my (%titleattr);
				$titleattr{name} = "title_$i";
				$titleattr{value} = $title;
				$form->push_input("text", \%titleattr);
				$mech->field("title_$i", $title);
				# Set URL field
				my (%urlattr);
				$urlattr{name} = "url_$i";
				$urlattr{value} = $url;
				$form->push_input("text", \%urlattr);
				$mech->field("url_$i", $url);
				print "$title - $url\n";
			  }
		}
		$mech->field("amt",$i);

		# Submit the form
		$mech->click("save");
	} else {
		warn $WWW::Yahoo::Login::ERROR;
	}

This script is set to parse the output from Bloglines, and other services might have slightly different flavors of OPML that will require some changes. Even though OPML is emerging as a standard, the format is flexible and different services implement it in different ways. For example, Bloglines uses a title attribute to indicate the title of a site, while some others use a text attribute. If the flavor of OPML that you're working with is different, change the line that contains the format of the <outline> tag (bold in the code).

Running the Hack

To run the code, call it from the command line and pass in the name of your OPML file. So if your Bloglines export file is named bloglines_export.xml, you'd invoke the script like this:

	perl import_blogroll_360.pl bloglines_export.xml

Once executed, the script logs in to Yahoo! 360 with your Yahoo! ID and password and analyzes the blogroll form. From there, it loops through the OPML file, adding the proper fields and values to the Yahoo! 360 form. Once it's through the file, $mech->click("save") does the work of clicking the Save button, and you'll have your old blogroll in a new location!