#!/usr/bin/perl ################################################## # Takes a list of firefox bookmarks (HTML format) # and adds them to my del.icio.us account. # Usage: # perl deladd.pl # It will prompt you for del.icio.us username # and password. # # Programmer: # J. van Donsel # 12/29/2004 # ################################################## use strict; use warnings; ######################################### # sub convert_to_html_string # Replaces non-ascii characters with %xx ######################################### sub convert_to_html_string($) { my @sdesc = split "", $_[0]; map { $_=sprintf("%%%02X", ord($_)) if ($_!~/[\w.\/:]/)} @sdesc; my $desc = join "", @sdesc; return $desc; } # end convert_to_html_string ######################################### # main # ######################################### if (scalar(@ARGV) == 0) { print "Usage:\n\tperl deladd.pl \n"; die; } # Get username/password print "Enter del.icio.us username:"; my $USERNAME=; chomp $USERNAME; print "Enter del.icio.us password:"; system("stty -echo"); my $PASSWORD=; system("stty echo"); chomp $PASSWORD; # Create a user agent object use LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->agent("Delicious Bookmark Adder"); # Get my entire list of bookmarks my $req = HTTP::Request->new(GET => "http://del.icio.us/api/posts/all"); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist'); $req->authorization_basic($USERNAME, $PASSWORD); my $res = $ua->request($req); my $all_posts; # Check the outcome of the response if ($res->is_success) { # Save our current post list as one long block $all_posts = $res->content; } else { print $res->status_line, "\n"; die; } # Go through each line of the bookmark file while(<>) { # Look for the URL (begins with HTTP) and the description tags if (/A HREF="(http:\/\/[^"]+)"[^>]*>([^<]+)new(GET => "http://del.icio.us/api/posts/add?&url=$url&description=$desc"); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist'); $req->authorization_basic($USERNAME, $PASSWORD); # Pass request to the user agent and get a response back my $res = $ua->request($req); # Check the outcome of the response if ($res->is_success) { print "ADDED\n"; } else { # Error print $res->status_line, "\n"; } # API for del.icio.us asks that queries be kept slow. sleep 3; } } #end while