def test_get(self): with open(os.path.join(LOCALDIR, 'response/music_a7f.json')) as fp: body = fp.read() HTTPretty.register_uri( HTTPretty.GET, 'http://api.fanart.tv/webservice/artist/e3c7f0d0beeaf45b3a0dd3b9dd8a3338/24e1b53c-3085-4581-8472-0b0088d2508c/JSON/all/1/2', body=body) a7f = Artist.get(id='24e1b53c-3085-4581-8472-0b0088d2508c') self.assertEqual(a7f.mbid, '24e1b53c-3085-4581-8472-0b0088d2508c')
def test_get(self): with open(os.path.join(LOCALDIR, 'response/music_a7f.json')) as fp: body = fp.read() HTTPretty.register_uri( HTTPretty.GET, 'http://api.fanart.tv/webservice/artist/e3c7f0d0beeaf45b3a0dd3b9dd8a3338/24e1b53c-3085-4581-8472-0b0088d2508c/JSON/all/1/2', body=body ) a7f = Artist.get(id='24e1b53c-3085-4581-8472-0b0088d2508c') self.assertEqual(a7f.mbid, '24e1b53c-3085-4581-8472-0b0088d2508c')
def downloadBackground(self): #Download background from fanart.tv try: self.getArtistId() #Start with fanart.tv artist = Artist.get(id=self.artistId) background = artist.backgrounds[0] with open(self.back_filepath, 'wb') as fp: fp.write(background.content()) self.currentart = pygame.transform.scale(pygame.image.load(self.back_filepath),(self.surface.get_width(),self.surface.get_height())) self.loadBackground() except: print "Error Downloading background"
def downloadBackground(self): #Download background from fanart.tv try: self.getArtistId() #Start with fanart.tv artist = Artist.get(id=self.artistId) background = artist.backgrounds[0] with open(self.back_filepath, 'wb') as fp: fp.write(background.content()) self.currentart = pygame.transform.scale( pygame.image.load(self.back_filepath), (self.surface.get_width(), self.surface.get_height())) self.loadBackground() except: print "Error Downloading background"
def downloadDiscImage(self): #Download cover from fanart.tv self.getArtistId() self.getAlbumId() target_album = None #Find album in the artist discs artist = Artist.get(id=self.artistId) for album in artist.albums: if album.mbid == self.albumId: target_album = album if target_album != None and len(target_album.covers) > 0: disc_image = target_album.covers[0] with open(self.disc_filepath, 'wb') as fp: fp.write(disc_image.content()) self.loadDiscImage()
def downloadDiscImage(self): #Download cover from fanart.tv self.getArtistId() self.getAlbumId() target_album = None #Find album in the artist discs artist = Artist.get(id=self.artistId) for album in artist.albums: if album.mbid == self.albumId: target_album = album if target_album != None and len(target_album.covers)>0: disc_image = target_album.covers[0] with open(self.disc_filepath, 'wb') as fp: fp.write(disc_image.content()) self.loadDiscImage()
def _update_cache(self): """ Since we call the same url for both info and artwork, we'll update both at the same time """ myDB = db.DBConnection() if self.id_type == 'artist': try: data = Artist.get(id=self.id) except Exception as e: dbartist = myDB.action( 'SELECT ArtistName, Type FROM artists WHERE ArtistID=?', [self.id]).fetchone()[0] if dbartist: logger.debug('Fanart.tv artist lookup error for %s: %s', dbartist, e) logger.debug('Stored id for %s is: %s', dbartist, self.id) else: logger.debug('Fanart.tv artist lookup error for %s: %s', self.id, e) return artist_url = None thumb_url = None image_url = None if data.thumbs: for thumbs in data.thumbs[0:1]: artist_url = str(thumbs.url) if artist_url: thumb_url = artist_url.replace('fanart/', 'preview/') image_url = thumb_url logger.debug('Fanart.tv artist image url: %s', thumb_url) else: logger.debug('Fanart.tv no artist image found for: %s', self.id) data = lastfm.request_lastfm("artist.getinfo", mbid=self.id, api_key=LASTFM_API_KEY) # Try with name if not found if not data: dbartist = myDB.action( 'SELECT ArtistName, Type FROM artists WHERE ArtistID=?', [self.id]).fetchone()[0] if dbartist: data = lastfm.request_lastfm( "artist.getinfo", artist=helpers.clean_musicbrainz_name( dbartist['ArtistName']), api_key=LASTFM_API_KEY) if not data: logger.debug('Last.fm connection cannot be made') try: self.info_summary = data['artist']['bio']['summary'] except KeyError: logger.debug('No artist bio summary found') self.info_summary = None try: self.info_content = data['artist']['bio']['content'] except KeyError: logger.debug('No artist bio found') self.info_content = None else: # get ArtistID from AlbumID lookup - ArtistID not passed into this function otherwise myDB = db.DBConnection() ArtistID = myDB.action( 'SELECT ArtistID FROM albums WHERE ReleaseID=?', [self.id]).fetchone()[0] try: data = Artist.get(id=ArtistID) except Exception as e: dbartist = myDB.action( 'SELECT ArtistName, Type FROM artists WHERE ArtistID=?', [ArtistID]).fetchone()[0] if dbartist: logger.debug('Fanart.tv artist lookup error for %s: %s', dbartist, e) logger.debug('Stored id for %s is: %s', dbartist, ArtistID) else: logger.debug('Fanart.tv artist lookup error for %s: %s', ArtistID, e) return album_url = None thumb_url = None image_url = None if data.albums: for x in data.albums: if x.mbid == self.id: album_url = str(x.covers[0]) if album_url: thumb_url = album_url.replace('fanart/', 'preview/') image_url = thumb_url logger.debug('Fanart.tv album url: %s', thumb_url) else: logger.debug('Fanart.tv no album image found for: %s', self.id) # Save the image URL to the database if image_url: if self.id_type == 'artist': myDB.action('UPDATE artists SET ArtworkURL=? WHERE ArtistID=?', [image_url, self.id]) else: myDB.action('UPDATE albums SET ArtworkURL=? WHERE AlbumID=?', [image_url, self.id]) # Save the thumb URL to the database if thumb_url: if self.id_type == 'artist': myDB.action('UPDATE artists SET ThumbURL=? WHERE ArtistID=?', [thumb_url, self.id]) else: myDB.action('UPDATE albums SET ThumbURL=? WHERE AlbumID=?', [thumb_url, self.id]) # Should we grab the artwork here if we're just grabbing thumbs or # info? Probably not since the files can be quite big # With fanart.tv only one url is used for both thumb_url and image_url - so only making one request # If seperate ones are desired in the future, the artwork vars below will need to be uncommented if image_url is not None: artwork = request.request_content(image_url, timeout=20) if image_url and self.query_type == 'artwork': # artwork = request.request_content(image_url, timeout=20) if artwork: # Make sure the artwork dir exists: if not os.path.isdir(self.path_to_art_cache): try: os.makedirs(self.path_to_art_cache) os.chmod(self.path_to_art_cache, int(headphones.CONFIG.FOLDER_PERMISSIONS, 8)) except OSError as e: logger.error( 'Unable to create artwork cache dir. Error: %s', e) self.artwork_errors = True self.artwork_url = image_url # Delete the old stuff for artwork_file in self.artwork_files: try: os.remove(artwork_file) except: logger.error('Error deleting file from the cache: %s', artwork_file) ext = os.path.splitext(image_url)[1] artwork_path = os.path.join( self.path_to_art_cache, self.id + '.' + helpers.today() + ext) try: with open(artwork_path, 'wb') as f: f.write(artwork) os.chmod(artwork_path, int(headphones.CONFIG.FILE_PERMISSIONS, 8)) except (OSError, IOError) as e: logger.error('Unable to write to the cache dir: %s', e) self.artwork_errors = True self.artwork_url = image_url # Grab the thumbnail as well if we're getting the full artwork (as long # as it's missing/outdated. if thumb_url and self.query_type in [ 'thumb', 'artwork' ] and not (self.thumb_files and self._is_current(self.thumb_files[0])): # artwork = request.request_content(thumb_url, timeout=20) if artwork: # Make sure the artwork dir exists: if not os.path.isdir(self.path_to_art_cache): try: os.makedirs(self.path_to_art_cache) os.chmod(self.path_to_art_cache, int(headphones.CONFIG.FOLDER_PERMISSIONS, 8)) except OSError as e: logger.error( 'Unable to create artwork cache dir. Error: %s' + e) self.thumb_errors = True self.thumb_url = thumb_url # Delete the old stuff for thumb_file in self.thumb_files: try: os.remove(thumb_file) except OSError as e: logger.error('Error deleting file from the cache: %s', thumb_file) ext = os.path.splitext(image_url)[1] thumb_path = os.path.join( self.path_to_art_cache, 'T_' + self.id + '.' + helpers.today() + ext) try: with open(thumb_path, 'wb') as f: f.write(artwork) os.chmod(thumb_path, int(headphones.CONFIG.FILE_PERMISSIONS, 8)) except (OSError, IOError) as e: logger.error('Unable to write to the cache dir: %s', e) self.thumb_errors = True self.thumb_url = image_url dbalbum = myDB.action( 'SELECT ArtistName, AlbumTitle, ReleaseID, Type FROM albums WHERE AlbumID=?', [self.id]).fetchone() if dbalbum: if dbalbum['ReleaseID'] != self.id: data = lastfm.request_lastfm("album.getinfo", mbid=dbalbum['ReleaseID'], api_key=LASTFM_API_KEY) if not data: data = lastfm.request_lastfm( "album.getinfo", artist=helpers.clean_musicbrainz_name( dbalbum['ArtistName']), album=helpers.clean_musicbrainz_name( dbalbum['AlbumTitle']), api_key=LASTFM_API_KEY) else: if dbalbum['Type'] != "part of": data = lastfm.request_lastfm( "album.getinfo", artist=helpers.clean_musicbrainz_name( dbalbum['ArtistName']), album=helpers.clean_musicbrainz_name( dbalbum['AlbumTitle']), api_key=LASTFM_API_KEY) else: # Series, use actual artist for the release-group artist = mb.getArtistForReleaseGroup(self.id) if artist: data = lastfm.request_lastfm( "album.getinfo", artist=helpers.clean_musicbrainz_name(artist), album=helpers.clean_musicbrainz_name( dbalbum['AlbumTitle']), api_key=LASTFM_API_KEY) if not data: logger.debug('Last.fm connection cannot be made') try: self.info_summary = data['album']['wiki']['summary'] except KeyError: logger.debug('No album summary found') self.info_summary = None try: self.info_content = data['album']['wiki']['content'] except KeyError: logger.debug('No album infomation found') self.info_content = None # Save the content & summary to the database no matter what if we've # opened up the url if self.id_type == 'artist': controlValueDict = {"ArtistID": self.id} else: controlValueDict = {"ReleaseGroupID": self.id} newValueDict = { "Summary": self.info_summary, "Content": self.info_content, "LastUpdated": helpers.today() } myDB.upsert("descriptions", newValueDict, controlValueDict)
def get_image_links(self, ArtistID=None, AlbumID=None): """ Here we're just going to open up the last.fm url, grab the image links and return them Won't save any image urls, or save the artwork in the cache. Useful for search results, etc. """ if ArtistID: self.id_type = 'artist' try: data = Artist.get(id=ArtistID) except ResponseFanartError as e: logger.debug('Fanart.tv lookup error for %s: %s', ArtistID, e) return logger.debug('Fanart.tv ArtistID: %s', ArtistID) artist_url = None thumb_url = None image_url = None if data.thumbs: for thumbs in data.thumbs[0:1]: artist_url = str(thumbs.url) if artist_url: thumb_url = artist_url.replace('fanart/', 'preview/') image_url = thumb_url logger.debug('Fanart.tv artist url: %s', thumb_url) else: logger.debug('Fanart.tv no artist image found for %s', ArtistID) else: self.id_type = 'album' try: data = Artist.get(id="ArtistID") except ResponseFanartError as e: logger.debug('Fanart.tv lookup error for %s: %s', ArtistID, e) return logger.debug('Fanart.tv AlbumID: %s', AlbumID) album_url = None thumb_url = None image_url = None if data.albums: for x in data.albums: if x.mbid == AlbumID: album_url = str(x.covers[0]) if album_url: thumb_url = album_url.replace('fanart/', 'preview/') image_url = thumb_url logger.debug('Fanart.tv album url: %s', thumb_url) else: logger.debug('Fanart.tv no album image found for %s', AlbumID) return {'artwork': image_url, 'thumbnail': thumb_url}