#!/usr/bin/perl

use Digest::MD5 qw(md5 md5_hex md5_base64);
use WWW::Mechanize;
use URI::Escape;

$| = 1;
my $mech = new WWW::Mechanize();

# get results from google
print "Searching $ARGV[0] on google... ";
$mech->get('http://www.google.com');
$res = $mech->submit_form(fields => {q => $ARGV[0]});
die "Probs contacting google\n" unless $res->is_success();
print "done.\n";

# parse results from google;
my $content = $mech->content();
while ($content =~ /<a class=l href="(.*?)">/gsi){
	print "Checking matches of $1 on del.icio.us...\n";
	# get scraped url from delicious
	my $url = md5_hex($1);
	print "***$url***\n";
	my $res = $mech->get("http://del.icio.us/rss/url/$url");
	die "Could not find url" unless $res->is_success();
	
	my $del_cont = $mech->content();
	
	my %tags;
	my %vals;

	while ($del_cont =~ /<taxo:topics>(.*?)<\/taxo:topics>/gsi){
		my $fuffa = $1;
		while ($fuffa =~ /\/tag\/(.*?)"/gi){
			$tags{$1}++;
		}
	}
	my $key;

	foreach $key (keys %tags){
		$vals{$tags{$key}} .= uri_unescape($key)." ";	
	}
	
	foreach $key (sort {$b <=> $a} keys %vals){
		print "$key -> $vals{$key}\n";
	}
	print "\n";
}




