#!/usr/bin/perl -w

use strict;
use Data::Dumper;
use LWP::Simple;
use HTML::Entities;

my $commandline=shift;
my $commandline2=shift;
my $commandline3=shift;
print "Del.icio.us tags screenscraping for all users in $commandline (saving to $commandline2) \n";


my $url;
my $page;
# my $browser = LWP::UserAgent->new;
my %masterlist=();

# Open file mentioned on commandline and read in all existing usernames (one per line)...

my @usernames;
my $username;
open (FPOINTER,$commandline);
while ($username= <FPOINTER>) {
	chomp $username;
	push @usernames, $username;
	# print "$username \n";
}
close (FPOINTER);

@usernames=("Bane","Bill22");

# now get all the tags for a username from del.icio.us/username/ 

foreach $username (@usernames) {
	#$username=$usernames[0];
	my @templist;	
	my $templine="";
	print "*$username \n";

	# page get needs to be done once for every username... 
	$url="http://del.icio.us/$username/";
	$page= get($url);

	# now take the usernames and add them to the array

	foreach (split ('<li><span>', $page))
	{
		# the information will be on the first line of this part...
		my @lines=split ('\n',$_);
		# print "*******\n $lines[0] \n ";
		if($lines[0]=~/[0-9]+ <\/span><a href="\/([^>]*)>([^>]*)<\/a>/){
			print "===== $2 \n";
			# push @templist,$2;
			$templine.="$2\t";
		}
			
	
	}

	$masterlist{ $username } = $templine;
	my $random_number=rand(60);
	my $delay=30+$random_number;
	sleep $delay; 
}


# print Dumper(%masterlist);
	
#my @uniquenames;
# Input: @list
# Output: @uniqed
#my %u = ();
#@uniquenames = grep {defined} map {
#if (exists $u{$_}) { undef; } else { $u{$_}=undef;$_; }
#} @usernames;
#undef %u;

# now save the array to filename

open (WRITEPOINTER, ">$commandline2") or die ("Couldn't open $commandline2\n");
foreach $username (@usernames){

	print $username."\t$masterlist{$username} \n";
	print WRITEPOINTER $username."\n";
	print WRITEPOINTER $masterlist{$username}."\n";
}
close (WRITEPOINTER);


my @ultimatetaglist;
foreach $username (@usernames){
	my @taglist=split('\t',$masterlist{$username});
	push @ultimatetaglist,@taglist;
}

my $tagname;

my @uniquenames;
# Input: @list
# Output: @uniqed
my %u = ();
@uniquenames = grep {defined} map {
if (exists $u{$_}) { undef; } else { $u{$_}=undef;$_; }
} @ultimatetaglist;
undef %u;

open  (WRITEPOINTER, ">$commandline3") or die ("Couldn't open $commandline3\n");
foreach $tagname (sort @uniquenames){
	print WRITEPOINTER $tagname."\n";
# 	print WRITEPOINTER $tagname."\n";
}
close (WRITEPOINTER);

