diff options
Diffstat (limited to 'misc.py')
-rw-r--r-- | misc.py | 105 |
1 files changed, 105 insertions, 0 deletions
@@ -1,4 +1,8 @@ from PyQt4 import QtCore, QtGui +import re +import urllib2, httplib, cookielib +import socket +socket.setdefaulttimeout(8) appIcon=QtGui.QIcon('gfx/icon.png') @@ -14,11 +18,112 @@ def sec2min(secs): 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<len(ret): + if ret[end]==')':balance-=1 + if ret[end]=='(':balance+=1 + end+=1 # can do better + # whole function is in ret[start:end] + # find function-name + name=ret[start+1:ret.find('(', start)] + result=None # result of this function + if name=='if': + # $if(param,if-true) + comma=ret.find(',',start) + param1=ret[start+len('$if('):comma] + param2=ret[comma+1:end-1] + result='' + if param1[1:] in tags: + result=param2 + else: + start+=1 + if result!=None: + ret=("%s%s%s")%(ret[0:start], result, ret[end:]) + + # perform all replacements! + for tag in tags: + ret=ret.replace('$%s'%(tag), str(tags[tag])) + return ret + +def fetch(SE, sites, song=None, xtra_tags={}): + """Returns None when nothing found, or [site,source-url].""" + url=format(SE, song, xtra_tags) + url=url.replace(' ', '+') + + request=urllib2.Request(url) + request.add_header('User-Agent', 'montypc') + opener=urllib2.build_opener() + data=opener.open(request).read() + + # look for urls on the search page! + regex=re.compile('<a href="(.*?)".*?>.*?<\/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('<br>', '<br />') + data=data.replace('<br />', '<br />') + data=data.strip() + return [data,finalURL] + return None + class Button(QtGui.QPushButton): iconSize=32 """A simple Button class which calls $onClick when clicked.""" |