From 183ffadad62a9faec081a988ad472a88452a06ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Fri, 27 Nov 2020 00:28:31 +0100 Subject: [PATCH] even more generic --- src/resources/apiinteractionresource.py | 41 ++++++++++++++++--------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/resources/apiinteractionresource.py b/src/resources/apiinteractionresource.py index dcf9ef0..849ee7b 100644 --- a/src/resources/apiinteractionresource.py +++ b/src/resources/apiinteractionresource.py @@ -51,13 +51,29 @@ class APIInteractionResource(UserStoreResource): else: return spotify_uri, None, None + def getcoverimage(self, releaseid: str, itemtype: str, title: str, artist: str, + album: str = "") -> tuple: + if itemtype != "recording" and itemtype != "release": + return None, None + try: + musicbimgurl = musicbrainzngs.get_image_list(releaseid)['images'] + imgurl = musicbimgurl[0]['thumbnails']['large'] + smallimgurl = musicbimgurl[0]['thumbnails']['small'] + except Exception as e: + _, imgurl, smallimgurl = self.queryspotifyinfo(itemtype, title=title, album=album, artist=artist) + current_app.logger.info(e) + return imgurl, smallimgurl + def getreleaseinfo(self, itemid: str) -> dict: currrelease = musicbrainzngs.get_release_by_id(itemid, includes=['artists'])['release'] retdata = {"id": itemid, "album": currrelease['title'], "type": "release"} if 'artist-credit' in currrelease and currrelease['artist-credit']: retdata['artist'] = currrelease['artist-credit'][0]['artist']['name'] - retdata['spotify_id'], retdata['cover_url'], retdata['cover_url_small'] = \ - self.queryspotifyinfo('release', artist=retdata['artist'], album=retdata['album']) + retdata['cover_url'], retdata['cover_url_small'] = self.getcoverimage( + itemid, 'release', title=retdata['title'], album=retdata.get('album'), + artist=retdata.get('artist')) + retdata['spotify_id'], _, _ = \ + self.queryspotifyinfo('release', title=retdata['title']) return retdata def getartistinfo(self, itemid: str) -> dict: @@ -83,17 +99,12 @@ class APIInteractionResource(UserStoreResource): if 'release-list' in currrecording and currrecording['release-list']: currrlease = currrecording['release-list'][0] retdata['album'] = currrlease['title'] - try: - imgurl = musicbrainzngs.get_image_list(currrlease['id'])['images'] - if imgurl: - retdata['cover_url_small'] = imgurl[0]['thumbnails']['small'] - retdata['cover_url'] = imgurl[0]['thumbnails']['large'] - retdata['spotify_id'], _, _ = \ - self.queryspotifyinfo('recording', title=retdata['title'], album=retdata.get('album'), - artist=retdata.get('artist')) - except Exception as e: - retdata['spotify_id'], retdata['cover_url'], retdata['cover_url_small'] = \ - self.queryspotifyinfo('recording', title=retdata['title'], album=retdata.get('album'), - artist=retdata.get('artist')) - current_app.logger.info(e) + imageid = currrlease['id'] + else: + imageid = itemid + retdata['cover_url'], retdata['cover_url_small'] = self.getcoverimage( + imageid, 'release', title=retdata['title'], + artist=retdata.get('artist')) + retdata['spotify_id'], _, _ = \ + self.queryspotifyinfo('recording', title=retdata['title']) return retdata