-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
340 lines (319 loc) · 13.9 KB
/
__init__.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -*- coding: utf-8 -*-
PLUGIN_NAME = u"AllMusic Genres"
PLUGIN_AUTHOR = u"Dave Hayes"
PLUGIN_DESCRIPTION = "Scrape AllMusic for Genre Tags"
PLUGIN_VERSION = "0.1"
PLUGIN_API_VERSIONS = ["0.9.0", "0.10"]
try:
from BeautifulSoup import BeautifulSoup
except:
try:
from picard.plugins.allmusic.BeautifulSoup import BeautifulSoup
except:
raise ImportError, "Could not import BeautifulSoup. Get it at http://www.crummy.com/software/BeautifulSoup/"
from PyQt4 import QtCore
from picard.ui.options import register_options_page, OptionsPage
from picard.config import BoolOption, IntOption, TextOption
#from picard.plugins.allmusic.ui_options_allmusic import Ui_AllMusicOptionsPage
from picard.metadata import register_album_metadata_processor, register_track_metadata_processor
from picard.util import partial, translate_artist
from picard.similarity import similarity2
import re
def ununicode (unicrap):
xlate={0xc0:'A', 0xc1:'A', 0xc2:'A', 0xc3:'A', 0xc4:'A', 0xc5:'A',
0xc6:'Ae', 0xc7:'C',
0xc8:'E', 0xc9:'E', 0xca:'E', 0xcb:'E',
0xcc:'I', 0xcd:'I', 0xce:'I', 0xcf:'I',
0xd0:'Th', 0xd1:'N',
0xd2:'O', 0xd3:'O', 0xd4:'O', 0xd5:'O', 0xd6:'O', 0xd8:'O',
0xd9:'U', 0xda:'U', 0xdb:'U', 0xdc:'U',
0xdd:'Y', 0xde:'th', 0xdf:'ss',
0xe0:'a', 0xe1:'a', 0xe2:'a', 0xe3:'a', 0xe4:'a', 0xe5:'a',
0xe6:'ae', 0xe7:'c',
0xe8:'e', 0xe9:'e', 0xea:'e', 0xeb:'e',
0xec:'i', 0xed:'i', 0xee:'i', 0xef:'i',
0xf0:'th', 0xf1:'n',
0xf2:'o', 0xf3:'o', 0xf4:'o', 0xf5:'o', 0xf6:'o', 0xf8:'o',
0xf9:'u', 0xfa:'u', 0xfb:'u', 0xfc:'u',
0xfd:'y', 0xfe:'th', 0xff:'y',
0xa1:'!', 0xa2:'{cent}', 0xa3:'{pound}', 0xa4:'{currency}',
0xa5:'{yen}', 0xa6:'|', 0xa7:'{section}', 0xa8:'{umlaut}',
0xa9:'{C}', 0xaa:'{^a}', 0xab:'<<', 0xac:'{not}',
0xad:'-', 0xae:'{R}', 0xaf:'_', 0xb0:'{degrees}',
0xb1:'{+/-}', 0xb2:'{^2}', 0xb3:'{^3}', 0xb4:"'",
0xb5:'{micro}', 0xb6:'{paragraph}', 0xb7:'*', 0xb8:'{cedilla}',
0xb9:'{^1}', 0xba:'{^o}', 0xbb:'>>',
0xbc:'{1/4}', 0xbd:'{1/2}', 0xbe:'{3/4}', 0xbf:'?',
0xd7:'*', 0xf7:'/'
}
r = ''
for i in unicrap:
if xlate.has_key(ord(i)):
r += xlate[ord(i)]
elif ord(i) >= 0x80:
pass
else:
r += str(i)
return r
def get_close_matches(search_string, search_set, max_results, min_score):
winners = []
matches = []
for items in search_set:
score = similarity2(search_string, items)
if score > min_score:
winners.append([score, items])
if winners == []:
return []
winners.sort()
winners.reverse()
for items in winners:
if len(matches) < max_results:
matches.append(items[1])
return matches
def finalize_genres(styles, metadata, target, albumtitle, albumartist, album, html):
if styles == [] and target == "album_data" and albumartist != "Various Artists":
album._requests += 1
album.tagger.xmlws.add_task(partial(artist_search, album, metadata, albumtitle, albumartist), position=1)
elif styles == [] and (target == "artist_data" or target == "artist_search"):
album._requests += 1
album.tagger.xmlws.add_task(partial(scrape_genres, html, album, "scrape_genre", albumtitle, albumartist, metadata), position=1)
elif styles == []:
print " * Dang, couldn't find anything!\n"
else:
metadata["genre"] = styles
for track in album._new_tracks:
track.metadata["genre"] = styles
print "\n"
def scrape_genres(html, album, target, albumtitle, albumartist, metadata):
styles = []
print " * Scraping genres:",
try:
for style in html.find(text="Begin Genre Listing").findNext('td').div.ul.findAll('a'):
styles.append(style.contents[0].replace("/ ","/").replace("R&B;", "R&B"))
if styles == []:
print "none"
for style in styles:
if style != styles[0]:
print "\b, " + style,
else:
print style,
except:
print "none"
finally:
finalize_genres(styles, metadata, target, albumtitle, albumartist, album, html)
album._requests -= 1
album._finalize_loading(None)
def scrape_styles(html, album, target, albumtitle, albumartist, metadata):
styles = []
print " * Scraping styles:",
try:
for style in html.find(text="Genre Listing").findNext('td').div.ul.findAll('a'):
styles.append(style.contents[0].replace("/ ","/").replace("R&B;", "R&B"))
if styles == []:
print "none"
for style in styles:
if style != styles[0]:
print "\b, " + style,
else:
print style,
except:
print "none"
finally:
finalize_genres(styles, metadata, target, albumtitle, albumartist, album, html)
album._requests -= 1
album._finalize_loading(None)
def parse_artist_search(search_results, albumartist):
brackets = re.compile(r" \[.*]")
results = []
artists = []
print " * Parsing artist search results",
try:
for listings in search_results.find(id="results-table").findAll('tr'):
try:
artist_string = listings.find(style="width:290px;word-wrap:break-word;").find('a').string
artist_url = listings.find(style="width:290px;word-wrap:break-word;").find('a')['href']
artist_string = unicode(ununicode(re.sub(r" \[.*]", r"", artist_string)))
results.append([artist_string,artist_url])
artists.append(artist_string)
except:
pass
except:
pass
if results == []:
print "[fail]"
return
print "[ OK ]"
print " * Looking for a close match:",
match = get_close_matches(albumartist, artists, 1, 0.85)
if match == []:
print "none"
return None
print match[0]
for search in results:
if match[0] == search[0]:
return search[1]
return None
def parse_album_search(search_results, albumartist, albumtitle):
results = []
strings = []
artists = []
condense = []
titles = []
print " * Parsing album search results",
try:
for listings in search_results.find(id="results-table").findAll('tr', attrs={"class" : "visible"}):
try:
artist_string = unicode(ununicode(listings.find(style="width:206px;word-wrap:break-word;").string))
album_string = unicode(ununicode(listings.find(style="width:230px;word-wrap:break-word;").a.string))
album_url = unicode(ununicode(listings.find(style="width:230px;word-wrap:break-word;").a["href"]))
artist_string = artist_string.replace('Original Soundtrack','Various Artists')
search_string = artist_string + album_string
results.append([artist_string, album_string, search_string, album_url])
except:
pass
except:
pass
if results == []:
print "[fail]"
return None
print "[ OK ]"
for records in results:
strings.append(records[2])
artists.append(records[0])
print " * Looking for a close match:",
artist_match = get_close_matches(albumartist, artists, 3, 0.87)
if artist_match != []:
for search in results:
for items in artist_match:
if items == search[0]:
if search[3] in condense:
pass
else:
condense.append(search)
for records in condense:
titles.append(records[1])
album_match = get_close_matches(albumtitle, titles, 1, 0.75)
if album_match != []:
for search in condense:
if album_match[0] == search[1]:
print search[1] + " by " + search[0]
return search[3]
search_string = albumartist + albumtitle
match = get_close_matches(search_string, strings, 1, 0.87)
if match == []:
print "none"
return None
for search in results:
if match[0] == search[2]:
print search[1] + " by " + search[0]
return search[3]
return None
def sanitize_data(data, albumartist):
data = data.replace('style=padding-right:20px;"','')
if albumartist == u"Various Artists":
data = data.replace('<class="subtitle">Various Artists', '<a href="" class="subtitle">Various Artists</a>')
data = re.sub(r"(?is)(<script[^>]*>)(.*?)(</script>)", " ", data)
data = data.replace('<td colspan="2" height=280px">','<td colspan="2" height="280px">')
data = data.replace('<<', '<<')
data = data.replace('>>', '>>')
data = data.replace('&', '&')
return data
def _data_downloaded(album, target, albumtitle, albumartist, metadata, data, http, error):
try:
if error:
print "[fail] "
print str(http.errorString())
album.log.error(str(http.errorString()))
else:
print "[ OK ] "
data = sanitize_data(data, albumartist)
try:
html = BeautifulSoup(data)
except:
print " * CRAP! Looks like we're getting bad (album) HTML from allmusic.com\n"
if target == "album_search" or target == "album_data":
album._requests += 1
print " * Sending artist search request",
artist_search(album, metadata, albumtitle, albumartist)
return
if target == "artist_search" or target == "artist_data":
return
if target == "album_search":
album_url = parse_album_search(html, albumartist, albumtitle)
if album_url != None:
print " * Requesting album data",
album._requests += 1
get_data(album_url, "album_data", albumtitle, albumartist, album, metadata)
elif albumartist != "Various Artists":
print " * Sending artist search request",
album._requests += 1
artist_search(album, metadata, albumtitle, albumartist)
else:
finalize_genres([], metadata, "scrape_genre", albumtitle, albumartist, album, html)
try:
if target == "artist_search" and html.find('title').string == "allmusic":
artist_url = parse_artist_search(html, albumartist)
if artist_url == None:
finalize_genres([], metadata, target, albumtitle, albumartist, album, html)
else:
print " * Requesting artist data",
album._requests += 1
get_data(artist_url, "artist_data", albumtitle, albumartist, album, metadata)
if target == "album_data" or target == "artist_data" or (target == "artist_search" and html.find('title').string != "allmusic"):
album._requests += 1
scrape_styles(html, album, target, albumtitle, albumartist, metadata)
except:
print " * CRAP! Looks like we're getting bad (artist) HTML from allmusic.com\n"
finally:
album._requests -= 1
album._finalize_loading(None)
def get_data(path, target, albumtitle, albumartist, album, metadata):
try:
album._requests += 1
album.tagger.xmlws.download("allmusic.com", 80, str(path),
partial(_data_downloaded, album, target, albumtitle, albumartist, metadata), position=1)
finally:
album._requests -= 1
return False
def _post(album, host, port, path, data, handler):
header = album.tagger.xmlws._prepare("POST", host, port, path)
album.tagger.xmlws.log.debug("POST-DATA %r", data)
requestid = album.tagger.xmlws.request(header, data)
album.tagger.xmlws._request_handlers[requestid] = (handler, False)
return True
def post(album, host, port, path, data, handler, position=None):
func = partial(_post, album, host, port, path, data, handler)
album.tagger.xmlws.add_task(func, position)
def post_data(search_data, target, albumtitle, albumartist, album, metadata):
print search_data,
try:
album._requests += 1
post(album, "allmusic.com", 80, "/cg/amg.dll", search_data,
partial(_data_downloaded, album, target, albumtitle, albumartist, metadata), position=1)
finally:
album._requests -= 1
return False
def album_search(album, metadata, albumtitle, albumartist):
search_data = "P=amg&opt1=2&sql=" + QtCore.QUrl.toPercentEncoding(albumtitle)
return post_data(search_data, "album_search", albumtitle, albumartist, album, metadata)
def artist_search(album, metadata, albumtitle, albumartist):
search_data = "P=amg&opt1=1&sql=" + QtCore.QUrl.toPercentEncoding(albumartist)
return post_data(search_data, "artist_search", albumtitle, albumartist, album, metadata)
def clean_album_title(albumtitle):
albumtitle = re.sub(r"\s+\(disc (\d+)(?::\s+([^)]+))?\)", r"", albumtitle)
albumtitle = re.sub(r"\s+\(bonus disc(?::\s+([^)]+))?\)", r"", albumtitle)
return albumtitle
def allmusic_genre(album, metadata, release):
if metadata["albumartist"] != "Various Artists":
albumartist = translate_artist(metadata['artist'], metadata['artistsort'])
else:
albumartist = metadata["albumartist"]
albumtitle = clean_album_title(metadata["album"])
albumartist = unicode(ununicode(albumartist))
albumtitle = unicode(ununicode(albumtitle))
print " * Looking for " + albumtitle + " by " + albumartist
print " * Sending album search request",
album._requests += 1
album.tagger.xmlws.add_task(partial(album_search, album, metadata, albumtitle, albumartist), position=1)
register_album_metadata_processor(allmusic_genre)
#register_options_page(AllMusicOptionsPage)