This commit is contained in:
@ -11,10 +11,10 @@ import musicbrainzngs
|
||||
from flask import request, current_app, abort
|
||||
|
||||
from flaskaddons.fred import flaskred
|
||||
from resources.spotifyuserstoreresource import SpotifyUserStoreResource
|
||||
from resources.apiinteractionresource import APIInteractionResource
|
||||
|
||||
|
||||
class SingleListApi(SpotifyUserStoreResource):
|
||||
class SingleListApi(APIInteractionResource):
|
||||
"""
|
||||
See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getList
|
||||
"""
|
||||
@ -23,7 +23,7 @@ class SingleListApi(SpotifyUserStoreResource):
|
||||
try:
|
||||
currcreds = self.encryptor.load(flaskred.get(request.headers.get('Authorization')).decode('UTF-8'))
|
||||
except Exception as e:
|
||||
current_app.logger.warning(e)
|
||||
current_app.logger.info(e)
|
||||
abort(401, "unauthorized")
|
||||
try:
|
||||
list_type = flaskred.get(listid).decode('UTF-8')
|
||||
@ -35,90 +35,30 @@ class SingleListApi(SpotifyUserStoreResource):
|
||||
offset = int(request.args.get('offset', 0))
|
||||
if list_type == 'release':
|
||||
currdata = musicbrainzngs.get_releases_in_collection(listid, limit, offset)['collection']
|
||||
releaselist = []
|
||||
itemlist = []
|
||||
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).get(
|
||||
'albums')
|
||||
if album_spot:
|
||||
album_spot = album_spot['items']
|
||||
if album_spot:
|
||||
releasedata['spotify_id'] = f"spotify:album:{album_spot[0]['id']}"
|
||||
try:
|
||||
imgurl = musicbrainzngs.get_image_list(release['id'])['images']
|
||||
if imgurl:
|
||||
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)
|
||||
itemlist.append(self.getreleaseinfo(release['id']))
|
||||
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 = []
|
||||
itemlist = []
|
||||
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 artist_spot:
|
||||
artist_data['spotify_id'] = f"spotify:artist:{artist_spot[0]['id']}"
|
||||
artist_image = artist_spot[0]['images']
|
||||
if artist_image:
|
||||
artist_data['cover_url'] = artist_image[0]['url']
|
||||
artist_data['cover_url_small'] = artist_image[len(artist_image) - 1]['url']
|
||||
artistlist.append(artist_data)
|
||||
itemlist.append(self.getartistinfo(artist['id']))
|
||||
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 = []
|
||||
itemlist = []
|
||||
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 work_spot:
|
||||
workdata['spotify_id'] = f"spotify:track:{work_spot[0]['id']}"
|
||||
work_image = work_spot[0]['album']['images']
|
||||
if work_image:
|
||||
workdata['cover_url'] = work_image[0]['url']
|
||||
workdata['cover_url_small'] = work_image[len(work_image) - 1]['url']
|
||||
worklist.append(workdata)
|
||||
itemlist.append(self.getworkinfo(work['id']))
|
||||
flaskred.set(work['id'], 'work'.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 = []
|
||||
itemlist = []
|
||||
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 imgurl:
|
||||
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 recording_spot:
|
||||
currrec['spotify_id'] = f"spotify:track:{recording_spot[0]['id']}"
|
||||
recordinglist.append(currrec)
|
||||
itemlist.append(self.getrecordinginfo(recording['id']))
|
||||
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
|
||||
return {"id": currdata['id'], "element_count": currdata[f'{list_type}-count'], "itemlist": itemlist,
|
||||
'type': list_type, 'name': currdata['name']}, 200
|
||||
|
Reference in New Issue
Block a user