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):
- req = MyOpener().open(url + '/search/?query=%s&search_type=artist' % (urllib.quote(artist))).read()
- rx_src = re.compile('Artist:.*?artist_id=(.*?)">(.*?)</a>', re.M|re.S|re.I)
- 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))) <= ld:
- link = url + '/artist/artist.php?artist_id=' + match.group(1)
- req = MyOpener().open(link).read()
- bio = re.search('var originalBiography.*?"(.*?)";', req, re.M|re.S|re.I)
- if bio: print re.sub('<.*?>', '', bio.group(1)).replace('\\','')
- break
- url = 'http://www.gracenote.com'
- ld = 3 # more info: http://en.wikipedia.org/wiki/Levenshtein_distance
- try:
- go(sys.argv[1])
- except Exception, e:
- print e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement