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
def _fetchFromUrl(self, url): if not url: return None try: release = None rq = request.Request(url=url) rq.add_header('Referer', self.referer) with request.urlopen(rq) as f: mediaTrackCount = 0 try: s = StringIO((f.read().decode('utf-8'))) o = json.load(s) if 'album' not in o: return None r = o['album'] tracks = r['tracks'] images = r['image'] coverUrl = None if images: for image in images: if isEqual(image['size'], 'extralarge'): coverUrl = image['#text'] release = Release(title=r['name']) release.trackCount = len(tracks) release.coverUrl = coverUrl if 'id' in r and r['id']: release.lastFMId = r['id'] tags = r['tags'] if tags: release.tags = [] for tag in r['tags']['tag']: tagName = tag['name'] if not isInList(release.tags, tagName): release.tags.append(tagName) if tracks: media = ReleaseMedia(releaseMediaNumber=1) media.tracks = [] for t in tracks['track']: track = Track(title=t['name']) track.duration = t['duration'] track.trackNumber = t['@attr']['rank'] if not ([t for t in media.tracks if t.trackNumber == track.trackNumber]): media.tracks.append(track) mediaTrackCount += 1 release.media = [] release.media.append(media) release.mediaCount = 1 release.trackCount = len(media.tracks) if 'wiki' in r and r['wiki']: if 'summary' in r['wiki'] and r['wiki']['summary']: release.lastFMSummary = r['wiki']['summary'] if not release.alternateNames: release.alternateNames = [] cleanedTitle = createCleanedName(release.title) if cleanedTitle not in release.alternateNames and cleanedTitle != release.title: release.alternateNames.append(cleanedTitle) # Not Valid if mediaTrackCount < 1: release = None return release except: self.logger.exception("LastFM: Error In SearchForRelease") pass return release except: self.logger.exception("LastFM: Error In SearchForRelease") pass return None
def lookupReleaseDetails(self, amgId): try: release = None url = "http://api.rovicorp.com/data/v1.1/album/info?apikey=" + self.API_KEY + "&sig=" + str( self._sig()) + "&include=images,releases,styles,tracks&albumid=" + amgId rq = request.Request(url=url) rq.add_header('Referer', self.referer) self.logger.debug("Performing All Music Guide Lookup For Release(s)") with request.urlopen(rq) as f: try: s = StringIO((f.read().decode('utf-8'))) o = json.load(s) if o: if 'status' in o and o['status'] == "ok": album = o['album'] if album: release = Release(title=album['title']) release.amgId = amgId release.tags = [] if 'flags' in album and album['flags']: for flag in album['flags']: if not isInList(release.tags, flag): release.tags.append(flag) rd = (album['originalReleaseDate'] or '').replace("-??", "") if rd: release.releaseDate = parseDate(rd) if 'genres' in album and album['genres']: release.genres = [] for style in album['genres']: genreName = style['name'] if not ([g for g in release.genres if isEqual(g.name, genreName)]): release.genres.append(Genre(name=genreName)) if 'styles' in album and album['styles']: release.genres = release.genres or [] for style in album['styles']: genreName = style['name'] if not ([g for g in release.genres if isEqual(g.name, genreName)]): release.genres.append(Genre(name=genreName)) if 'tracks' in album and album['tracks']: trackCount = 0 currentTrack = 0 releaseMedia = [] for disc in set(map(lambda x: x['disc'], album['tracks'])): media = ReleaseMedia(releaseMediaNumber=disc) media.tracks = [] for amgTrack in ( [t for t in album['tracks'] if isEqual(str(t['disc']), str(disc))]): currentTrack += 1 track = Track(title=amgTrack['title']) track.duration = amgTrack['duration'] track.trackNumber = currentTrack track.releaseMediaNumber = disc track.amgId = amgTrack['ids']['trackId'] if not [t for t in media.tracks if isEqual(t.title, amgTrack['title'])]: media.tracks.append(track) trackCount += len(media.tracks) releaseMedia.append(media) release.media = releaseMedia release.mediaCount = len(releaseMedia) if not release.alternateNames: release.alternateNames = [] cleanedTitle = createCleanedName(release.title) if cleanedTitle not in release.alternateNames and cleanedTitle != release.title: release.alternateNames.append(cleanedTitle) except: self.logger.exception("AllMusicGuide: Error In lookupArtist") pass return release except: self.logger.exception("AllMusicGuide: Error In lookupArtist") pass return None