From 3efaa34b9403715774978e73dc2b7043a8b170fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Thu, 26 Nov 2020 21:13:37 +0100 Subject: [PATCH] fix spotify stuff --- src/resources/itemapi.py | 11 +++++++---- src/resources/singlelistapi.py | 14 ++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/resources/itemapi.py b/src/resources/itemapi.py index 82ba22b..a4b8f7c 100644 --- a/src/resources/itemapi.py +++ b/src/resources/itemapi.py @@ -37,9 +37,12 @@ class ItemApi(SpotifyUserStoreResource): if 'artist-credit' in currrelease['release'] and currrelease['release']['artist-credit']: retdata['artist'] = currrelease['artist-credit'][0]['artist']['name'] album_spot = \ - self.spotify.search(q=f"{retdata.get('artist', '')} {retdata['title']}", limit=1)[ - 'albums'][ - 'items'] + self.spotify.search(q=f"{retdata.get('artist', '')} {retdata['title']}", limit=1).get( + 'albums') + if album_spot: + album_spot = album_spot['items'] + if len(album_spot) > 0: + retdata['spotify_id'] = f"spotify:album:{album_spot[0]['id']}" try: imgurl = musicbrainzngs.get_image_list(currrelease['id'])['images'] if len(imgurl) > 0: @@ -65,7 +68,7 @@ class ItemApi(SpotifyUserStoreResource): retdata = {"id": itemid, "title": currwork['title']} if len(work_spot) > 0: retdata['spotify_id'] = f"spotify:track:{work_spot[0]['id']}" - work_image = work_spot[0]['images'] + work_image = work_spot[0]['album']['images'] if len(work_image) > 0: retdata['cover_url'] = work_image[0]['url'] retdata['cover_url_small'] = work_image[len(work_image) - 1]['url'] diff --git a/src/resources/singlelistapi.py b/src/resources/singlelistapi.py index 7a11001..bbc9bc9 100644 --- a/src/resources/singlelistapi.py +++ b/src/resources/singlelistapi.py @@ -43,10 +43,12 @@ class SingleListApi(SpotifyUserStoreResource): currartist = currrelease['release']['artist-credit'][0]['artist'] releasedata['artist'] = currartist['name'] album_spot = \ - self.spotify.search(q=f"{releasedata.get('artist', '')} {release['title']}", limit=1)[ - 'albums']['items'] - if len(album_spot) > 0: - releasedata['spotify_id'] = f"spotify:album:{album_spot[0]['id']}" + self.spotify.search(q=f"{releasedata.get('artist', '')} {release['title']}", limit=1).get( + 'albums') + if album_spot: + album_spot = album_spot['items'] + if len(album_spot) > 0: + releasedata['spotify_id'] = f"spotify:album:{album_spot[0]['id']}" try: imgurl = musicbrainzngs.get_image_list(release['id'])['images'] if len(imgurl) > 0: @@ -70,7 +72,7 @@ class SingleListApi(SpotifyUserStoreResource): artist_data['cover_url'] = artist_image[0]['url'] artist_data['cover_url_small'] = artist_image[len(artist_image) - 1]['url'] artistlist.append(artist_data) - flaskred.set(artist['id'], 'artist'.encode('UTF-8')) + flaskred.set(artist['id'], 'artist'.encode('UTF-8')) retdata = {"id": currdata['id'], "element_count": currdata['artist-count'], "itemlist": artistlist} elif list_type == 'work': currdata = musicbrainzngs.get_works_in_collection(listid, limit, offset)['collection'] @@ -80,7 +82,7 @@ class SingleListApi(SpotifyUserStoreResource): work_spot = self.spotify.search(q=work['title'], type="track", limit=1)['tracks']['items'] if len(work_spot) > 0: workdata['spotify_id'] = f"spotify:track:{work_spot[0]['id']}" - work_image = work_spot[0]['images'] + work_image = work_spot[0]['album']['images'] if len(work_image) > 0: workdata['cover_url'] = work_image[0]['url'] workdata['cover_url_small'] = work_image[len(work_image) - 1]['url']