Advertisement
Guest User

AMG_Review.py

a guest
Jun 22nd, 2010
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import encodings.utf_8
  3. import sys, urllib, re
  4.  
  5. class MyOpener(urllib.FancyURLopener):
  6.     version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'
  7.  
  8. def strip_white(stp):
  9.     stp = stp.replace('amp;','')
  10.     stp = re.sub('\W+?','', stp.lower())
  11.     return stp
  12.  
  13. def levenshtein(s1, s2):
  14.   l1 = len(s1)
  15.   l2 = len(s2)
  16.   matrix = [list(range(l1 + 1))] * (l2 + 1)
  17.   for zz in range(l2 + 1):
  18.     matrix[zz] = list(range(zz,zz + l1 + 1))
  19.   for zz in range(0,l2):
  20.     for sz in range(0,l1):
  21.       if s1[sz] == s2[zz]:
  22.         matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz])
  23.       else:
  24.         matrix[zz+1][sz+1] = min(matrix[zz+1][sz] + 1, matrix[zz][sz+1] + 1, matrix[zz][sz] + 1)
  25.   return matrix[l2][l1]
  26.  
  27. def go(artist, album):
  28.     src = '/cg/amg.dll?p=amg&sql=%s&P=amg&opt1=2' % (urllib.quote(album))
  29.     rx_src = re.compile('(<tr class="visible".*?<td class="cell" style.*?>)(.*?)(<.*?;"><a href=")(.*?)(">)', re.M|re.S|re.I)
  30.     req = MyOpener().open(url + src).read().decode('latin-1').encode('utf-8')
  31.     for match in rx_src.finditer(req):
  32.         if strip_white(artist) == strip_white(match.group(2)) or levenshtein(strip_white(artist), strip_white(match.group(2))) <= 3:
  33.             global rel
  34.             link = match.group(4).replace('amp;','')
  35.             req = MyOpener().open(url + link + '~T1').read().decode('latin-1').encode('utf-8')
  36.             rel = re.search('<!--Begin Content-->(.*?)<!--ADBANNER-->', req, re.S|re.M)
  37.             break
  38.  
  39. def get_review():
  40.     revw = re.search('<td align="left" class="title">.*?"author">by\s.*?</td>.*?<p>(.*?)</p>', rel.group(1), re.M|re.S)
  41.     if revw:
  42.         amg_review = re.sub('<.*?>', '', revw.group(1))
  43.         rev_author = re.search('<td align=\"left\" class=\"title\">.*?"author\">by\s.(.*?)<', rel.group(1), re.M|re.S)
  44.         print amg_review + '\r\nAMG Review by ' + rev_author.group(1)
  45.  
  46. url = 'http://www.allmusic.com'
  47. rel = None;
  48.  
  49. try:
  50.     go(sys.argv[1], sys.argv[2])
  51.     if not rel == None: get_review()
  52. except Exception, e:
  53.     print e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement