Compare commits
16 Commits
ecaec93819
...
master
Author | SHA1 | Date | |
---|---|---|---|
fbd25db750 | |||
2e31e99aae | |||
c18dbb8d06 | |||
5f2f73c9df | |||
ea76463739 | |||
73c1636063 | |||
791003f89e | |||
aab6e3e3cd | |||
bf8759d176 | |||
d62ad11de9 | |||
e81c50a4b1 | |||
2b1650b36e | |||
6e1a283f2c | |||
183ffadad6 | |||
811cfc929e | |||
19ea61e339 |
@ -16,4 +16,4 @@ COPY ./src .
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:8080", "--workers", "1", "--threads", "1", "app:app"]
|
||||
ENTRYPOINT ["gunicorn", "-b", "0.0.0.0:8080", "--workers", "4", "--threads", "1", "app:app"]
|
@ -1,28 +0,0 @@
|
||||
version: '3'
|
||||
|
||||
networks:
|
||||
backend:
|
||||
external: false
|
||||
|
||||
services:
|
||||
backend:
|
||||
image: registry.kmlabz.com/onspot/backend
|
||||
restart: always
|
||||
depends_on:
|
||||
- db
|
||||
environment:
|
||||
- ONSPOT_REDIS_URL=redis://db:6379/0
|
||||
- ONSPOT_ENCODED_SECRET_KEY=emdlc2RmYnZoa2xyYWl3ZmJkdmtzZ2Vhd2ZiaWx2a3M=
|
||||
- ONSPOT_SPOTIFY_CLIENT_ID=CHANGEME
|
||||
- ONSPOT_SPOTIFY_CLIENT_SECRET=KEEPMESECRET
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
networks:
|
||||
- backend
|
||||
db:
|
||||
image: redis
|
||||
restart: always
|
||||
ports:
|
||||
- "127.0.0.1:6379:6379"
|
||||
networks:
|
||||
- backend
|
29
src/app.py
29
src/app.py
@ -1,18 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import logging
|
||||
import musicbrainzngs
|
||||
from flask import Flask
|
||||
from flask_restful import Api
|
||||
from flask_cors import CORS
|
||||
import sentry_sdk
|
||||
from sentry_sdk.integrations.flask import FlaskIntegration
|
||||
from sentry_sdk.integrations.redis import RedisIntegration
|
||||
from healthcheck import HealthCheck
|
||||
|
||||
from flaskaddons.marshm import ma
|
||||
from flaskaddons.fred import flaskred
|
||||
from utils.config import SENTRY_DSN, RELEASEMODE, RELEASE_ID, PORT, DEBUG, REDIS_URL, ALLOWED_ORIGINS
|
||||
from utils.errorhandlers import register_all_error_handlers
|
||||
from utils.config import SENTRY_DSN, RELEASEMODE, RELEASE_ID, REDIS_URL, ALLOWED_ORIGINS
|
||||
from utils import register_all_error_handlers, redis_available
|
||||
from resources import LoginApi, ListsApi, MeApi, SingleListApi, ItemApi
|
||||
|
||||
"""
|
||||
@ -27,7 +27,7 @@ __version__text__ = "1"
|
||||
if SENTRY_DSN:
|
||||
sentry_sdk.init(
|
||||
dsn=SENTRY_DSN,
|
||||
integrations=[FlaskIntegration()],
|
||||
integrations=[FlaskIntegration(), RedisIntegration()],
|
||||
traces_sample_rate=1.0,
|
||||
send_default_pii=True,
|
||||
release=RELEASE_ID,
|
||||
@ -36,8 +36,6 @@ if SENTRY_DSN:
|
||||
)
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['JWT_BLACKLIST_ENABLED'] = True
|
||||
app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh']
|
||||
app.config['REDIS_URL'] = REDIS_URL
|
||||
api = Api(app)
|
||||
health = HealthCheck()
|
||||
@ -45,10 +43,6 @@ ma.init_app(app)
|
||||
flaskred.init_app(app)
|
||||
CORS(app, origins=ALLOWED_ORIGINS)
|
||||
|
||||
formatter = logging.Formatter(
|
||||
fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s"
|
||||
)
|
||||
|
||||
|
||||
@app.before_first_request
|
||||
def before_first_request():
|
||||
@ -57,26 +51,13 @@ def before_first_request():
|
||||
musicbrainzngs.https = True
|
||||
|
||||
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(formatter)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.addHandler(handler)
|
||||
|
||||
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/<listid>')
|
||||
api.add_resource(ItemApi, '/api/items/<itemid>')
|
||||
|
||||
health.add_check(redis_available)
|
||||
app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())
|
||||
|
||||
register_all_error_handlers(app)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(
|
||||
debug=bool(DEBUG),
|
||||
host="0.0.0.0",
|
||||
port=int(PORT),
|
||||
)
|
||||
|
@ -1,5 +1,5 @@
|
||||
"""
|
||||
Base resource with user handling and Spotify integration
|
||||
Base resource with user handling, Spotify and MusicBrains integration
|
||||
"""
|
||||
|
||||
__author__ = '@tormakris'
|
||||
@ -25,30 +25,22 @@ class APIInteractionResource(UserStoreResource):
|
||||
spotify_uri = "spotify:"
|
||||
if mb_type == "artist":
|
||||
spot_data = self.spotify.search(q=querystring, type="artist", limit=1)['artists']['items']
|
||||
spotify_uri += "artist:"
|
||||
if spot_data:
|
||||
imagedata = spot_data[0]['images']
|
||||
else:
|
||||
if not spot_data:
|
||||
return None, None, None
|
||||
spotify_uri += "artist:"
|
||||
imagedata = spot_data[0]['images']
|
||||
elif mb_type == "release":
|
||||
spot_data = self.spotify.search(q=querystring, type="artist", limit=1).get(
|
||||
'albums')
|
||||
if spot_data:
|
||||
spot_data = spot_data['items']
|
||||
else:
|
||||
spot_data = self.spotify.search(q=querystring, type="album", limit=1)['albums']['items']
|
||||
if not spot_data:
|
||||
return None, None, None
|
||||
spotify_uri += "album:"
|
||||
if spot_data:
|
||||
imagedata = spot_data[0]['images']
|
||||
else:
|
||||
return None, None, None
|
||||
imagedata = spot_data[0]['images']
|
||||
elif mb_type == "work" or mb_type == "recording":
|
||||
spot_data = self.spotify.search(q=querystring, type="track", limit=1)['tracks']['items']
|
||||
spotify_uri += "track:"
|
||||
if spot_data:
|
||||
imagedata = spot_data[0]['album']['images']
|
||||
else:
|
||||
if not spot_data:
|
||||
return None, None, None
|
||||
spotify_uri += "track:"
|
||||
imagedata = spot_data[0]['album']['images']
|
||||
else:
|
||||
return None, None, None
|
||||
|
||||
@ -58,13 +50,29 @@ class APIInteractionResource(UserStoreResource):
|
||||
else:
|
||||
return spotify_uri, None, None
|
||||
|
||||
def getcoverimage(self, releaseid: str, itemtype: str, artist: str, title: str = "",
|
||||
album: str = "") -> tuple:
|
||||
if itemtype != "recording" and itemtype != "release":
|
||||
return None, None
|
||||
try:
|
||||
musicbimgurl = musicbrainzngs.get_image_list(releaseid)['images']
|
||||
imgurl = musicbimgurl[0]['thumbnails']['large']
|
||||
smallimgurl = musicbimgurl[0]['thumbnails']['small']
|
||||
except Exception as e:
|
||||
_, imgurl, smallimgurl = self.queryspotifyinfo(itemtype, title=title, album=album, artist=artist)
|
||||
current_app.logger.info(e)
|
||||
return imgurl, smallimgurl
|
||||
|
||||
def getreleaseinfo(self, itemid: str) -> dict:
|
||||
currrelease = musicbrainzngs.get_release_by_id(itemid, includes=['artists'])['release']
|
||||
retdata = {"id": itemid, "album": currrelease['title'], "type": "release"}
|
||||
if 'artist-credit' in currrelease and currrelease['artist-credit']:
|
||||
retdata['artist'] = currrelease['artist-credit'][0]['artist']['name']
|
||||
retdata['spotify_id'], retdata['cover_url'], retdata['cover_url_small'] = \
|
||||
self.queryspotifyinfo('release', artist=retdata['artist'], album=retdata['album'])
|
||||
retdata['cover_url'], retdata['cover_url_small'] = self.getcoverimage(
|
||||
itemid, 'release', album=retdata.get('album'),
|
||||
artist=retdata.get('artist'))
|
||||
retdata['spotify_id'], _, _ = \
|
||||
self.queryspotifyinfo('release', album=currrelease['title'])
|
||||
return retdata
|
||||
|
||||
def getartistinfo(self, itemid: str) -> dict:
|
||||
@ -90,17 +98,12 @@ class APIInteractionResource(UserStoreResource):
|
||||
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 imgurl:
|
||||
retdata['cover_url_small'] = imgurl[0]['thumbnails']['small']
|
||||
retdata['cover_url'] = imgurl[0]['thumbnails']['large']
|
||||
retdata['spotify_id'], _, _ = \
|
||||
self.queryspotifyinfo('recording', title=retdata['title'], album=retdata.get('album'),
|
||||
artist=retdata.get('artist'))
|
||||
except Exception as e:
|
||||
retdata['spotify_id'], retdata['cover_url'], retdata['cover_url_small'] = \
|
||||
self.queryspotifyinfo('recording', title=retdata['title'], album=retdata.get('album'),
|
||||
artist=retdata.get('artist'))
|
||||
current_app.logger.info(e)
|
||||
imageid = currrlease['id']
|
||||
else:
|
||||
imageid = itemid
|
||||
retdata['cover_url'], retdata['cover_url_small'] = self.getcoverimage(
|
||||
imageid, 'release', title=retdata['title'],
|
||||
artist=retdata.get('artist'))
|
||||
retdata['spotify_id'], _, _ = \
|
||||
self.queryspotifyinfo('recording', title=retdata['title'])
|
||||
return retdata
|
||||
|
@ -7,6 +7,8 @@ __copyright__ = "Copyright 2020, onSpot Team"
|
||||
__module_name__ = "itemapi"
|
||||
__version__text__ = "1"
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from flask import request, current_app, abort
|
||||
|
||||
from flaskaddons.fred import flaskred
|
||||
@ -24,6 +26,8 @@ class ItemApi(APIInteractionResource):
|
||||
except Exception as e:
|
||||
current_app.logger.info(e)
|
||||
abort(401, "unauthorized")
|
||||
flaskred.expire(request.headers.get('Authorization'), timedelta(minutes=15))
|
||||
|
||||
try:
|
||||
itemtype = flaskred.get(itemid).decode('UTF-8')
|
||||
except Exception as e:
|
||||
|
@ -7,6 +7,8 @@ __copyright__ = "Copyright 2020, onSpot Team"
|
||||
__module_name__ = "listsapi"
|
||||
__version__text__ = "1"
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import musicbrainzngs
|
||||
from flask import current_app, abort, request
|
||||
|
||||
@ -26,6 +28,8 @@ class ListsApi(UserStoreResource):
|
||||
current_app.logger.info(e)
|
||||
abort(401, "unauthorized")
|
||||
|
||||
flaskred.expire(request.headers.get('Authorization'), timedelta(minutes=15))
|
||||
|
||||
musicbrainzngs.auth(currcreds['name'], currcreds['password'])
|
||||
collections = musicbrainzngs.get_collections()
|
||||
musicbrainzngs.auth(None, None)
|
||||
|
@ -8,6 +8,7 @@ __module_name__ = "loginapi"
|
||||
__version__text__ = "1"
|
||||
|
||||
import uuid
|
||||
from datetime import timedelta
|
||||
|
||||
import musicbrainzngs
|
||||
from flask import request, current_app, abort
|
||||
@ -43,7 +44,7 @@ class LoginApi(UserStoreResource):
|
||||
self.encryptor.store(body)
|
||||
token = str(uuid.uuid4())
|
||||
flaskred.set(token, userobj['name'].encode('UTF-8'))
|
||||
|
||||
flaskred.expire(token, timedelta(minutes=15))
|
||||
return {
|
||||
'token': token
|
||||
}, 200
|
||||
|
@ -7,6 +7,8 @@ __copyright__ = "Copyright 2020, onSpot Team"
|
||||
__module_name__ = "meapi"
|
||||
__version__text__ = "1"
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from flask import request, current_app, abort
|
||||
from flask_restful import Resource
|
||||
|
||||
@ -24,4 +26,5 @@ class MeApi(Resource):
|
||||
except Exception as e:
|
||||
current_app.logger.info(e)
|
||||
abort(401, "unauthorized")
|
||||
flaskred.expire(request.headers.get('Authorization'), timedelta(minutes=15))
|
||||
return {"name": currusername}, 200
|
||||
|
@ -7,6 +7,8 @@ __copyright__ = "Copyright 2020, onSpot Team"
|
||||
__module_name__ = "singlelistapi"
|
||||
__version__text__ = "1"
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
import musicbrainzngs
|
||||
from flask import request, current_app, abort
|
||||
|
||||
@ -25,6 +27,7 @@ class SingleListApi(APIInteractionResource):
|
||||
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:
|
||||
|
@ -1 +1,3 @@
|
||||
from .aes_encrypt import AESCrypto
|
||||
from .healthchecks import redis_available
|
||||
from .errorhandlers import register_all_error_handlers
|
@ -5,15 +5,11 @@ import os
|
||||
Configuration
|
||||
"""
|
||||
|
||||
|
||||
__author__ = "@tormakris"
|
||||
__copyright__ = "Copyright 2020, onSpot Team"
|
||||
__module_name__ = "config"
|
||||
__version__text__ = "1"
|
||||
|
||||
|
||||
PORT = os.environ.get("ONSPOT_PORT", 8080)
|
||||
DEBUG = os.environ.get("ONSPOT_DEBUG", True)
|
||||
ALLOWED_ORIGINS = os.environ.get('ALLOWED_ORIGINS', '*')
|
||||
|
||||
SENTRY_DSN = os.environ.get("SENTRY_DSN")
|
||||
|
18
src/utils/healthchecks.py
Normal file
18
src/utils/healthchecks.py
Normal file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from flaskaddons import flaskred
|
||||
|
||||
|
||||
"""
|
||||
Healthchek functions
|
||||
"""
|
||||
|
||||
__author__ = "@tormakris"
|
||||
__copyright__ = "Copyright 2020, onSpot Team"
|
||||
__module_name__ = "healthchecks"
|
||||
__version__text__ = "1"
|
||||
|
||||
|
||||
def redis_available():
|
||||
redisstatus=flaskred.info()
|
||||
return True, redisstatus
|
Reference in New Issue
Block a user