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 forms—perfect 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.
Figure 1. 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).