#gets info from gimages downloaded pages and ?
use LWP::Simple;
use LWP::UserAgent;
use HTTP::GetImages;

my $ua = LWP::UserAgent->new;
$ua->timeout(17);
$ua->env_proxy;
$|++;

$data="C:\\projects\\chartsweep90s\\90s-final\.htm";
open DATA,"<$data";
@lines=<DATA>;
%hits=();
foreach $line (@lines)
{
	if ($line =~ /GETT/)
	{
		push(@goodlines,$line);
	}
}


foreach $line (@goodlines)
{
	$line =~ s/[\(\)\/\\\&\+\.\;é\n]//gi;
	#print "line is $line\n";
	@splitline=split("\<td\>",$line);
	$song="0";
	$artist="0";
	$date="0";
	$savedir="";
	$date=$splitline[2];
	$song=$splitline[3];
	$artist=$splitline[4];
	#print "$date $song $artist\n";
	if ($artist)
	{$savedir=$artist;
	$savedir =~ s/ //g;}
	$hits{$date}=[$date,$artist,$song];
	$searchurl='http://anonymouse.org/cgi-bin/anon-www.cgi/http://images.google.com/images?q=';
	
	
	
	#print "searchurl is $searchurl\n";
	if ($artist && $song)
	{
		$hits{$date}[4]=$searchurl.$artist."\+".$song;
		
		#@b=reverse sort {length $a <=> length $b} @splitsong;
		
	}
}


sub get_direct_links
{
	foreach $date (sort keys %hits)
	{
		$cleanartist=$hits{$date}[1];
		$cleanartist=~s/ //g;
		#print `move \/Y images\\$cleanartist\\\*\.jpg images\\$date\n`;
	$url=$hits{$date}[$i];
		my $response = $ua->get($url);
		print $response->content;
		if ($response->is_success) {
		print `mkdir images\\$date`;
		print "got googles return page at least\n";
		$stuff=$response->content;
		while ($stuff=~ /(http[^\=\&]*\.jpg)/g)
		{
			$imageloc=$1;
			push(@imagelocs,$imageloc) if ($imageloc =~ !/images\.google/);
		}	
		gett($date);
		}
	else {print "response was NOT success!\n";}

	}
}

get_direct_links;


sub gett
{
$dir=shift(@_);
print "imagelocs is \n";
print join("\n",@imagelocs);
print "dir is $dir\n";
	$_ = new HTTP::GetImages (
                dir  => "images\/$dir",
                todo => [@imagelocs],
                dont => ['ALL'],
                chat => 1,
		max_attempts => 1,
	);

        $_->print_imgs;
        #$_->print_done;
        #$_->print_failed;
        #$_->print_ignored;

        my $hash = $_->imgs_as_hash;
        foreach (keys %{$hash}){
                warn "$_ = ",$hash->{$_},"\n";
        }
	@imagelocs=();
}