All pastes #829747 Raw Edit

scroogscape modified

public text v1 · immutable
#829747 ·published 2007-12-23 13:04 UTC
rendered paste body
#!/usr/bin/perl
use WWW::Mechanize;
use HTML::Strip;
use HTML::Entities;
 
our $hs = HTML::Strip->new();
our $mech = WWW::Mechanize->new();
sub querylyrics {
	$mech->get("http://www.scroogle.org/cgi-bin/scraper.htm");
	my $artist = urldecode(shift);
	my $title = urldecode(shift);
	$mech->field("Gw", "intitle:\"" . $title . "\" \"" . $artist . "\" lyrics", );
	$mech->submit();
	foreach ($mech->content() =~ m/\d*. <A Href=\"(.*?)\">/ig) {
		if ($_ =~ m/azlyrics\.com\/lyrics/i) {
    		return azlyrics_scrape($_, $artist, $title)
		} elsif ($_ =~ m/lyrics007\.com/) {
			return l007_scrape($_, $artist, $title);
		} elsif ($_ =~ m/actionext\.com/) {
			return actionext_scrape($_, $artist, $title);
		} elsif ($_ =~ m/sing365\.com/) {
			return sing365_scrape($_, $artist, $title);
		} elsif ($_ =~ m/letssingit\.com/) {
			return letssingit_scrape($_, $artist, $title);
		} elsif ($_ =~ m/lyricwiki\.org/) {
			return lyricwiki_scrape($_, $artist, $title);
		} elsif ($_ =~ m/songmeanings\.net/) {
			return songmeanings_scrape($_, $artist, $title);
		} elsif ($_ =~ m/wearethelyrics\.com/) {
			return wearethelyrics_scrape($_, $artist, $title);
		}
	}
	return 0;
}


sub lyricwiki_scrape {
	my $loc = shift;
	$mech->get($loc);
	my $current = $mech->content();
	my $out;
	if ($current =~ m/<div id="lyric">(.*?)<\/div/s) {
		$out = $1;
	} else {
		print "Regex Failed for lyricwiki\n";
		return 0;
	}
	$out =~ s/\n//sgi;
	$out =~ s/<br\/>/\n/sgi;
	my $doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift() . "\" title=\"" . shift() . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}

sub letssingit_scrape {
	my $loc = shift;
	my $out;
	$mech->get($loc);
	my $current = $mech->content();
	if ($current =~ m/<TR class=row2><TD><PRE>(.*)<\/PRE><SPAN class=credits>/s) {
		$out = $1;
	} else {
		print "Regex Failed for letssingit\n";
		return 0;
	}
	my $doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift() . "\" title=\"" . shift() . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}


sub sing365_scrape {
	my $loc = shift;
	$mech->get($loc);
	my $current = $mech->content();
	my $out;
	if ($current =~ m/<\/font><\/a><br><br>'\);(.*)<hr size=1 color=#cccccc>/s) {
		$out = $1;
	} else {
		print "Regex Failed for sing365\n";
		return 0;
	}
	$out =~ s/\n//sgi;
	$out =~ s/<br>/\n/sgi;
	my $doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift() . "\" title=\"" . shift() . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}

sub actionext_scrape {
	my $loc = shift;
	$mech->get($loc);
	my $current = $mech->content();
	my $out;
	if ($current =~ m/<div class="print-title">(.*)<div class="foundat">/s) {
		$out = $1;
	} else {
		print "Regex Failed for actionext\n";
		return 0;
	}
	$out =~ s/\n//sgi;
	$out =~ s/<br \/>/\n/sgi;
	my $doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift() . "\" title=\"" . shift() . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}

sub azlyrics_scrape {
	my $loc = shift;
	$mech->get($loc);
	my $current = $mech->content();
	my $out = "";
	if ($current =~ m/<FONT Face=Verdana size=5><BR>(.*)\[ <a href="http:\/\/www.azlyrics.com">www.azlyrics.com<\/a> \]<BR><BR>/s) {
		$out = $1;
	} else {
		print "Regex Failed for azlyrics $loc\n";
		return 0;
	}
	my $doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift() . "\" title=\"" . shift() . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}

sub l007_scrape {
	my $loc = shift;
	$mech->get($loc);
	my $current = $mech->content();
	my $out = "";
	if ($current =~ m/print version<\/a>(.*)<br><br>The hottest songs from/si) {
		$out = $1;
	} else {
		print "Regex Failed for lyric007\n";
		return 0;
	}
	$out =~ s/\n//sgi;
	$out =~ s/<br>/\n/sgi;
	my $doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift() . "\" title=\"" . shift() . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}

sub songmeanings_scrape {
	my $loc = shift;
	$mech->get($loc);
	my $current = $mech->content();
	if ($current =~ m/<td width="100%" style="text-align:left;">.*<td width="100%" style="text-align:left;">\s*(.*?)\s*<\/td>/si) {
		$out = $1;
	} else {
		print "Regex Failed for songmeanings\n";
		return 0;
	}
	$out =~ s/\n//sgi;
	$out =~ s/<br *\/?>/\n/sgi;
	my $doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift . "\" title=\"" . shift . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}

sub wearethelyrics_scrape {
	my $loc = shift;
	$mech->get($loc);
	$current = $mech->content();
	if ($current =~ m/<\/h3>\n<p>\s*(.*?)\s*<\/p>/si) {
		$out = $1;
	} else {
		print "Regex Failed for wearethelyrics\n";
		return 0;
	}
	$out =~ s/\n//sgi;
	$out =~ s/<br *\/?>/\n/sgi;
	$doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics page_url=\"" . $loc . "\" artist=\"" . shift . "\" title=\"" . shift . "\">" . encode_entities($hs->parse($out)) . "</lyrics>";
	system("dcop", "amarok", "contextbrowser", "showLyrics", $doc);
	return 1;
}


while (1) {
	my $message = <STDIN>;
	chomp($message);
	if ($message =~ /^configure/) {
		system("dcop", "amarok", "playlist", "popupMessage", "This script does not require any configuration.");
	} elsif ($message =~ /^fetchLyrics/) {
		my @tofetch = split(/ /, $message);
		if (querylyrics($tofetch[1], $tofetch[2]) == 0) {
			system("dcop", "amarok", "contextbrowser", "showLyrics", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <lyrics>Failed to find any lyrics.</lyrics>");
		}
	}
}

sub urldecode {
  my $str = shift;
  $str =~ s/%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
  return $str;
}