#!/usr/bin/perl
# goear2disk.pl
# GPL v3 by badblody & KaR]V[aN
# http://karman.homelinux.net
# karman.san@gmail.com
#

use IO::Socket;

print "goear2disk.pl by badblody & KaR]V[aN, GPLv3 2008\n";
print "http://karman.homelinux.net\n";
my $salida = 0;
while ($salida == 0) {
	my $busqueda; print "\n";
	print "Termino a buscar: "; $busqueda = <STDIN>; chop($busqueda);
	print "Error\n" if(&goear(&ascii2hex($busqueda)) eq "Error");
	print "\nRealizar otra busqueda? (S/n): "; my $otra = <STDIN>; chop($otra); #print "\n";
	if($otra eq 'n') { $salida = 1; }
}

sub goear {
	print "Buscando.. \n\n";
	my @resp;
	$packed_ip = gethostbyname("goear.com");
    	if (defined $packed_ip) {
        	$ip = inet_ntoa($packed_ip);
    	}
	my $peticion = "GET /search.php?q=$_[0] HTTP/1.1\r\n";
	$peticion .= "Host: www.goear.com\r\n";
	$peticion .= "Connection: Close\r\n\r\n";

	eval {
		local $SIG{ALRM} = sub { alarm 0; return "Error" };
		alarm 10;
		my $socketear = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $ip, PeerPort => 80) || return "Error";
		if(!$socketear) {
			alarm 0; return "Error";
		} else {
			print $socketear $peticion;
			@resp = <$socketear>;
			close($socketear);
			alarm 0;
		}
	};

	my $juntito = join("",@resp);
	$juntito =~ s/\r//g;
	$juntito =~ s/\n//g;
	$juntito =~ m/googlesyndication/g;
	my $bueno = substr($juntito,pos($juntito));

	my @collectedid;
	my @collectedtitle;

	if ($bueno =~ /<div id="boxlast">Result of the search \((\d+)\)<\/div>/) {
		if ($1 == 0) { print "No se han encontrado resultados\n"; return; }
		if ($1 < 10) { print "Mostrando $1 resultados\n"; }
		else { print "Mostrando los 10 primeros resultados de $1\n"; }
	}
								
	$juntito =~ m/googlesyndication/g;
	my $bueno = substr($juntito,pos($juntito));
	$juntito =~ m/<div id\=\"box2\">/g;
	my $bueno = substr($juntito,pos($juntito));
	$juntito =~ m/<div id\=\"box2\">/g;
	my $bueno = substr($juntito,pos($juntito));
				
	
	while ($bueno =~ /<a title="(.+?)" href="listen\.php\?v=(.+?)" class="escuchar"/g) {
		push(@collectedtitle,$1);
		push(@collectedid,$2);
	}

	return "Error" if (@collectedtitle != @collectedid);

	my @enlaces;
	for(my $i=0;$i<@collectedid;$i++) {
		my $xmlpeti = "GET /files/sst/secm$collectedid[$i].xml HTTP/1.1\r\n";
		$xmlpeti .= "Host: www.goear.com\r\n";
		$xmlpeti .= "Referer: www.goear.com\r\n";
		$xmlpeti .= "Connection: Close\r\n\r\n";
		
		my $socketxml = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $ip, PeerPort => 80) || return "Error";
		print $socketxml $xmlpeti;
		my @resp = <$socketxml>;
		close($socketear);

		foreach my $linea (@resp) {
			if ($linea =~ /<song path="http:\/\/www\.(.+?)"/) {
				push(@enlaces,"http://$1");
			}
		}
	}

	for(my $i=0;$collectedid[$i];$i++) {
		print "$collectedtitle[$i]: $enlaces[$i]\n";
	}
}


# sub ascii2hex por Nitz, http://blogantastico.blogspot.com
sub ascii2hex {
	my $resultado;
	@entrada=split(//,$_[0]);
	for($i=0;$i<@entrada;$i++){
		if(ord(($entrada[$i])>=0 and ord($entrada[$i])<=47) or (ord($entrada[$i])>=58 and ord($entrada[$i])<=64) or (ord($entrada[$i])>=91 and ord($entrada[$i])<=96) or (ord($entrada[$i])>=123 and ord($entrada[$i])<=255)){
			if(length(sprintf("%X",ord($entrada[$i])))<=1){$resultado.="%0".sprintf("%X",ord($entrada[$i]))}
			else{$resultado.="%".sprintf("%X",ord($entrada[$i]))}
		}
		else{$resultado.=$entrada[$i];}
	}
	return $resultado;
}       
																	
											
