Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import encodings.utf_8
- import sys, urllib, re
- class MyOpener(urllib.FancyURLopener):
- version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'
- def strip_white(stp):
- stp = stp.replace('amp;','')
- stp = re.sub('\W+?','', stp.lower())
- return stp
- def levenshtein(s1, s2):
- l1 = len(s1)
- l2 = len(s2)
- matrix = [list(range(l1 + 1))] * (l2 + 1)
- for zz in range(l2 + 1):
- matrix[zz] = list(range(zz,zz + l1 + 1))
- for zz in range(0,l2):
- for sz in range(0,l1):
- if s1[sz] == s2[zz]:
- matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz])
- else:
- matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz] + 1)
- return matrix[l2][l1]
- def go(artist, album):
- src = '/cg/amg.dll?p=amg&sql=%s&P=amg&opt1=2' % (urllib.quote(album))
- rx_src = re.compile('(<tr class="visible".*?<td class="cell" style.*?>)(.*?)(<.*?;"><a href=")(.*?)(">)', re.M|re.S|re.I)
- req = MyOpener().open(url + src).read().decode('latin-1').encode('utf-8')
- for match in rx_src.finditer(req):
- if strip_white(artist) == strip_white(match.group(2)) or levenshtein(strip_white(artist), strip_white(match.group(2))) <= 3:
- global rel
- link = match.group(4).replace('amp;','')
- req = MyOpener().open(url + link + '~T1').read().decode('latin-1').encode('utf-8')
- rel = re.search('<!--Begin Content-->(.*?)<!--ADBANNER-->', req, re.S|re.M)
- break
- def get_review():
- revw = re.search('<td align="left" class="title">.*?"author">by\s.*?</td>.*?<p>(.*?)</p>', rel.group(1), re.M|re.S)
- if revw:
- amg_review = re.sub('<.*?>', '', revw.group(1))
- rev_author = re.search('<td align=\"left\" class=\"title\">.*?"author\">by\s.(.*?)<', rel.group(1), re.M|re.S)
- print amg_review + '\r\nAMG Review by ' + rev_author.group(1)
- url = 'http://www.allmusic.com'
- rel = None;
- try:
- go(sys.argv[1], sys.argv[2])
- if not rel == None: get_review()
- except Exception, e:
- print e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement