68 lines
2.9 KiB
Python
68 lines
2.9 KiB
Python
"""
|
|
Endpoinds manipulating single lists
|
|
"""
|
|
|
|
__author__ = '@tormakris'
|
|
__copyright__ = "Copyright 2020, onSpot Team"
|
|
__module_name__ = "singlelistapi"
|
|
__version__text__ = "1"
|
|
|
|
from datetime import timedelta
|
|
|
|
import musicbrainzngs
|
|
from flask import request, current_app, abort
|
|
|
|
from flaskaddons.fred import flaskred
|
|
from resources.apiinteractionresource import APIInteractionResource
|
|
|
|
|
|
class SingleListApi(APIInteractionResource):
|
|
"""
|
|
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.info(e)
|
|
abort(401, "unauthorized")
|
|
flaskred.expire(request.headers.get('Authorization'), timedelta(minutes=15))
|
|
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']
|
|
itemlist = []
|
|
for release in currdata['release-list']:
|
|
itemlist.append(self.getreleaseinfo(release['id']))
|
|
flaskred.set(release['id'], 'release'.encode('UTF-8'))
|
|
elif list_type == 'artist':
|
|
currdata = musicbrainzngs.get_artists_in_collection(listid, limit, offset)['collection']
|
|
itemlist = []
|
|
for artist in currdata['artist-list']:
|
|
itemlist.append(self.getartistinfo(artist['id']))
|
|
flaskred.set(artist['id'], 'artist'.encode('UTF-8'))
|
|
elif list_type == 'work':
|
|
currdata = musicbrainzngs.get_works_in_collection(listid, limit, offset)['collection']
|
|
itemlist = []
|
|
for work in currdata['work-list']:
|
|
itemlist.append(self.getworkinfo(work['id']))
|
|
flaskred.set(work['id'], 'work'.encode('UTF-8'))
|
|
elif list_type == 'recording':
|
|
currdata = musicbrainzngs.get_recordings_in_collection(listid, limit, offset)['collection']
|
|
itemlist = []
|
|
for recording in currdata['recording-list']:
|
|
itemlist.append(self.getrecordinginfo(recording['id']))
|
|
flaskred.set(recording['id'], 'recording'.encode('UTF-8'))
|
|
else:
|
|
abort(417, "wrong type of collection")
|
|
musicbrainzngs.auth(None, None)
|
|
return {"id": currdata['id'], "element_count": currdata[f'{list_type}-count'], "itemlist": itemlist,
|
|
'type': list_type, 'name': currdata['name']}, 200
|