O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Yahoo! Hacks
By Paul Bausch
October 2005
More Info

HACK
#31
Publish Your Yahoo! Bookmarks
Give the gift of your accumulated links to the world by publishing your Yahoo! Bookmarks to the Web
The Code
[Discuss (0) | Link to this hack]

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;
		}


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.