""" Base resource with user handling, Spotify and MusicBrains integration """ __author__ = '@tormakris' __copyright__ = "Copyright 2020, onSpot Team" __module_name__ = "apiinteractionresource" __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}" spotify_uri = "spotify:" if mb_type == "artist": spot_data = self.spotify.search(q=querystring, type="artist", limit=1)['artists']['items'] if not spot_data: return None, None, None spotify_uri += "artist:" imagedata = spot_data[0]['images'] elif mb_type == "release": spot_data = self.spotify.search(q=querystring, type="artist", limit=1).get('albums') if not spot_data: return None, None, None spot_data = spot_data['items'] spotify_uri += "album:" imagedata = spot_data[0]['images'] elif mb_type == "work" or mb_type == "recording": spot_data = self.spotify.search(q=querystring, type="track", limit=1)['tracks']['items'] if not spot_data: return None, None, None spotify_uri += "track:" imagedata = spot_data[0]['album']['images'] 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 getcoverimage(self, releaseid: str, itemtype: str, artist: str, title: 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['cover_url'], retdata['cover_url_small'] = self.getcoverimage( itemid, 'release', album=retdata.get('album'), artist=retdata.get('artist')) retdata['spotify_id'], _, _ = \ self.queryspotifyinfo('release', album=currrelease['title']) 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'] 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