# -*- coding: utf-8 -*- import encodings.utf_8 import os, sys, subprocess, urllib, re, pickle, win32com.client class MyOpener(urllib.FancyURLopener): version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11' def usage(): print 'Usage: AMG file_path artist album [-track tracknumber title]' def q(enq): return '"' + enq + '"' def last_arg(arg): return re.search(arg, sys.argv[len(sys.argv) - 1]) def strip_white(stp): stp = stp.replace('amp;','') stp = re.sub('\W+?','', stp.lower()) return stp def clean_up(cln): i = ['\\','/','"','`',';',','] for j in range(len(i)/2): cln = cln.replace(i[2*j-2], i[2*j-1]) return cln def re_parse(dx, dt, rx): match = re.search(rx, dt) if match: if dx == 'r': return match.group(1) else: amg[dx] = match.group(1) def re_parse_multi(dx, dt, rx): if dx <> None: res = [] match = re.findall(rx, dx, re.M) if match: for i in range(len(match)): res.append(match[i]) res = '; '.join(res) amg[dt] = res def re_song_review(link): req = MyOpener().open(link).read().decode('latin-1').encode('utf-8') res = re.search('Song Review.*?by\s.(.*?)<.*?

(.*?)

', req, re.S) if res: return re.sub('<.*?>', '', res.group(2).replace(' ',' ') + ' AMG Song Review by ' + res.group(1)) def go(): src = '/cg/amg.dll?p=amg&sql=%s&P=amg&opt1=2' % (urllib.quote(album)) rx_src = re.compile('()(.*?)(<.*?;">)', re.M|re.S) req = MyOpener().open(url + src).read().decode('latin-1').encode('utf-8') if last_arg('_dbg_') <> None: print '\n\r' + 'Search result: ' + url + src for match in rx_src.finditer(req): if strip_white(artist).count(strip_white(match.group(2))) + strip_white(match.group(2)).count(strip_white(artist)) > 0: global rel, trx if last_arg('_dbg_') <> None: print 'AMG artist match: ' + match.group(2).replace('amp;','') + ', for artist: ' + artist + ' [' + album + ']' link = match.group(4).replace('amp;','') amg['AMG_Release_URL'] = url + link req = MyOpener().open(url + link).read().decode('latin-1').encode('utf-8') rel = re.search('(.*?)', req, re.S) if chk_trx() == 1: trx = rel.group(1) break def re_parse_release(): if last_arg('_noc_') == None: cover = re.search('http://image\.allmusic\.com.*?\.jpg', rel.group(1)) if cover and not os.path.isfile(out + 'AMG_' + album + '.jpg'): urllib.urlretrieve(cover.group(0), out + 'AMG_' + album + '.jpg') re_parse('AMG_Rating', rel.group(1), 'rating-stars.*?title=\"(.*?) Star') re_parse('AMG_Release_Date', rel.group(1), 'Release Date.*?(\d{4})') re_parse('AMG_Label', rel.group(1), 'Label.*?>(\w.*?)') re_parse_multi(re_parse('r', rel.group(1), 'Genre Listing-->(.*)(.*)(.*)(.*)