From b241245e9cb727c758ef8fa163233c2dde178129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Thu, 26 Nov 2020 06:15:05 +0100 Subject: [PATCH] scouring the api finished --- src/app.py | 2 +- src/resources.py | 52 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/app.py b/src/app.py index 8d92eda..51dfc58 100644 --- a/src/app.py +++ b/src/app.py @@ -68,7 +68,7 @@ api.add_resource(LoginApi, '/api/auth/login') api.add_resource(MeApi, '/api/auth/me') api.add_resource(ListsApi, '/api/lists') api.add_resource(SingleListApi, '/api/lists/') -api.add_resource(TrackApi, '/api/lists//') +api.add_resource(TrackApi, '/api/items/') app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run()) diff --git a/src/resources.py b/src/resources.py index 8286d16..57b27a4 100644 --- a/src/resources.py +++ b/src/resources.py @@ -145,9 +145,9 @@ class SingleListApi(Resource): releaselist = [] for release in currdata['release-list']: releasedata = {"id": release['id'], "album": release['title']} - currlease = musicbrainzngs.get_release_by_id(release['id'], includes=['artists']) - if 'artist-credit' in currlease['release'] and currlease['release']['artist-credit']: - currartist = currlease['release']['artist-credit'][0]['artist'] + 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'] releaselist.append(releasedata) flaskred.set(release['id'], 'release'.encode('UTF-8')) @@ -191,8 +191,48 @@ class SingleListApi(Resource): class TrackApi(Resource): """ - See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getTrack + See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getItem """ + encryptor = EncryptedUserRedis(ENCODED_SECRET_KEY) - def get(self, listid: str, trackid: str): - pass + def get(self, itemid: 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: + item_type = flaskred.get(itemid).decode('UTF-8') + except Exception as e: + current_app.logger.warning(e) + abort(404, "unknown list") + musicbrainzngs.auth(currcreds['name'], currcreds['password']) + + if item_type == 'release': + currrelease = musicbrainzngs.get_release_by_id(itemid, includes=['artists'])['release'] + print(currrelease) + retdata = {"id": itemid, "album": currrelease['title']} + if 'artist-credit' in currrelease and currrelease['artist-credit']: + retdata['artist'] = currrelease['artist-credit'][0]['artist']['name'] + elif item_type == 'artist': + currartist = musicbrainzngs.get_artist_by_id(itemid)['artist'] + retdata = {"id": itemid, "artist": currartist['name']} + elif item_type == 'work': + currwork = musicbrainzngs.get_work_by_id(itemid)['work'] + retdata = {"id": itemid, "title": currwork['title']} + elif item_type == 'recording': + currrecording = musicbrainzngs.get_recording_by_id(itemid, includes=['artists', 'releases'])['recording'] + retdata = {"id": itemid, "title": currrecording['title']} + 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'] + else: + abort(417, "wrong type of item") + + musicbrainzngs.auth(None, None) + + retdata['type'] = item_type + return retdata, 200