def lookupArtist(self, name): try: artist = None url = self.baseUrl + "artist.getInfo&artist=" + parse.quote_plus(name) rq = request.Request(url=url) rq.add_header('Referer', self.referer) self.logger.debug("Performing LastFM Lookup For Artist") with request.urlopen(rq) as f: try: s = StringIO((f.read().decode('utf-8'))) o = json.load(s) if 'artist' in o and o['artist']: r = o['artist'] artist = Artist(name=r['name']) artist.images = [] if 'mbid' in r: artist.musicBrainzId = r['mbid'] if 'image' in r: images = r['image'] if images: for image in images: if isEqual(image['size'], 'extralarge'): artist.images.append(Image(url=image['#text'])) if 'tags' in r: tags = r['tags'] if tags: artist.tags = [] for tags in r['tags']['tag']: tag = tags['name'] if not isInList(artist.tags, tag): artist.tags.append(tag) if 'bio' in r: bio = r['bio'] if bio: artist.bioContent = bio['content'] except: self.logger.exception("LastFM: Error In LookupArtist") pass # if artist: # print(artist.info()) return artist except: self.logger.exception("LastFM: Error In LookupArtist") pass return None
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 searchForArtist(self, name): """ Perform a search in all enabled search engines and return an aggregate Artist for the given Artist name :param name: String Name of the Artist to find :return: Artist Populated Artist or None if error or not found """ if not name: return None if name in self.cache: return self.cache[name] try: startTime = arrow.utcnow().datetime artist = Artist(name=name) artist.roadieId = str(uuid.uuid4()) if self.iTunesSearcher.IsActive: artist = artist.mergeWithArtist(self.iTunesSearcher.lookupArtist(name)) if self.mbSearcher.IsActive: artist = artist.mergeWithArtist(self.mbSearcher.lookupArtist(name)) if self.lastFMSearcher.IsActive: artist = artist.mergeWithArtist(self.lastFMSearcher.lookupArtist(name)) if self.spotifySearcher.IsActive: artist = artist.mergeWithArtist(self.spotifySearcher.lookupArtist(name)) if self.allMusicSearcher.IsActive: artist = artist.mergeWithArtist(self.allMusicSearcher.lookupArtist(name)) if artist: # Fetch images with only urls, remove any with neither URL or BLOB if artist.images: images = [] firstImageInImages = None for image in artist.images: if not image.image and image.url: image.image = self.imageSearcher.getImageBytesForUrl(image.url) if image.image: # Resize to maximum image size and convert to JPEG img = Image.open(io.BytesIO(image.image)).convert('RGB') img.resize(self.imageMaximumSize) b = io.BytesIO() img.save(b, "JPEG") image.image = b.getvalue() firstImageInImages = firstImageInImages or image.image image.signature = image.averageHash() images.append(image) if images: dedupedImages = [] imageSignatures = [] for image in images: if image.signature not in imageSignatures: imageSignatures.append(image.signature) dedupedImages.append(image) artist.images = dedupedImages if not artist.thumbnail and firstImageInImages: try: img = Image.open(io.BytesIO(firstImageInImages)).convert('RGB') img.thumbnail(self.artistThumbnailSize) b = io.BytesIO() img.save(b, "JPEG") artist.thumbnail = b.getvalue() except: pass # Add special search names to alternate names if not artist.alternateNames: artist.alternateNames = [] if artist.name not in artist.alternateNames: cleanedArtistName = createCleanedName(artist.name) if cleanedArtistName != artist.name.lower().strip() and \ cleanedArtistName not in artist.alternateNames: artist.alternateNames.append(cleanedArtistName) if not artist.bioContext: try: artist.bioContext = wikipedia.summary(artist.name) except: pass self.cache[name] = artist elapsedTime = arrow.utcnow().datetime - startTime printableName = name.encode('ascii', 'ignore').decode('utf-8') self.logger.debug("searchForArtist Elapsed Time [" + str(elapsedTime) + "] Name [" + printableName + "] Found [" + (artist.name if artist else "") + "] MusicBrainzId [" + str(artist.musicBrainzId) + "] " + " iTunesId [" + str(artist.iTunesId) + "] " + " amgId [" + str(artist.amgId) + "]" + " spotifyId [" + str(artist.spotifyId) + "]" .encode('ascii', 'ignore').decode('utf-8') + "]") return artist except: self.logger.exception("Error In searchForArtist") return None