""" Base resource with user handling and Spotify integration """ __author__ = '@tormakris' __copyright__ = "Copyright 2020, onSpot Team" __module_name__ = "spotifyuserstoreresource" __version__text__ = "1" from flask import current_app from spotipy.oauth2 import SpotifyClientCredentials import spotipy import musicbrainzngs from resources.userstoreresource import UserStoreResource from utils.config import SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET class APIInteractionResource(UserStoreResource): spotify = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET)) def queryspotifyinfo(self, mb_type: str, title: str = "", artist: str = "", album: str = "") -> tuple: querystring = f"{artist} {album} {title}" if mb_type == "artist": spot_data = self.spotify.search(q=querystring, type="artist", limit=1)['artists']['items'] spotify_uri = "spotify:artist:" if spot_data: imagedata = spot_data[0]['images'] else: return None, None, None elif mb_type == "release": spot_data = self.spotify.search(q=querystring, type="artist", limit=1).get( 'albums') if spot_data: spot_data = spot_data['items'] else: return None, None, None spotify_uri = "spotify:album:" if spot_data: imagedata = spot_data[0]['images'] else: return None, None, None elif mb_type == "work" or mb_type == "recording": spot_data = self.spotify.search(q=querystring, type="track", limit=1)['tracks']['items'] spotify_uri = "spotify:track:" if spot_data: imagedata = spot_data[0]['album']['images'] else: return None, None, None else: return None, None, None spotify_uri += spot_data[0]['id'] if imagedata: return spotify_uri, imagedata[0]['url'], imagedata[len(imagedata) - 1]['url'] else: return spotify_uri, None, None 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']) return retdata def getartistinfo(self, itemid: str) -> dict: currartist = musicbrainzngs.get_artist_by_id(itemid)['artist'] retdata = {"id": itemid, "artist": currartist['name'], "type": "artist"} retdata['spotify_id'], retdata['cover_url'], retdata['cover_url_small'] = \ self.queryspotifyinfo('artist', artist=retdata['artist']) return retdata def getworkinfo(self, itemid: str) -> dict: currwork = musicbrainzngs.get_work_by_id(itemid)['work'] retdata = {"id": itemid, "title": currwork['title'], "type": "work"} retdata['spotify_id'], retdata['cover_url'], retdata['cover_url_small'] = \ self.queryspotifyinfo('work', title=retdata['title']) return retdata def getrecordinginfo(self, itemid: str) -> dict: currrecording = musicbrainzngs.get_recording_by_id(itemid, includes=['artists', 'releases'])['recording'] retdata = {"id": itemid, "title": currrecording['title'], "type": "recording"} if 'artist-credit' in currrecording and currrecording['artist-credit']: currartist = currrecording['artist-credit'][0]['artist'] retdata['artist'] = currartist['name'] 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) return retdata