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 re_parse(dt, rx):
- match = re.search(rx, dt)
- if match: return match.group(1)
- 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)
- req = MyOpener().open(str(url + src)).read().decode('latin-1').encode('utf-8')
- for match in rx_src.finditer(str(req)):
- if strip_white(artist) == strip_white(match.group(2)) or levenshtein(strip_white(artist), strip_white(match.group(2))) <= 3:
- global rel, link
- link = match.group(4).replace('amp;','')
- req = MyOpener().open(url + link).read().decode('latin-1').encode('utf-8')
- rel = re.search('<!--Begin Content-->(.*?)<!--ADBANNER-->', req, re.S|re.M)
- break
- def get_review():
- revw = re_parse(rel.group(1), '<td align=\"left\" class=\"title\">.*?\"author\">by\s.*?</td>.*?<p>(.*?)</p>')
- if revw:
- amg_review = re.sub('<.*?>', '', revw)
- if amg_review.find('Read More...') > 0:
- rx_more = re.compile('<td align="left" class="title">.*?"author">by\s.*?</td>.*?<p>(.*?)</p>', re.M|re.S)
- more = re_parse(rel.group(1), '\.\.\. <a href="(.*?)\">Read More\.\.\.</a>')
- more_review = MyOpener().open(url + more).read().decode('latin-1').encode('utf-8')
- amg_review = re.sub('<.*?>', '', rx_more.search(more_review).group(1))
- amg = amg_review + '\r\nAMG Review by ' + re_parse(rel.group(1), '<td align=\"left\" class=\"title\">.*?"author\">by\s.(.*?)<')
- print amg
- 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