#!/usr/bin/perl -w

# here's the first site plugin for the search engine
# this one searches listen.com
# not nearly finished yet.
# ugly as all hell too.

use strict;
require LWP::UserAgent;

sub meta_search_listen_com{
    my $artist = shift @_;
    my $base = "http://www.listen.com/";
    my $search = "results_exact.jsp?artist=";
    my $ua = new LWP::UserAgent;
    $ua->timeout(10);
    my $query = $base . $search . $artist;
    my $request = new HTTP::Request('GET',$query);
    my $response = $ua->request($request);
    my @records; # where we put the results we find
    
    if($response->is_success){
	
	my @page = split /\n/, $response->content;
	my %been_there;
	foreach(@page){
	    
	    if((/artdetail\.jsp\?artistid=(\d+)\">(.+)<\/A>/) &&
	       !$been_there{$1}){
		my $id = $1;

		my $artist_guess = $2;
		my $go = 0;

		foreach(split /\+/, $query){
		    if($artist_guess =~ /$_/i){ $go = 1;}
		}
		
#		print STDERR $artist_guess;
		if($go){
		    my $fr = $base . "artdetail.jsp?artistid=" . $id;
#		print STDERR $fr;
		    $been_there{$id} = 1;		
		    my $second_request = new HTTP::Request('GET',$fr);
		    my $followup = $ua->request($second_request);
		    
		    print STDERR "\nfollowing up on $id: ";
		    print STDERR "$artist_guess ";
#		    print STDERR " : ", &strip_html($artist_guess);
		    if($followup->is_success){
			push(@records, leaf_parse($followup->content,
						  \%been_there,
						  $query,
						  $artist_guess));
		    }
		}
	    }
	}
    }
    return @records;
}

sub leaf_parse{
    my $content = shift @_;
    my $been_there = shift @_;
    my $query = shift @_;
    my $artist_guess = shift @_;
    my @records;

    my @page = split /\n/, $content;
    my $i = 0;
    
    for ($i = 0; $i < $#page; $i++){
	if(($page[$i] =~ /redir\.jsp\?URL=(.+)\" TARGET=\"Artist\"><B>Download<\/B><\/A>:(.+)<\/FONT>/) &&
	   (!$been_there->{$1})){
	    my %temp;
	    $been_there->{$1} = 1;
	    # the url
	    $temp{'url'} = $1;
	    $temp{'format'} = $2;
	    # the file format
	    if($page[$i] =~ /MP3/i){
		$temp{'format'} = "MP3";
		
	    }
	    # the song title
	    if($page[$i - 3] =~ />(.+)<\/FONT>/){
		$temp{'song'} = $1 || "unknown";
		print STDERR $temp{'song'};
	    }
	    # the artist (from the query if the page doesn't
	    # tell us anything
	    
	    $temp{'artist'} = $artist_guess || $query;
	    push(@records,\%temp);
	}
	if(($page[$i] =~ /<A HREF=\"http:(.*)\.ra\">/) &&
	   (!$been_there->{$1})){
	    my %temp;
	    $been_there->{$1} = 1;
	    $temp{'url'} = "http:" . $1 . ".ra";
	    $temp{'format'} = "realaudio";
	    if($page[$i - 3] =~ />(.+)<\/FONT>/){
		$temp{'song'} = $1;
		print STDERR $temp{'song'}
	    }
	    $temp{'artist'} = $artist_guess || $query;
	    $temp{'song'} = $temp{'song'} || "unknown";
	    push(@records,\%temp);
	}
    }
    return @records;
}

sub strip_html{
    my @stuff = @_;
    foreach(@stuff){
        s/(<[^>]+>)?//;
	s/>//;
	s/<//;
	print STDERR $_;
    }
    return @stuff;
}











