This commit is contained in:
parent
4a6ddc4010
commit
d0de7cb3c0
@ -23,3 +23,6 @@ RELEASEMODE = os.environ.get("ONSPOT_RELEASEMODE", "dev")
|
||||
REDIS_URL = os.getenv("ONSPOT_REDIS_URL")
|
||||
|
||||
ENCODED_SECRET_KEY = os.getenv("ONSPOT_ENCODED_SECRET_KEY")
|
||||
|
||||
SPOTIFY_CLIENT_ID = os.getenv("ONSPOT_SPOTIFY_CLIENT_ID")
|
||||
SPOTIFY_CLIENT_SECRET = os.getenv("ONSPOT_SPOTIFY_CLIENT_SECRET")
|
||||
|
@ -5,9 +5,11 @@ import uuid
|
||||
from flask_restful import Resource
|
||||
from flask import request, current_app, abort
|
||||
import musicbrainzngs
|
||||
import spotipy
|
||||
from spotipy.oauth2 import SpotifyClientCredentials
|
||||
|
||||
from fred import flaskred
|
||||
from config import ENCODED_SECRET_KEY
|
||||
from config import ENCODED_SECRET_KEY, SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET
|
||||
from schemas import UserSchema
|
||||
from aes_encrypt import EncryptedUserRedis
|
||||
|
||||
@ -23,10 +25,18 @@ __version__text__ = "1"
|
||||
INVALID_JSON_SCHEMA_MSG = "invalid json schema"
|
||||
|
||||
|
||||
class LoginApi(Resource):
|
||||
userschema = UserSchema(many=False)
|
||||
class UserStoreResource(Resource):
|
||||
encryptor = EncryptedUserRedis(ENCODED_SECRET_KEY)
|
||||
|
||||
|
||||
class SpotifyUserStoreResource(UserStoreResource):
|
||||
spotify = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=SPOTIFY_CLIENT_ID,
|
||||
client_secret=SPOTIFY_CLIENT_SECRET))
|
||||
|
||||
|
||||
class LoginApi(UserStoreResource):
|
||||
userschema = UserSchema(many=False)
|
||||
|
||||
def post(self):
|
||||
"""
|
||||
See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/logon
|
||||
@ -83,11 +93,10 @@ class MeApi(Resource):
|
||||
return {"name": currusername}, 200
|
||||
|
||||
|
||||
class ListsApi(Resource):
|
||||
class ListsApi(UserStoreResource):
|
||||
"""
|
||||
See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getAllLists
|
||||
"""
|
||||
encryptor = EncryptedUserRedis(ENCODED_SECRET_KEY)
|
||||
|
||||
def get(self):
|
||||
try:
|
||||
@ -120,11 +129,10 @@ class ListsApi(Resource):
|
||||
return returndict, 200
|
||||
|
||||
|
||||
class SingleListApi(Resource):
|
||||
class SingleListApi(SpotifyUserStoreResource):
|
||||
"""
|
||||
See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getList
|
||||
"""
|
||||
encryptor = EncryptedUserRedis(ENCODED_SECRET_KEY)
|
||||
|
||||
def get(self, listid: str):
|
||||
try:
|
||||
@ -146,6 +154,9 @@ class SingleListApi(Resource):
|
||||
for release in currdata['release-list']:
|
||||
releasedata = {"id": release['id'], "album": release['title']}
|
||||
currrelease = musicbrainzngs.get_release_by_id(release['id'], includes=['artists'])
|
||||
album_spot = self.spotify.search(q=release['title'], type="album", limit=1)['albums']['items']
|
||||
if len(album_spot) > 0:
|
||||
releasedata['spotify_id'] = f"spotify:album:{album_spot[0]['id']}"
|
||||
if 'artist-credit' in currrelease['release'] and currrelease['release']['artist-credit']:
|
||||
currartist = currrelease['release']['artist-credit'][0]['artist']
|
||||
releasedata['artist'] = currartist['name']
|
||||
@ -156,14 +167,22 @@ class SingleListApi(Resource):
|
||||
currdata = musicbrainzngs.get_artists_in_collection(listid, limit, offset)['collection']
|
||||
artistlist = []
|
||||
for artist in currdata['artist-list']:
|
||||
artistlist.append({"id": artist['id'], "artist": artist['name']})
|
||||
flaskred.set(artist['id'], 'artist'.encode('UTF-8'))
|
||||
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']}"
|
||||
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']:
|
||||
worklist.append({"id": work['id'], "title": work['title']})
|
||||
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']}"
|
||||
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':
|
||||
@ -178,6 +197,9 @@ class SingleListApi(Resource):
|
||||
if 'release-list' in currrecording['recording'] and currrecording['recording']['release-list']:
|
||||
currrlease = currrecording['recording']['release-list'][0]
|
||||
currrec['album'] = currrlease['title']
|
||||
recording_spot = self.spotify.search(q=recording['title'], type="track", 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}
|
||||
@ -189,11 +211,10 @@ class SingleListApi(Resource):
|
||||
return retdata, 200
|
||||
|
||||
|
||||
class ItemApi(Resource):
|
||||
class ItemApi(SpotifyUserStoreResource):
|
||||
"""
|
||||
See: https://swagger.kmlabz.com/?urls.primaryName=onSpot%20Backend#/backend/getItem
|
||||
"""
|
||||
encryptor = EncryptedUserRedis(ENCODED_SECRET_KEY)
|
||||
|
||||
def get(self, itemid: str):
|
||||
try:
|
||||
@ -208,18 +229,27 @@ class ItemApi(Resource):
|
||||
abort(404, "unknown item")
|
||||
if item_type == 'release':
|
||||
currrelease = musicbrainzngs.get_release_by_id(itemid, includes=['artists'])['release']
|
||||
print(currrelease)
|
||||
album_spot = self.spotify.search(q=currrelease['title'], type="album", limit=1)['albums']['items']
|
||||
retdata = {"id": itemid, "album": currrelease['title']}
|
||||
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']}"
|
||||
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']}"
|
||||
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']
|
||||
@ -227,6 +257,8 @@ class ItemApi(Resource):
|
||||
if 'release-list' in currrecording and currrecording['release-list']:
|
||||
currrlease = currrecording['release-list'][0]
|
||||
retdata['album'] = currrlease['title']
|
||||
if len(recording_spot) > 0:
|
||||
retdata['spotify_id'] = f"spotify:track:{recording_spot[0]['id']}"
|
||||
else:
|
||||
abort(417, "wrong type of item")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user