1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
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<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."""
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)
|