diff options
Diffstat (limited to 'plugins/Lyrics.py')
-rw-r--r-- | plugins/Lyrics.py | 69 |
1 files changed, 17 insertions, 52 deletions
diff --git a/plugins/Lyrics.py b/plugins/Lyrics.py index 895a401..9cb0b4e 100644 --- a/plugins/Lyrics.py +++ b/plugins/Lyrics.py @@ -1,8 +1,5 @@ from PyQt4 import QtGui,QtCore -import urllib2, httplib, cookielib -import socket -socket.setdefaulttimeout(8) import re from thread import start_new_thread from traceback import print_exc @@ -104,64 +101,31 @@ class wgLyrics(QtGui.QWidget): if line.strip(): sites[line[0:line.find('\t')]]=line[line.find('\t'):].strip() # construct URL to search! - url=settings.get('lyrics.engine', LY_DEFAULT_ENGINE) - url=url.replace('$artist', song.getArtist()) - url=url.replace('$title', song.getTitle()).replace('$album', song.getAlbum()) - url=url.replace(' ', '+') + SE=settings.get('lyrics.engine', LY_DEFAULT_ENGINE) try: - request=urllib2.Request(url) - request.add_header('User-Agent', 'montypc') - opener=urllib2.build_opener() - data=opener.open(request).read() - - # look for urls! - regex=re.compile('<a href="(.*?)".*?>.*?<\/a>') - urls=regex.findall(data) - - # look for predefined urls, which are good lyrics-sites - finalURL=None - finalRegex=None - for url in urls: - if finalURL: - break - for site in sites: - if url.find(site)>=0: - finalURL=url - finalRegex=sites[site] - break - - match=None - QtCore.QCoreApplication.postEvent(self, ResetEvent(song)) - #print finalURL - if finalURL: - cj = cookielib.CookieJar() - opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) - r = opener.open(finalURL) - data=r.read() - regex=re.compile(finalRegex, re.IGNORECASE|re.MULTILINE|re.DOTALL) - match=regex.search(data) - #print match - if match: - data=match.group(1) - data=data.replace('<br>', '<br />') - data=data.replace('<br />', '<br />') - data=data.strip() + ret=fetch(SE, sites, song, {}) + if ret: + txt='%s<br /><br /><a href="%s">%s</a>'%(ret[0],ret[1],ret[1]) # save for later use! if lyFName: # we can't save if the path isn't correct - file=open(lyFName, 'w') - file.write(data) - file.close() - data='%s<br /><br />(source: <a href="%s">%s</a>)'%(data,finalURL,finalURL) + try: + file=open(lyFName, 'w') + file.write(ret[0]) + file.close() + except: + # probably a wrong path! + pass else: - data='Lyrics not found :\'(' - - QtCore.QCoreApplication.postEvent(self, AddHtmlEvent(data)) + txt="No lyrics found :'(" + QtCore.QCoreApplication.postEvent(self, ResetEvent(song)) + QtCore.QCoreApplication.postEvent(self, AddHtmlEvent(txt)) except: print_exc() QtCore.QCoreApplication.postEvent(self, ResetEvent(song)) - QtCore.QCoreApplication.postEvent(self, AddHtmlEvent('Oh noes, an error occured while fetching lyrics!')) + QtCore.QCoreApplication.postEvent(self, AddHtmlEvent('Woops, site unavailable!'\ + '<br />You have an internet connection?')) self._fetchCnt=0 def onDisconnect(self, params): @@ -178,6 +142,7 @@ class pluginLyrics(Plugin): o=None def __init__(self, winMain): Plugin.__init__(self, winMain, 'Lyrics') + def _load(self): self.o=wgLyrics(None) def getInfo(self): return "Show (and fetch) the lyrics of the currently playing song." |