All pastes #119256 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-08-06 19:19 UTC
rendered paste body
#! /usr/bin/env python

import os,sys,urllib,re

DEBUG = 0
albumFile = multiArtistFile = artistOverviewFile = artistDiscogFile = None
domain         = 'http://www.allmusic.com'
artistRequest  = '/cg/amg.dll?P=amg&SQL=%s&OPT1=1'

def prepRequest(s):
   s = s.lower()
   if s.startswith('the '):
      s = s.replace('the ','')
   s = s.replace(' ','|')
   return s

def makeRequest(s):
   global albumFile,multiArtistFile,artistOverviewFile,artistDiscogFile
   albumPage      = re.compile(r'<div id="albumpage">.*')
   multiArtist    = re.compile(r'<div id="artistsearchresults">.*')
   artistPage     = re.compile(r'<div id="artistpage">.*')
   artistOverview = re.compile(r'<div id="artistpage">.*')
   artistDiscog   = re.compile(r'(<a HREF=")(/cg/amg.dll[?]p=.*?T2)(">Discography</a></td>)')

   file = urllib.urlopen(domain+s).read()
   if albumPage.search(file) != None:
      if DEBUG:
         print 'found albumpage'
      albumFile = file
      parseAlbumOverview(file)
   elif multiArtist.search(file) != None:
      if DEBUG:
         print 'found multiartists'
      multiArtistFile = file
      parseMultiArtists(file)
   elif artistPage.search(file) != None:
      if DEBUG:
         print 'found artistpage'
      artistOverview=file
      try:
         discogURL = artistDiscog.search(file).group(2)
      except:
         print '\r%s\n%s' % (('no album list found for %s' % (artist)).center(60),('falling back to manual entry mode').center(60))
         manualMode()
         return
      if DEBUG:
         print 'found artist discog'
      artistDiscogFile = urllib.urlopen(domain+discogURL).read()
      parseArtistDiscog(artistDiscogFile)
   else: print '\r no match'

def parseMultiArtists(file):
   global artist
   avList = []
   items = ['url','artist','genre','active']
   item = {}
   table = re.compile(r'(<!--Begin Search Results-->)(.*?)(<!--Begin Right SideBar-->)')
   reg1 = re.compile(r'(<a href=")(/cg/amg.dll[?]p=amg&amp;sql=(1|4)1:.*?)(">)(.*?)(</a>.*?">)(.*?)(</T.*?">)(.*?)(</td>)')
   reg2 = re.compile(r'([0-9s,\ -]+)')
   string = table.search(file).group(2)
   vals = reg1.findall(string)
#   for i  in vals:
#      print i

   for i in vals:
      if reg2.match(i[8]) == None:
         act = 'N/A'
      else:
         act = i[8].strip()
      if i[6].startswith('&nbsp;'):
         gen = 'N/A'
      else:
         gen = i[6].strip()
      avList.append(dict(zip(items,(i[1].strip(),i[4].strip(),gen,act))))

   choice = -1
   curItm = 0
   page = 1

   while choice == -1:
      end = 0
      os.system('clear')
      print '\r%s\n pg.%d%s %-12.12s %s' % (('multiple artists found!').center(60),page,('artist').center(28),'genre',('active years').center(10))
      line()
      page += 1
      for i in range(25):
         if curItm >= len(avList)-1:
            page = 1
            end = 1
            print '\r %2d: %-28.28s %-12.10s %s' % (curItm+1,avList[curItm]['artist'],avList[curItm]['genre'],avList[curItm]['active'].center(10))
            print '\r%s' % (('RETURN for top, 0 for manual entry').center(60))
            curItm = 0
            break
         print '\r %2d: %-28.28s %-12.10s %s' % (curItm+1,avList[curItm]['artist'],avList[curItm]['genre'],avList[curItm]['active'].center(10))
         curItm += 1

      if not end:
         print '\r%s' % (('RETURN for more, 0 for manual entry').center(60))

      print 'choose: ',
      try:
         choice = int(sys.stdin.readline()) -1
      except:
         continue
      print choice
      if choice == -1:
         manualInfo()
         return
      elif choice < -1 or choice > len(avList)-1:
         choice = -1
         curItm = 0
         continue
      else:
         artist = avList[choice]['artist']
         url = avList[choice]['url']
         s = url.split('amp;')
         url=s[0]+s[1]
         makeRequest(url)

def parseArtistOverview(file):
   url = None
   urlreg = re.compile(r'(<a HREF=")(/cg/amg.dll[?]p=amg&amp;uid=.*?)(">Discography</a)')
   url = urlreg.search(file).group(2)
   makeRequest(url)

def parseArtistDiscog(file):
   global album,year
   choice = -2
   albList = []
   items = ['year','url','title','label']
   table = re.compile(r'<!--Begin Album List-->(.*?)<!--End Album List-->')
   reg = re.compile(r'<td class="sorted-cell">(.*?)</td>.*?<a href="(/cg/amg.dll[?]p=amg&amp;sql=10:.*?)">(.*?)</a>.*?<td class="cell" style="width:150px;word-wrap:break-word;">(.*?)</td>')
   string = table.search(file).group(1)
   vals = reg.findall(string)

   for i in vals:
      albList.append(dict(zip(items,(i[0],i[1],i[2],i[3]))))

   for i in albList:
      if i['title'].lower() == album.lower():
         album = i['title']
         year = i['year']
         makeRequest(i['url'])
         return
   os.system('clear')
   print '\r     %-35.35s  %-4.4s  %-25.25s' % ('title', 'year', 'label')
   line()
   if len(albList) < 1:
      print '\r%s' % (('no albums found').center(60))
      return
   for i in range(len(albList)):
      print '\r %2d: %-35.35s  %-4.4s  %-25.25s' % (i+1,albList[i]['title'],albList[i]['year'],albList[i]['label'])
   print '\r%s' % (('0 for manual entry').center(60))
   while (choice == -2):
      print '\rchoose: ',
      try:
         choice = int(sys.stdin.readline()) -1
      except:
         continue
      if choice < -1 or choice > i:
         choice = -2
   album = albList[choice]['title']
   year = albList[choice]['year']
   url = albList[choice]['url']
   s=url.split('amp;')
   url=s[0]+s[1]
   makeRequest(url)

def parseAlbumOverview(file):
   reg4           = re.compile(r'(<TD class="cell">)([0-9]*)(</TD>)') #(?<!"/>)?
   reg5           = re.compile(r'(<a href="/cg/amg.dll[?]p=amg&amp;sql=33:.*?">)(.*?)(</a>)')
   num = reg4.findall(file)
   tit = reg5.findall(file)
#   print file
#   print tit
   os.system('clear')
   print '\r%s' %(('found...collected the following data').center(60))
   line()
   print '\r artist:  %s\n album:   %s\n year:    %s' % (artist.title(),album.title(),year)
   line()
   if len(num) == len(tit) != 0:

      for i in range(len(num)):
         print '\r %02d: %s' % (int(num[i][1]),tit[i][1].title())
   else:
      print '\r%s\n%s' % (('no tracklist found for %s - %s' % (artist,album)).center(60),('falling back to manual entry mode').center(60))
      manualMode()

def manualMode():
   print '\rinput info manually'

def line():
   print '\r ------------------------------------------------------------'

year = 0
string = 'STRING'
print string.title()
print '\r artist: ',
artist = sys.stdin.readline().strip()
album =''
# print '\r album: ',
# album = sys.stdin.readline().strip()
makeRequest(artistRequest%(prepRequest(artist)))