backend/src/resources/singlelistapi.py

123 lines
6.8 KiB
Python

"""
Endpoinds manipulating single lists
"""
__author__ = '@tormakris'
__copyright__ = "Copyright 2020, onSpot Team"
__module_name__ = "singlelistapi"
__version__text__ = "1"
import musicbrainzngs
from flask import request, current_app, abort
from flaskaddons.fred import flaskred
from resources.spotifyuserstoreresource import SpotifyUserStoreResource
class SingleListApi(SpotifyUserStoreResource):
"""
See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getList
"""
def get(self, listid: str):
try:
currcreds = self.encryptor.load(flaskred.get(request.headers.get('Authorization')).decode('UTF-8'))
except Exception as e:
current_app.logger.warning(e)
abort(401, "unauthorized")
try:
list_type = flaskred.get(listid).decode('UTF-8')
except Exception as e:
current_app.logger.warning(e)
abort(404, "unknown list")
musicbrainzngs.auth(currcreds['name'], currcreds['password'])
limit = int(request.args.get('limit', 10))
offset = int(request.args.get('offset', 0))
if list_type == 'release':
currdata = musicbrainzngs.get_releases_in_collection(listid, limit, offset)['collection']
releaselist = []
for release in currdata['release-list']:
releasedata = {"id": release['id'], "album": release['title']}
currrelease = musicbrainzngs.get_release_by_id(release['id'], includes=['artists'])
if 'artist-credit' in currrelease['release'] and currrelease['release']['artist-credit']:
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']}"
try:
imgurl = musicbrainzngs.get_image_list(release['id'])['images']
if len(imgurl) > 0:
releasedata['cover_url'] = imgurl[0]['thumbnails']['large']
releasedata['cover_url_small'] = imgurl[0]['thumbnails']['small']
except Exception as e:
current_app.logger.warning(e)
releaselist.append(releasedata)
flaskred.set(release['id'], 'release'.encode('UTF-8'))
retdata = {"id": currdata['id'], "element_count": currdata['release-count'], "itemlist": releaselist}
elif list_type == 'artist':
currdata = musicbrainzngs.get_artists_in_collection(listid, limit, offset)['collection']
artistlist = []
for artist in currdata['artist-list']:
artist_data = {"id": artist['id'], "artist": artist['name']}
artist_spot = self.spotify.search(q=artist['name'], type="artist", limit=1)['artists']['items']
if len(artist_spot) > 0:
artist_data['spotify_id'] = f"spotify:artist:{artist_spot[0]['id']}"
artist_image = artist_spot[0]['images']
if len(artist_image) > 0:
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'))
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']
worklist = []
for work in currdata['work-list']:
workdata = {"id": work['id'], "title": work['title']}
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']
if len(work_image) > 0:
workdata['cover_url'] = work_image[0]['url']
workdata['cover_url_small'] = work_image[len(work_image) - 1]['url']
worklist.append(workdata)
flaskred.set(work['id'], 'recording'.encode('UTF-8'))
retdata = {"id": currdata['id'], "element_count": currdata['work-count'], "itemlist": worklist}
elif list_type == 'recording':
currdata = musicbrainzngs.get_recordings_in_collection(listid, limit, offset)['collection']
recordinglist = []
for recording in currdata['recording-list']:
currrec = {"id": recording['id'], "title": recording['title']}
currrecording = musicbrainzngs.get_recording_by_id(recording['id'], includes=['artists', 'releases'])
if 'artist-credit' in currrecording['recording'] and currrecording['recording']['artist-credit']:
currartist = currrecording['recording']['artist-credit'][0]['artist']
currrec['artist'] = currartist['name']
if 'release-list' in currrecording['recording'] and currrecording['recording']['release-list']:
currrlease = currrecording['recording']['release-list'][0]
currrec['album'] = currrlease['title']
try:
imgurl = musicbrainzngs.get_image_list(currrlease['id'])['images']
if len(imgurl) > 0:
currrec['cover_url_small'] = imgurl[0]['thumbnails']['small']
currrec['cover_url'] = imgurl[0]['thumbnails']['large']
except Exception as e:
current_app.logger.warning(e)
recording_spot = \
self.spotify.search(q=f"{currrec.get('artist', '')} {currrec.get('album', '')} {currrec['title']}",
limit=1)['tracks']['items']
if len(recording_spot) > 0:
currrec['spotify_id'] = f"spotify:track:{recording_spot[0]['id']}"
recordinglist.append(currrec)
flaskred.set(recording['id'], 'recording'.encode('UTF-8'))
retdata = {"id": currdata['id'], "element_count": currdata['recording-count'], "itemlist": recordinglist}
else:
abort(417, "wrong type of collection")
musicbrainzngs.auth(None, None)
retdata['type'] = list_type
retdata['name'] = currdata['name']
return retdata, 200