#! /usr/bin/python # ScroogLyrics python version, original idea by ultramancool # python coding by mattepiu #needs both mechanize and stripogram modules. import sys, os import array from os.path import *from mechanize import Browser from stripogram import html2text, html2safehtml import re import string #from optparse import OptionParser usage = '''artist title''' br = Browser() class PyLyrics(): def fetchLyrics(self, artist, title): br.open("http://www.scroogle.org/cgi-bin/scraper.htm") assert br.viewing_html() #print br.title() br.select_form(name="qform") #br["n"] = ["5"] br["Gw"] = 'site:www.azlyrics.com OR site:www.lyrics007.com OR site:www.actionext.com OR site:www.sing365.com OR site:artist.letssingit.com OR site:lyricwiki.org OR site:letras.terra.com.br "%s" "%s" lyrics' % (artist, title) response = br.submit() #print "ok" #print br.form mytext = html2safehtml(response.read(),valid_tags=("a")) #print mytext myres = re.compile('\') for x in myres.findall(mytext): print x if x.startswith("http://www.azlyrics.com/lyrics"): return self.azlyrics_scrape(x, artist, title) elif x.startswith("http://www.lyrics007.com"): return self.lyrics007_scrape(x, artist, title) elif x.startswith("http://www.actionext.com"): return self.actionext_scrape(x, artist, title) elif x.startswith("http://www.sing365.com"): return self.sing365_scrape(x, artist, title) elif x.startswith("http://artists.letssingit.com"): return self.letssingit_scrape(x, artist, title) elif x.startswith("http://lyricwiki.org"): return self.lyricwiki_scrape(x, artist, title) elif x.startswith("http://letras.terra.com.br"): return self.terra_scrape(x, artist, title) return 0 def azlyrics_scrape(self, myurl, artist, title): br.open(myurl) #print br.title() textl = br.response().read() a = re.compile("<\/b>
(.*)\[
www.azlyrics.com<\/a> \]

", re.DOTALL).search(textl) if a: ftext = a.groups() else: print "ARGH" return 0 doc = '''dcop amarok contextbrowser showLyrics \" %s\"''' % (myurl, artist, title, ftext) doc = re.sub("
|
", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("

|\(\'|\',\)|
",\)|
",\)|",\)|\[.*\]', '', doc))) os.system(doc.encode('ascii', 'xmlcharrefreplace')) return 1 def lyrics007_scrape(self, myurl, artist, title): br.open(myurl) #print br.title() textl = br.response().read() a = re.compile("print version<\/a>(?P.*)

The hottest songs from ", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \" %s\"''' % (myurl, artist, title, ftext) doc = re.sub("
|
", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("

|
",\)|
",\)|",\)', '', doc))) os.system(doc.encode('ascii', 'xmlcharrefreplace')) return 1 def actionext_scrape(self, myurl, artist, title): br.open(myurl) #print br.title() textl = br.response().read() a = re.compile("(.*)
", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \" %s\"''' % (myurl, artist, title, ftext) doc = re.sub("
|
", "\n", re.sub(r"\\r|\\n|\\t", "" ,re.sub('\("

|
",\)|
",\)|",\)|\',\)|\(\'.*<\/div>', '', doc))) os.system(doc.encode('ascii', 'xmlcharrefreplace')) return 1 def sing365_scrape(self, myurl, artist, title): br.open(myurl) #print br.title() textl = br.response().read() a = re.compile("<\/font><\/a>

'\);(.*)
", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \" %s\"''' % (myurl, artist, title, ftext) doc = re.sub("
|
", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("

|
",\)|\(\"||
\",\)|\",\)', '', doc))) os.system(doc.encode('ascii', 'xmlcharrefreplace')) return 1 def letssingit_scrape(self, myurl, artist, title): br.open(myurl) #print br.title() textl = br.response().read() a = re.compile("
(.*)<\/PRE>", re.DOTALL).search(textl)
		if a:
			ftext = a.groups()
		else:
			return 0
		doc = '''dcop amarok contextbrowser showLyrics \" %s\"''' % (myurl, artist, title, ftext)
		doc = re.sub("
|
", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("

|
",\)|
",\)|",\)|\(\'|\',\)', '', doc))) os.system(doc.encode('ascii', 'xmlcharrefreplace')) return 1 def lyricwiki_scrape(self, myurl, artist, title): br.open(myurl) print br.title() textl = br.response().read() a = re.compile("
(.*)
", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \" %s\"''' % (myurl, artist, title, ftext) doc = re.sub("
|
|
", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("

|
",\)|
",\)|",\)', '', doc))) os.system(doc.encode('ascii', 'xmlcharrefreplace')) return 1 def terra_scrape(self, myurl, artist, title): br.open(myurl) print br.title() textl = br.response().read() a = re.compile("

(.*)

", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \" %s\"''' % (myurl, artist, title, ftext) doc = re.sub("
|
|
", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("

|
",\)|
",\)|",\)|\(\'|\',\)', '', doc))) os.system(doc.encode('ascii', 'xmlcharrefreplace')) return 1 #------------------------------------ #parser = OptionParser() #options, args = parser.parse_args() #artist = args[0] #title = args[1] plugin = PyLyrics() while True: signal=sys.stdin.readline() if re.search('(fetchLyrics|configure)',signal): if re.search('configure', signal): os.system("dcop amarok playlist popupMessage \"This script does not require any configuration.\"") else: artist=string.rstrip(os.popen("dcop amarok player artist").read()) title=string.rstrip(os.popen("dcop amarok player title").read()) if not plugin.fetchLyrics(artist, title): atitle = re.sub(r"\W*\(.*\)\W*","",title) aartist = re.sub(r"\W*\(.*\)\W*","",artist) if ( atitle == title ) & ( aartist == artist ): acc = os.popen("dcop amarok contextbrowser showLyrics").read() if len(acc) <= 60: os.system('''dcop amarok contextbrowser showLyrics \" No Lyrics found.\"''') elif not plugin.fetchLyrics(aartist, atitle): acc = os.popen("dcop amarok contextbrowser showLyrics").read() if len(acc) <= 60: os.system('''dcop amarok contextbrowser showLyrics \" No Lyrics found.\"''')