def api_albumart_set(self, directory, imageurl): if not cherrypy.session['admin']: raise cherrypy.HTTPError(401, 'Unauthorized') b64imgpath = albumArtFilePath(directory) fetcher = albumartfetcher.AlbumArtFetcher() data, header = fetcher.retrieveData(imageurl) self.albumartcache_save(b64imgpath, data)
def api_fetchalbumart(self, value): cherrypy.session.release_lock() params = json.loads(value) directory = params['directory'] #try getting a cached album art image b64imgpath = albumArtFilePath(directory) img_data = self.albumartcache_load(b64imgpath) if img_data: cherrypy.response.headers["Content-Length"] = len(img_data) return img_data #try getting album art inside local folder fetcher = albumartfetcher.AlbumArtFetcher() localpath = os.path.join(cherry.config['media.basedir'], directory) header, data, resized = fetcher.fetchLocal(localpath) if header: if resized: #cache resized image for next time self.albumartcache_save(b64imgpath, data) cherrypy.response.headers.update(header) return data elif cherry.config['media.fetch_album_art']: #fetch album art from online source album = os.path.basename(directory) artist = os.path.basename(os.path.dirname(directory)) keywords = artist + ' ' + album log.i("Fetching album art for keywords '%s'" % keywords) header, data = fetcher.fetch(keywords) if header: cherrypy.response.headers.update(header) self.albumartcache_save(b64imgpath, data) return data cherrypy.HTTPRedirect("/res/img/folder.png", 302)
def api_fetchalbumarturls(self, searchterm): if not cherrypy.session['admin']: raise cherrypy.HTTPError(401, 'Unauthorized') _save_and_release_session() fetcher = albumartfetcher.AlbumArtFetcher() imgurls = fetcher.fetchurls(searchterm) # show no more than 10 images return imgurls[:min(len(imgurls), 10)]
def api_fetchalbumarturls(self, searchterm, method=None): if not cherrypy.session['admin']: raise cherrypy.HTTPError(401, 'Unauthorized') _save_and_release_session() fetch_args = {} if method is None else {'method': method} fetcher = albumartfetcher.AlbumArtFetcher(**fetch_args) imgurls = fetcher.fetchurls(searchterm) # show no more than 10 images return imgurls[:min(len(imgurls), 10)]
def api_fetchalbumart(self, directory): cherrypy.session.release_lock() default_folder_image = "/res/img/folder.png" #try getting a cached album art image b64imgpath = albumArtFilePath(directory) img_data = self.albumartcache_load(b64imgpath) if img_data: cherrypy.response.headers["Content-Length"] = len(img_data) return img_data #try getting album art inside local folder fetcher = albumartfetcher.AlbumArtFetcher() localpath = os.path.join(cherry.config['media.basedir'], directory) header, data, resized = fetcher.fetchLocal(localpath) if header: if resized: #cache resized image for next time self.albumartcache_save(b64imgpath, data) cherrypy.response.headers.update(header) return data elif cherry.config['media.fetch_album_art']: #fetch album art from online source try: foldername = os.path.basename(directory) keywords = foldername log.i( _("Fetching album art for keywords {keywords!r}").format( keywords=keywords)) header, data = fetcher.fetch(keywords) if header: cherrypy.response.headers.update(header) self.albumartcache_save(b64imgpath, data) return data else: # albumart fetcher failed, so we serve a standard image raise cherrypy.HTTPRedirect(default_folder_image, 302) except: # albumart fetcher threw exception, so we serve a standard image raise cherrypy.HTTPRedirect(default_folder_image, 302) else: # no local album art found, online fetching deactivated, show default raise cherrypy.HTTPRedirect(default_folder_image, 302)
def test_fetchLocal_id3(): """Album art can be fetched with tinytag""" # PNG image data, 1 x 1, 1-bit grayscale, non-interlaced _PNG_IMG_DATA = unhexlify(b''.join(b""" 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 0000 0001 0000 0001 0100 0000 0037 6ef9 2400 0000 1049 4441 5478 9c62 6001 0000 00ff ff03 0000 0600 0557 bfab d400 0000 0049 454e 44ae 4260 82""".split())) fetcher = albumartfetcher.AlbumArtFetcher() with patch('cherrymusicserver.albumartfetcher.TinyTag') as TinyTagMock: TinyTagMock.get().get_image.return_value = _PNG_IMG_DATA with helpers.tempdir('test_albumartfetcher') as tmpd: artpath = helpers.mkpath('test.mp3', parent=tmpd) fetcher.fetchLocal(tmpd) TinyTagMock.get.assert_called_with(artpath, image=True) assert TinyTagMock.get().get_image.called
def api_fetchalbumart(self, directory): _save_and_release_session() default_folder_image = "../res/img/folder.png" log.i('Fetching album art for: %s' % directory) filepath = os.path.join(cherry.config['media.basedir'], directory) if os.path.isfile(filepath): # if the given path is a file, try to get the image from ID3 tag = TinyTag.get(filepath, image=True) image_data = tag.get_image() if image_data: log.d('Image found in tag.') header = {'Content-Type': 'image/jpg', 'Content-Length': len(image_data)} cherrypy.response.headers.update(header) return image_data else: # if the file does not contain an image, display the image of the # parent directory directory = os.path.dirname(directory) #try getting a cached album art image b64imgpath = albumArtFilePath(directory) img_data = self.albumartcache_load(b64imgpath) if img_data: cherrypy.response.headers["Content-Length"] = len(img_data) return img_data #try getting album art inside local folder fetcher = albumartfetcher.AlbumArtFetcher() localpath = os.path.join(cherry.config['media.basedir'], directory) header, data, resized = fetcher.fetchLocal(localpath) if header: if resized: #cache resized image for next time self.albumartcache_save(b64imgpath, data) cherrypy.response.headers.update(header) return data elif cherry.config['media.fetch_album_art']: # maximum of files to try to fetch metadata for albumart keywords METADATA_ALBUMART_MAX_FILES = 10 #fetch album art from online source try: foldername = os.path.basename(directory) keywords = foldername # remove any odd characters from the folder name keywords = re.sub('[^A-Za-z\s]', ' ', keywords) # try getting metadata from files in the folder for a more # accurate match files = os.listdir(localpath) for i, filename in enumerate(files): if i >= METADATA_ALBUMART_MAX_FILES: break path = os.path.join(localpath, filename) metadata = metainfo.getSongInfo(path) if metadata.artist and metadata.album: keywords = '{} - {}'.format(metadata.artist, metadata.album) break log.i(_("Fetching album art for keywords {keywords!r}").format(keywords=keywords)) header, data = fetcher.fetch(keywords) if header: cherrypy.response.headers.update(header) self.albumartcache_save(b64imgpath, data) return data else: # albumart fetcher failed, so we serve a standard image raise cherrypy.HTTPRedirect(default_folder_image, 302) except: # albumart fetcher threw exception, so we serve a standard image raise cherrypy.HTTPRedirect(default_folder_image, 302) else: # no local album art found, online fetching deactivated, show default raise cherrypy.HTTPRedirect(default_folder_image, 302)
def try_method(method, timeout=5): fetcher = albumartfetcher.AlbumArtFetcher(method=method, timeout=timeout) results = fetcher.fetchurls('best of') ok_(results, "method {0!r} results: {1}".format(method, results))
def api_fetchalbumart(self, directory): _save_and_release_session() default_folder_image = "../res/img/folder.png" log.i('Fetching album art for: %s' % directory) filepath = os.path.join(cherry.config['media.basedir'], directory) if os.path.isfile(filepath): # if the given path is a file, try to get the image from ID3 tag = TinyTag.get(filepath, image=True) image_data = tag.get_image() if image_data: log.d('Image found in tag.') header = { 'Content-Type': 'image/jpg', 'Content-Length': len(image_data) } cherrypy.response.headers.update(header) return image_data else: # if the file does not contain an image, display the image of the # parent directory directory = os.path.dirname(directory) #try getting a cached album art image b64imgpath = albumArtFilePath(directory) img_data = self.albumartcache_load(b64imgpath) if img_data: cherrypy.response.headers["Content-Length"] = len(img_data) return img_data #try getting album art inside local folder fetcher = albumartfetcher.AlbumArtFetcher() localpath = os.path.join(cherry.config['media.basedir'], directory) header, data, resized = fetcher.fetchLocal(localpath) if header: if resized: #cache resized image for next time self.albumartcache_save(b64imgpath, data) cherrypy.response.headers.update(header) return data elif cherry.config['media.fetch_album_art']: #fetch album art from online source try: foldername = os.path.basename(directory) keywords = foldername log.i( _("Fetching album art for keywords {keywords!r}").format( keywords=keywords)) header, data = fetcher.fetch(keywords) if header: cherrypy.response.headers.update(header) self.albumartcache_save(b64imgpath, data) return data else: # albumart fetcher failed, so we serve a standard image raise cherrypy.HTTPRedirect(default_folder_image, 302) except: # albumart fetcher threw exception, so we serve a standard image raise cherrypy.HTTPRedirect(default_folder_image, 302) else: # no local album art found, online fetching deactivated, show default raise cherrypy.HTTPRedirect(default_folder_image, 302)
def try_method(method, timeout=15): fetcher = albumartfetcher.AlbumArtFetcher(method=method, timeout=timeout) results = fetcher.fetchurls('best of') results += fetcher.fetchurls('best of') # once is not enough sometimes (?) ok_(results, "method {0!r} results: {1}".format(method, results))