Exemplo n.º 1
0
 def lookupArtist(self, name, fetchReleases=False):
     # http://api.rovicorp.com/search/v2.1/music/search?apikey=5um457xsnur2a6hp43vuarrs&sig=972d1b7669c7362c4789016442c03b93&query=Men+At+Work&entitytype=artist&include=all
     try:
         if name not in self.cache:
             artist = []
             include = "all"
             if not fetchReleases:
                 include = "images,aliases,associatedwith,groupmembers,musicstyles"
             url = "http://api.rovicorp.com/search/v2.1/music/search?apikey=" + self.API_KEY + "&sig=" + str(
                     self._sig()) + "&query=" + parse.quote_plus(
                     name) + "&entitytype=artist&include=" + include + "&size=1"
             rq = request.Request(url=url)
             rq.add_header('Referer', self.referer)
             self.logger.debug("Performing All Music Guide Lookup For Artist")
             with request.urlopen(rq) as f:
                 try:
                     s = StringIO((f.read().decode('utf-8')))
                     o = json.load(s)
                     if o:
                         if 'searchResponse' not in o \
                                 or 'results' not in o['searchResponse'] \
                                 or not o['searchResponse']['results'][0]:
                             return None
                         amgArtist = o['searchResponse']['results'][0]['name']
                         if amgArtist:
                             artist = Artist(name=amgArtist['name'])
                             artist.amgId = amgArtist['ids']['nameId']
                             artist.artistType = ArtistType.Group if amgArtist['isGroup'] else ArtistType.Person
                             if 'musicGenres' in amgArtist and amgArtist['musicGenres']:
                                 if 'genre' in amgArtist['musicGenres']:
                                     artist.genres = []
                                     for genre in amgArtist['musicGenres']:
                                         if not isInList(artist.genres, genre):
                                             artist.genres.append(genre)
                             bd = None
                             if 'birth' in amgArtist and 'date' in amgArtist['birth']:
                                 bd = amgArtist['birth']['date'].replace("-??", "")
                             if bd:
                                 if artist.artistType == ArtistType.Person:
                                     artist.birthDate = parseDate(bd)
                                 else:
                                     artist.beginDate = parseDate(bd)
                             ed = (amgArtist['death']['date'] or '').replace("-??", "")
                             if ed:
                                 artist.endDate = parseDate(ed)
                             if 'discography' in amgArtist and amgArtist['discography']:
                                 artist.releases = []
                                 for album in amgArtist['discography']:
                                     release = Release(title=album['title'])
                                     release.amgId = album['ids']['albumId']
                                     rd = (album['year'] or '').replace("-??", "")
                                     if rd:
                                         release.releaseDate = parseDate(rd)
                                     release.releaseType = album['type']
                                     release.tags = []
                                     if 'flags' in album and album['flags']:
                                         for flag in album['flags']:
                                             if flag not in release.tags:
                                                 release.tags.append(flag)
                                     release.releaseLabels = []
                                     if album['label']:
                                         label = Label(name=album['label'], sortName=album['label'])
                                         release.releaseLabels.append(ReleaseLabel(label=label, release=release))
                                     release.releaseType = album['type']
                                     release.trackCount = 0
                                     artist.releases.append(release)
                             # TODO groupMembers
                             if 'images' in amgArtist:
                                 artist.images = []
                                 try:
                                     for image in amgArtist['images']:
                                         if image['formatid'] == 16:  # the largest images
                                             imageUrl = image['url']
                                             if imageUrl not in artist.images:
                                                 artist.images.append(Image(url=imageUrl))
                                 except:
                                     pass
                             try:
                                 if 'musicBioOverview' in amgArtist['musicBio']:
                                     artist.bioContext = amgArtist['musicBio']['musicBioOverview'][0]['overview']
                             except:
                                 pass
                             if 'musicStyles' in amgArtist and amgArtist['musicStyles']:
                                 artist.genres = []
                                 for style in amgArtist['musicStyles']:
                                     genreName = style['name']
                                     if not ([g for g in artist.genres if isEqual(g.name, genreName)]):
                                         artist.genres.append(Genre(name=genreName))
                                         # TODO associateWith
                 except HTTPError:
                     print("Spotify: Http Error")
                 except:
                     self.logger.exception("AllMusicGuide: Error In lookupArtist")
                     pass
                     # if artist:
                     #     print(artist.info())
             self.cache[name.lower()] = artist
         return self.cache[name.lower()]
     except:
         self.logger.exception("AllMusicGuide: Error In lookupArtist")
         pass
     return None