rendered paste body#! /usr/bin/python# ScroogLyrics python version, original idea by ultramancool# python coding by mattepiu#needs both mechanize and stripogram modules.import sys, osimport arrayfrom os.path import *from mechanize import Browserfrom stripogram import html2text, html2safehtmlimport reimport string#from optparse import OptionParserusage = '''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('\<a href=\"(.*?)\">') 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><BR>(.*)\[ <a href=\"http:\/\/www.azlyrics.com\">www.azlyrics.com<\/a> \]<BR><BR>", re.DOTALL).search(textl) if a: ftext = a.groups() else: print "ARGH" return 0 doc = '''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics page_url=\\"%s\\" artist=\\"%s\\" title=\\"%s\\">%s</lyrics>\"''' % (myurl, artist, title, ftext) doc = re.sub("<br>|<BR>", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("<br><br>|\(\'|\',\)|<br>",\)|<BR>",\)|",\)|<i>\[.*\]</i>', '', 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<mystring>.*)<br><br>The hottest songs from ", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics page_url=\\"%s\\" artist=\\"%s\\" title=\\"%s\\">%s</lyrics>\"''' % (myurl, artist, title, ftext) doc = re.sub("<br>|<BR>", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("<br><br>|<br>",\)|<BR>",\)|",\)', '', 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("</h3>(.*)<div class=\"foundat\">", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics page_url=\\"%s\\" artist=\\"%s\\" title=\\"%s\\">%s</lyrics>\"''' % (myurl, artist, title, ftext) doc = re.sub("<br>|<BR>", "\n", re.sub(r"\\r|\\n|\\t", "" ,re.sub('\("<br><br>|<br>",\)|<BR>",\)|",\)|\',\)|\(\'.*<\/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><br><br>'\);(.*)<hr size=1 color=#cccccc>", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics page_url=\\"%s\\" artist=\\"%s\\" title=\\"%s\\">%s</lyrics>\"''' % (myurl, artist, title, ftext) doc = re.sub("<br>|<BR>", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("<br><br>|<br>",\)|\(\"|</script>|<BR>\",\)|\",\)', '', 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("<TR class=row2><TD><PRE>(.*)<\/PRE>", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics page_url=\\"%s\\" artist=\\"%s\\" title=\\"%s\\">%s</lyrics>\"''' % (myurl, artist, title, ftext) doc = re.sub("<br>|<BR>", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("<br><br>|<br>",\)|<BR>",\)|",\)|\(\'|\',\)', '', 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("<div id=\"lyric\">(.*)</div>", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics page_url=\\"%s\\" artist=\\"%s\\" title=\\"%s\\">%s</lyrics>\"''' % (myurl, artist, title, ftext) doc = re.sub("<br>|<BR>|<br/>", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("<br><br>|<br>",\)|<BR>",\)|",\)', '', 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("<p>(.*)</p>", re.DOTALL).search(textl) if a: ftext = a.groups() else: return 0 doc = '''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics page_url=\\"%s\\" artist=\\"%s\\" title=\\"%s\\">%s</lyrics>\"''' % (myurl, artist, title, ftext) doc = re.sub("<br>|<BR>|<br/>", "\n", re.sub(r"\\r|\\n", "" ,re.sub('\("<br><br>|<br>",\)|<BR>",\)|",\)|\(\'|\',\)', '', 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 \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics>No Lyrics found.</lyrics>\"''') elif not plugin.fetchLyrics(aartist, atitle): acc = os.popen("dcop amarok contextbrowser showLyrics").read() if len(acc) <= 60: os.system('''dcop amarok contextbrowser showLyrics \"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" ?> <lyrics>No Lyrics found.</lyrics>\"''')