from PyQt4 import QtCore, QtGui import re import urllib2, httplib, cookielib import socket socket.setdefaulttimeout(8) appIcon=QtGui.QIcon('gfx/icon.png') eventLoop=QtCore.QEventLoop() def doEvents(): """Make some time for necessary events.""" eventLoop.processEvents(QtCore.QEventLoop.AllEvents) def sec2min(secs): """Converts seconds to min:sec.""" min=int(secs/60) sec=secs%60 if sec<10:sec='0'+str(sec) return str(min)+':'+str(sec) def numeric_compare(x, y): if x>y: return 1 elif x==y: return 0 return -1 def unique(seq): """Retrieve list of unique elements.""" seen = [] return t(c for c in seq if not (c in seen or seen.append(c))) def format(string, song=None, xtra_tags={}): """Replace all tags in $str with their respective value.""" # what tags are available? if song: tags=song._data else: tags={} for tag in xtra_tags: tags[tag]=xtra_tags[tag] ret=string # first perform some functions: $name(value) func=re.compile('\$[a-z]{2}\(', re.MULTILINE|re.IGNORECASE) start=0 loops=20 # my stupidity is endless, so we better make sure to never always loop! while True and loops>0: loops-=1 match=func.search(ret[start:]) if not match: break # we've found a match! # look for matching parenthesis! start+=match.start() # we have to add, because we start search from $start! end=ret.find('(', start)+1 balance=1 while balance>0 and end.*?<\/a>') urls=regex.findall(data) # look for predefined urls, which are good lyrics-sites # we assume they are in order of importance; the first one matching # is taken 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 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) if match: data=match.group(1) data=data.replace('
', '
') data=data.replace('
', '
') data=data.strip() return [data,finalURL] return None class Button(QtGui.QPushButton): iconSize=32 """A simple Button class which calls $onClick when clicked.""" def __init__(self, caption, onClick=None, iconPath=None, iconOnly=False, parent=None): QtGui.QPushButton.__init__(self, parent) if onClick: self.connect(self, QtCore.SIGNAL('clicked(bool)'), onClick) if iconPath: self.changeIcon(iconPath) if not(iconPath and iconOnly): QtGui.QPushButton.setText(self, caption) self.setToolTip(caption) def setText(self, caption): self.setToolTip(caption) if self.icon()==None: self.setText(caption) def changeIcon(self, iconPath): icon=QtGui.QIcon() icon.addFile(iconPath, QtCore.QSize(self.iconSize, self.iconSize)) self.setIcon(icon)