This commit is contained in:
88
src/resources/itemapi.py
Normal file
88
src/resources/itemapi.py
Normal file
@@ -0,0 +1,88 @@
|
||||
"""
|
||||
Endpoinds manipulating items
|
||||
"""
|
||||
|
||||
__author__ = '@tormakris'
|
||||
__copyright__ = "Copyright 2020, onSpot Team"
|
||||
__module_name__ = "itemapi"
|
||||
__version__text__ = "1"
|
||||
|
||||
import musicbrainzngs
|
||||
from flask import request, current_app, abort
|
||||
|
||||
from flaskaddons.fred import flaskred
|
||||
from resources.spotifyuserstoreresource import SpotifyUserStoreResource
|
||||
|
||||
|
||||
class ItemApi(SpotifyUserStoreResource):
|
||||
"""
|
||||
See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getItem
|
||||
"""
|
||||
|
||||
def get(self, itemid: str):
|
||||
try:
|
||||
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 item")
|
||||
|
||||
if item_type == 'release':
|
||||
currrelease = musicbrainzngs.get_release_by_id(itemid, includes=['artists'])['release']
|
||||
album_spot = self.spotify.search(q=currrelease['title'], type="album", limit=1)['albums']['items']
|
||||
retdata = {"id": itemid, "album": currrelease['title']}
|
||||
try:
|
||||
imgurl = musicbrainzngs.get_image_list(currrelease['id'])['images']
|
||||
if len(imgurl) > 0:
|
||||
retdata['cover_url'] = imgurl[0]['image']
|
||||
except Exception as e:
|
||||
current_app.logger.warning(e)
|
||||
if len(album_spot) > 0:
|
||||
retdata['spotify_id'] = f"spotify:album:{album_spot[0]['id']}"
|
||||
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']
|
||||
artist_spot = self.spotify.search(q=currartist['name'], type="artist", limit=1)['artists']['items']
|
||||
retdata = {"id": itemid, "artist": currartist['name']}
|
||||
if len(artist_spot) > 0:
|
||||
retdata['spotify_id'] = f"spotify:artist:{artist_spot[0]['id']}"
|
||||
artist_image = artist_spot[0]['images']
|
||||
if len(artist_image) > 0:
|
||||
retdata['cover_url'] = artist_image[0]['url']
|
||||
elif item_type == 'work':
|
||||
currwork = musicbrainzngs.get_work_by_id(itemid)['work']
|
||||
work_spot = self.spotify.search(q=currwork['title'], type="track", limit=1)['tracks']['items']
|
||||
retdata = {"id": itemid, "title": currwork['title']}
|
||||
if len(work_spot) > 0:
|
||||
retdata['spotify_id'] = f"spotify:track:{work_spot[0]['id']}"
|
||||
work_image = work_spot[0]['images']
|
||||
if len(work_image) > 0:
|
||||
retdata['cover_url'] = work_image[0]['url']
|
||||
elif item_type == 'recording':
|
||||
currrecording = musicbrainzngs.get_recording_by_id(itemid, includes=['artists', 'releases'])['recording']
|
||||
recording_spot = self.spotify.search(q=currrecording['title'], type="track", limit=1)['tracks']['items']
|
||||
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']
|
||||
try:
|
||||
imgurl = musicbrainzngs.get_image_list(currrlease['id'])['images']
|
||||
if len(imgurl) > 0:
|
||||
retdata['cover_url'] = imgurl[0]['image']
|
||||
except Exception as e:
|
||||
current_app.logger.warning(e)
|
||||
if len(recording_spot) > 0:
|
||||
retdata['spotify_id'] = f"spotify:track:{recording_spot[0]['id']}"
|
||||
else:
|
||||
abort(417, "wrong type of item")
|
||||
|
||||
retdata['type'] = item_type
|
||||
return retdata, 200
|
||||
Reference in New Issue
Block a user