Dec. 5, 2008, 9:48 p.m.
posted by neverloop
Publish Your Yahoo! Bookmarks
![]()
Give the gift of your accumulated links to the world by publishing your Yahoo! Bookmarks to the Web.
Part of the fun of using the Web is being able to share where you've been with others. The fact that any web site can be connected to any other web site via a link is one of its defining features. On weblogs, a list of links to other weblogs is called a blogroll, and there's even a web service called Blogrolling.com that helps people organize and publish these lists of links. Since Yahoo! Bookmarks is basically one big link repository, you're already using it to store and organize your favorite links. The only piece missing from rolling your own blogroll or link directory is publishing your Yahoo! Bookmarks for the world to see.
If you log in and view your Yahoo! Bookmarks at http://bookmarks.yahoo.com, you'll notice an Export Bookmarks link toward the top of the page. This feature is primarily used for transferring bookmarks between Yahoo! and your browser. But because the Netscape bookmark format is an extension of standard HTML, you can use the export file to publish your links. To get the file, choose Export Bookmarks
Netscape (for Windows)
"Click here" to export it. Once you have the bookmarks.html file on your desktop, you can upload it to a web server, where anyone can view it.
This process takes only a minute or two, but you wouldn't want to do this each time you added a bookmark, changed a bookmark, or reorganized your links. With a bit of Perl, you can automate this whole process and write some cleaner HTML in the process.
To run this hack, you'll need a couple of nonstandard Perl modules installed: WWW::Yahoo::Login and WWW::Mechanize for logging into Yahoo! and fetching the bookmarks.html export, respectively.
The Code
This script logs into Yahoo! and fetches your exported bookmarks. From there, it goes through the bookmarks line by line, adding folder names and links to a filename you specify in $file. Be sure to replace the $user and $pass values with your Yahoo! username and password so that the script can log in on your behalf. Create a file called getBookmarks.pl and add the following code:
#!/usr/bin/perl # getBookmarks.pl # A script to download Yahoo! Bookmarks and format them as HTML # Usage: getBookmarks.pl use WWW::Yahoo::Login qw( login logout ); use WWW::Mechanize; use strict; # Set some user variables my $user = "insert Yahoo! ID"; my $pass = "insert Yahoo! Password"; my $file = "links.html"; my $bookmarkurl = "http://bookmarks.yahoo.com/config/"; $bookmarkurl .= "export_bookmark?.commit=1"; my $mech = WWW::Mechanize->new( ); # Log into Yahoo! my $resp = login( mech => $mech, uri => $bookmarkurl, user => $user, pass => $pass, ); # If login succeeded, loop through the HTML if ($resp) { print "Login ok!\n"; my $bookmarks = $mech->content; my @bookmarks = split(/\n/, $bookmarks); # Open the output file open FILE, "> $file" or die "Can't open $file : $!"; # Loop through the bookmarks, printing to file foreach my $line (@bookmarks) { # If the line is folder, print it if ($line =~ m!<H3[^>].*>(.*?) </H3>!gi) { print FILE "<div class=\"folder\">"; print FILE $1; print FILE "</div>\n"; } # If the line is a bookmark, print it if ($line =~ m!<A HREF="([^"]+)"[^>]+>(.*?)</A>!gi) { print FILE "<div class=\"link\">"; print FILE "<a href=\"$1\">$2</a>"; print FILE "</div>\n"; } } # Close the output file close FILE; } else { warn $WWW::Yahoo::Login::ERROR; }
Running the Hack
To run the code, simply call the script from a command prompt:
perl getBookmarks.pl
After the script has run, you should find a new file called links.html, full of your Yahoo! Bookmarks. Each link should be on its own line, surrounded by HTML <div> tags. This lets you create your own look and feel for the links with an external stylesheet. The folders have a CSS class of folder, and links have a CSS class of link. The file is all set for you to drop it into an existing web site as a server-side include, or with a bit more formatting it could be its own page.
Now that this process is automated through a script, you can run it on a regular schedule to reap its benefits. Any changes you make at Yahoo! Bookmarks will be reflected on your remote site the next time this script runs; once every 24 hours should do it. In Windows, you can set it to run as a scheduled task from the Control Panel, calling Perl from the Run line, like this:
C:\Perl\bin\perl.exe "C:\insert your location \getBookmarks.pl "
And from Linux-based servers, you can set it as a cron job.
Now that you're able to use Yahoo! as a public links manager, the process of sharing resources should be much simpler.
- Comment