2020-02-03 23:37:18 +00:00
|
|
|
from flask import Blueprint, request, jsonify
|
2020-04-30 14:54:05 +01:00
|
|
|
from google.cloud import firestore
|
|
|
|
from werkzeug.security import generate_password_hash
|
2019-08-08 12:25:53 +01:00
|
|
|
|
2019-08-12 00:34:04 +01:00
|
|
|
import os
|
2019-08-08 12:25:53 +01:00
|
|
|
import json
|
2019-08-17 18:30:13 +01:00
|
|
|
import logging
|
2020-02-24 18:15:38 +00:00
|
|
|
from datetime import datetime
|
2019-08-08 12:25:53 +01:00
|
|
|
|
2022-11-28 18:34:29 +00:00
|
|
|
from music.api.decorators import login_or_jwt, login_required, \
|
2022-08-16 18:00:38 +01:00
|
|
|
admin_required, cloud_task, validate_json, validate_args, spotify_link_required, no_locked_users
|
2020-06-30 16:38:06 +01:00
|
|
|
from music.cloud import queue_run_user_playlist, offload_or_run_user_playlist
|
|
|
|
from music.cloud.tasks import update_all_user_playlists, update_playlists
|
2020-07-29 10:45:40 +01:00
|
|
|
|
|
|
|
from music.tasks.create_playlist import create_playlist
|
2020-06-21 15:30:51 +01:00
|
|
|
from music.tasks.run_user_playlist import run_user_playlist
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
from music.model.user import User
|
|
|
|
from music.model.playlist import Playlist
|
|
|
|
|
2019-10-19 17:14:11 +01:00
|
|
|
import music.db.database as database
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
from spotframework.net.network import SpotifyNetworkException
|
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
blueprint = Blueprint('api', __name__)
|
|
|
|
db = firestore.Client()
|
2019-08-17 18:30:13 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/playlists', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-11-28 22:30:00 +00:00
|
|
|
def all_playlists_route(auth: dict = None, user: User = None):
|
2021-03-23 22:26:59 +00:00
|
|
|
"""Retrieve all playlists for a given user
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user ([type], optional): [description]. Defaults to None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
HTTP Response: All playlists for given user
|
|
|
|
"""
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
assert user is not None
|
2019-10-23 14:44:17 +01:00
|
|
|
return jsonify({
|
2020-07-29 10:45:40 +01:00
|
|
|
'playlists': [i.to_dict() for i in Playlist.collection.parent(user.key).fetch()]
|
2019-10-23 14:44:17 +01:00
|
|
|
}), 200
|
2019-07-31 20:31:01 +01:00
|
|
|
|
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
@blueprint.route('/playlist', methods=['GET', 'DELETE'])
|
2022-08-08 18:37:17 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2020-07-29 10:45:40 +01:00
|
|
|
@validate_args(('name', str))
|
2022-11-28 22:30:00 +00:00
|
|
|
def playlist_get_delete_route(auth: dict = None, user: User = None):
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
playlist = user.get_playlist(name := request.args['name'], raise_error=False)
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if playlist is None:
|
2022-11-28 22:30:00 +00:00
|
|
|
return jsonify({'error': f'playlist {name} not found'}), 404
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if request.method == "GET":
|
|
|
|
return jsonify(playlist.to_dict()), 200
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
elif request.method == 'DELETE':
|
|
|
|
Playlist.collection.parent(user.key).delete(key=playlist.key)
|
|
|
|
return jsonify({"message": 'playlist deleted', "status": "success"}), 200
|
2019-07-31 12:24:10 +01:00
|
|
|
|
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
@blueprint.route('/playlist', methods=['POST', 'PUT'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2020-07-29 10:45:40 +01:00
|
|
|
@validate_json(('name', str))
|
2022-11-28 22:30:00 +00:00
|
|
|
def playlist_post_put_route(auth: dict = None, user: User = None):
|
2020-04-30 14:54:05 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
request_json = request.get_json()
|
2020-04-30 14:54:05 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist_name = request_json['name']
|
|
|
|
playlist_references = []
|
2020-04-30 14:54:05 +01:00
|
|
|
|
2022-08-16 23:19:08 +01:00
|
|
|
if request_refs := request_json.get('playlist_references', None):
|
|
|
|
if request_refs != -1:
|
|
|
|
for i in request_refs:
|
2020-04-30 14:54:05 +01:00
|
|
|
|
2021-06-16 20:40:14 +01:00
|
|
|
playlist = user.get_playlist(i, raise_error=False)
|
2020-07-29 10:45:40 +01:00
|
|
|
if playlist is not None:
|
|
|
|
playlist_references.append(db.document(playlist.key))
|
|
|
|
else:
|
|
|
|
return jsonify({"message": f'managed playlist {i} not found', "status": "error"}), 400
|
2020-04-30 14:54:05 +01:00
|
|
|
|
2022-08-16 23:19:08 +01:00
|
|
|
if len(playlist_references) == 0 and request_refs != -1:
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist_references = None
|
2020-04-30 14:54:05 +01:00
|
|
|
|
2021-06-16 20:40:14 +01:00
|
|
|
searched_playlist = user.get_playlist(playlist_name, raise_error=False)
|
2020-04-30 14:54:05 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
# CREATE
|
|
|
|
if request.method == 'PUT':
|
2019-08-03 12:10:24 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if searched_playlist is not None:
|
|
|
|
return jsonify({'error': 'playlist already exists'}), 400
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist = Playlist(parent=user.key)
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist.name = request_json['name']
|
2019-08-05 18:29:46 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
for key in [i for i in Playlist.mutable_keys if i not in ['playlist_references', 'type']]:
|
|
|
|
setattr(playlist, key, request_json.get(key, None))
|
2019-08-05 21:43:09 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist.playlist_references = playlist_references
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist.last_updated = datetime.utcnow()
|
|
|
|
playlist.lastfm_stat_last_refresh = datetime.utcnow()
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if playlist_type := request_json.get('type'):
|
|
|
|
playlist_type = playlist_type.strip().lower()
|
2020-07-29 10:45:40 +01:00
|
|
|
if playlist_type in ['default', 'recents', 'fmchart']:
|
|
|
|
playlist.type = playlist_type
|
|
|
|
else:
|
|
|
|
playlist.type = 'default'
|
|
|
|
logger.warning(f'invalid type ({playlist_type}), {user.username} / {playlist_name}')
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if user.spotify_linked:
|
|
|
|
new_playlist = create_playlist(user, playlist_name)
|
|
|
|
playlist.uri = str(new_playlist.uri)
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist.save()
|
|
|
|
logger.info(f'added {user.username} / {playlist_name}')
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
return jsonify({"message": 'playlist added', "status": "success"}), 201
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
# UPDATE
|
|
|
|
elif request.method == 'POST':
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if searched_playlist is None:
|
|
|
|
return jsonify({'error': "playlist doesn't exist"}), 400
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
# ATTRIBUTES
|
|
|
|
for rec_key, rec_item in request_json.items():
|
|
|
|
# type and parts require extra validation
|
|
|
|
if rec_key in [k for k in Playlist.mutable_keys if k not in ['type', 'parts', 'playlist_references']]:
|
2021-06-16 20:40:14 +01:00
|
|
|
setattr(searched_playlist, rec_key, request_json[rec_key])
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
# COMPONENTS
|
2022-08-16 23:19:08 +01:00
|
|
|
if request_parts := request_json.get('parts'):
|
|
|
|
if request_parts == -1:
|
2021-06-16 20:40:14 +01:00
|
|
|
searched_playlist.parts = []
|
2020-07-29 10:45:40 +01:00
|
|
|
else:
|
2022-08-16 23:19:08 +01:00
|
|
|
searched_playlist.parts = request_parts
|
|
|
|
|
|
|
|
if request_part_addition := request_json.get('add_part'):
|
|
|
|
if request_part_addition not in searched_playlist.parts:
|
|
|
|
searched_playlist.parts = searched_playlist.parts + [request_part_addition]
|
|
|
|
|
|
|
|
if request_part_deletion := request_json.get('remove_part'):
|
|
|
|
if request_part_deletion in searched_playlist.parts:
|
|
|
|
searched_playlist.parts.remove(request_part_deletion)
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if playlist_references is not None:
|
|
|
|
if playlist_references == -1:
|
2021-06-16 20:40:14 +01:00
|
|
|
searched_playlist.playlist_references = []
|
2020-07-29 10:45:40 +01:00
|
|
|
else:
|
2021-06-16 20:40:14 +01:00
|
|
|
searched_playlist.playlist_references = playlist_references
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2022-08-16 23:19:08 +01:00
|
|
|
if request_ref_addition := request_json.get('add_ref'):
|
|
|
|
playlist = user.get_playlist(request_ref_addition, raise_error=False)
|
|
|
|
if playlist is not None and playlist.id not in [x.id for x in searched_playlist.playlist_references]:
|
|
|
|
searched_playlist.playlist_references = searched_playlist.playlist_references + [db.document(playlist.key)]
|
|
|
|
else:
|
|
|
|
return jsonify({"message": f'managed playlist {request_ref_addition} not found', "status": "error"}), 400
|
|
|
|
|
|
|
|
if request_ref_deletion := request_json.get('remove_ref'):
|
|
|
|
playlist = user.get_playlist(request_ref_deletion, raise_error=False)
|
|
|
|
if playlist is not None and playlist.id in [x.id for x in searched_playlist.playlist_references]:
|
|
|
|
searched_playlist.playlist_references = [i for i in searched_playlist.playlist_references if i.id != playlist.id]
|
|
|
|
else:
|
|
|
|
return jsonify({"message": f'managed playlist {request_ref_deletion} not found', "status": "error"}), 400
|
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
# ATTRIBUTE WITH CHECKS
|
2022-08-16 23:19:08 +01:00
|
|
|
if request_type := request_json.get('type'):
|
|
|
|
playlist_type = request_type.strip().lower()
|
2020-07-29 10:45:40 +01:00
|
|
|
if playlist_type in ['default', 'recents', 'fmchart']:
|
2021-06-16 20:40:14 +01:00
|
|
|
searched_playlist.type = playlist_type
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2021-06-16 20:40:14 +01:00
|
|
|
searched_playlist.update()
|
2020-07-29 10:45:40 +01:00
|
|
|
logger.info(f'updated {user.username} / {playlist_name}')
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
return jsonify({"message": 'playlist updated', "status": "success"}), 200
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2022-11-28 18:34:29 +00:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/user', methods=['GET', 'POST'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-11-28 22:30:00 +00:00
|
|
|
def user_route(auth: dict = None, user: User = None):
|
2020-04-30 14:54:05 +01:00
|
|
|
assert user is not None
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if request.method == 'GET':
|
2020-04-30 14:54:05 +01:00
|
|
|
return jsonify(user.to_dict()), 200
|
2019-08-05 21:43:09 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
else: # POST
|
2019-09-16 02:22:58 +01:00
|
|
|
request_json = request.get_json()
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if (username := request_json.get('username')) and username.strip().lower() != user.username:
|
|
|
|
if user.type != "admin":
|
|
|
|
return jsonify({'status': 'error', 'message': 'unauthorized'}), 401
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
user = User.collection.filter('username', '==', request_json['username'].strip().lower()).get()
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if (locked := request_json.get('locked')) and user.type == "admin":
|
|
|
|
logger.info(f'updating lock {user.username} / {locked}')
|
|
|
|
user.locked = locked
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if (spotify_linked := request_json.get('spotify_linked')) and not spotify_linked:
|
2020-04-30 14:54:05 +01:00
|
|
|
logger.info(f'deauthing {user.username}')
|
2022-11-28 22:30:00 +00:00
|
|
|
|
|
|
|
user.access_token = None
|
|
|
|
user.refresh_token = None
|
|
|
|
user.spotify_linked = False
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-10-19 17:57:56 +01:00
|
|
|
if 'lastfm_username' in request_json:
|
2020-04-30 14:54:05 +01:00
|
|
|
logger.info(f'updating lastfm username {user.username} -> {request_json["lastfm_username"]}')
|
|
|
|
user.lastfm_username = request_json['lastfm_username']
|
|
|
|
|
2022-08-14 19:32:24 +01:00
|
|
|
if user.lastfm_username is None:
|
|
|
|
user.lastfm_username = ""
|
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if apns_token := request_json.get('apns_token'):
|
2022-11-28 18:34:29 +00:00
|
|
|
if user.apns_tokens is None:
|
|
|
|
user.apns_tokens = []
|
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if apns_token not in user.apns_tokens:
|
|
|
|
logger.info(f'adding apns token {user.username} -> {apns_token}')
|
|
|
|
user.apns_tokens = user.apns_tokens + [apns_token]
|
2022-11-28 18:34:29 +00:00
|
|
|
else:
|
2022-11-28 22:30:00 +00:00
|
|
|
logger.info(f'skipping duplicate apns token {user.username} -> {apns_token}')
|
2022-11-28 18:34:29 +00:00
|
|
|
|
2022-12-10 08:26:19 +00:00
|
|
|
if 'notify' in request_json:
|
|
|
|
notify = request_json['notify']
|
|
|
|
|
2022-12-09 08:37:05 +00:00
|
|
|
logger.info(f'updating notification settings for {user.username} -> {notify}')
|
|
|
|
user.notify = notify
|
|
|
|
|
2022-12-10 08:26:19 +00:00
|
|
|
if 'notify_playlist_updates' in request_json:
|
|
|
|
notify_playlist_updates = request_json['notify_playlist_updates']
|
|
|
|
|
2022-12-09 08:37:05 +00:00
|
|
|
logger.info(f'updating playlist update notification settings for {user.username} -> {notify_playlist_updates}')
|
|
|
|
user.notify_playlist_updates = notify_playlist_updates
|
|
|
|
|
2022-12-10 08:26:19 +00:00
|
|
|
if 'notify_tag_updates' in request_json:
|
|
|
|
notify_tag_updates = request_json['notify_tag_updates']
|
|
|
|
|
2022-12-09 08:37:05 +00:00
|
|
|
logger.info(f'updating playlist update notification settings for {user.username} -> {notify_tag_updates}')
|
|
|
|
user.notify_tag_updates = notify_tag_updates
|
|
|
|
|
2022-12-10 08:26:19 +00:00
|
|
|
if 'notify_admins' in request_json:
|
|
|
|
notify_admins = request_json['notify_admins']
|
|
|
|
|
2022-12-09 08:37:05 +00:00
|
|
|
logger.info(f'updating admin notification settings for {user.username} -> {notify_admins}')
|
|
|
|
user.notify_admins = notify_admins
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
user.update()
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
logger.info(f'updated {user.username}')
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'message': 'account updated', 'status': 'succeeded'}), 200
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
|
2022-08-10 23:15:30 +01:00
|
|
|
@blueprint.route('/user', methods=['DELETE'])
|
|
|
|
@login_or_jwt
|
2022-11-28 22:30:00 +00:00
|
|
|
def user_delete_route(auth: dict = None, user: User = None):
|
2022-08-10 23:15:30 +01:00
|
|
|
assert user is not None
|
|
|
|
|
|
|
|
if user.type == 'admin' and (username_override := request.args.get('username')) is not None:
|
|
|
|
user = User.collection.filter('username', '==', username_override.strip().lower()).get()
|
|
|
|
|
|
|
|
User.collection.delete(user.key, child=True)
|
|
|
|
|
|
|
|
logger.info(f'user {user.username} deleted')
|
|
|
|
|
|
|
|
return jsonify({'message': 'account deleted', 'status': 'succeeded'}), 200
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/users', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2019-09-16 02:22:58 +01:00
|
|
|
@admin_required
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-11-28 22:30:00 +00:00
|
|
|
def all_users_route(auth: dict = None, user: User = None):
|
2019-10-23 14:44:17 +01:00
|
|
|
return jsonify({
|
2020-04-30 14:54:05 +01:00
|
|
|
'accounts': [i.to_dict() for i in User.collection.fetch()]
|
2019-10-23 14:44:17 +01:00
|
|
|
}), 200
|
2019-07-30 16:25:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/user/password', methods=['POST'])
|
2019-09-16 02:22:58 +01:00
|
|
|
@login_required
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2020-07-29 10:45:40 +01:00
|
|
|
@validate_json(('new_password', str), ('current_password', str))
|
2022-11-28 22:30:00 +00:00
|
|
|
def change_password(user: User = None):
|
2019-07-30 16:25:01 +01:00
|
|
|
request_json = request.get_json()
|
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if len(new_password := request_json['new_password']) == 0:
|
2020-07-29 10:45:40 +01:00
|
|
|
return jsonify({"error": 'zero length password'}), 400
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2022-11-28 22:30:00 +00:00
|
|
|
if len(new_password) > 30:
|
2020-07-29 10:45:40 +01:00
|
|
|
return jsonify({"error": 'password too long'}), 400
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if user.check_password(request_json['current_password']):
|
2022-11-28 22:30:00 +00:00
|
|
|
user.password = generate_password_hash(new_password)
|
2020-07-29 10:45:40 +01:00
|
|
|
user.update()
|
|
|
|
logger.info(f'password udpated {user.username}')
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
return jsonify({"message": 'password changed', "status": "success"}), 200
|
2019-07-29 11:44:10 +01:00
|
|
|
else:
|
2020-07-29 10:45:40 +01:00
|
|
|
logger.warning(f"incorrect password {user.username}")
|
|
|
|
return jsonify({'error': 'wrong password provided'}), 401
|
2019-08-02 12:54:18 +01:00
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/playlist/run', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2020-07-29 10:45:40 +01:00
|
|
|
@validate_args(('name', str))
|
2022-11-28 22:30:00 +00:00
|
|
|
def run_playlist(auth: dict = None, user: User = None):
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD':
|
|
|
|
queue_run_user_playlist(user.username, request.args['name']) # pass to either cloud tasks or functions
|
2019-08-02 12:54:18 +01:00
|
|
|
else:
|
2021-02-08 16:18:16 +00:00
|
|
|
run_user_playlist(user, request.args['name']) # update synchronously
|
2020-07-29 10:45:40 +01:00
|
|
|
|
|
|
|
return jsonify({'message': 'execution requested', 'status': 'success'}), 200
|
2019-08-03 23:36:14 +01:00
|
|
|
|
|
|
|
|
2019-08-08 12:25:53 +01:00
|
|
|
@blueprint.route('/playlist/run/task', methods=['POST'])
|
2019-09-16 02:22:58 +01:00
|
|
|
@cloud_task
|
2020-06-30 16:38:06 +01:00
|
|
|
def run_playlist_task(): # receives cloud tasks request for update
|
2019-08-08 12:25:53 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
|
|
|
payload = json.loads(payload)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
logger.info(f'running {payload["username"]} / {payload["name"]}')
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2020-06-30 16:38:06 +01:00
|
|
|
offload_or_run_user_playlist(payload['username'], payload['name']) # check whether offloading to cloud function
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'message': 'executed playlist', 'status': 'success'}), 200
|
2019-08-08 12:25:53 +01:00
|
|
|
|
2020-06-29 20:11:05 +01:00
|
|
|
logger.critical('no payload provided')
|
|
|
|
|
2019-08-08 12:25:53 +01:00
|
|
|
|
2019-08-03 23:36:14 +01:00
|
|
|
@blueprint.route('/playlist/run/user', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-11-28 22:30:00 +00:00
|
|
|
def run_user(auth: dict = None, user: User = None):
|
2019-08-03 23:36:14 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
if user.type == 'admin':
|
|
|
|
user_name = request.args.get('username', user.username)
|
2019-09-16 02:22:58 +01:00
|
|
|
else:
|
2020-04-30 14:54:05 +01:00
|
|
|
user_name = user.username
|
2019-08-03 23:36:14 +01:00
|
|
|
|
2020-05-15 23:25:19 +01:00
|
|
|
update_playlists(user_name)
|
2019-08-03 23:36:14 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'message': 'executed user', 'status': 'success'}), 200
|
2019-08-03 23:36:14 +01:00
|
|
|
|
|
|
|
|
2019-08-08 12:25:53 +01:00
|
|
|
@blueprint.route('/playlist/run/user/task', methods=['POST'])
|
2019-09-16 02:22:58 +01:00
|
|
|
@cloud_task
|
2019-08-08 12:25:53 +01:00
|
|
|
def run_user_task():
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
2020-05-15 23:25:19 +01:00
|
|
|
update_playlists(payload)
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'message': 'executed user', 'status': 'success'}), 200
|
2019-08-08 12:25:53 +01:00
|
|
|
|
|
|
|
|
2019-08-03 23:36:14 +01:00
|
|
|
@blueprint.route('/playlist/run/users', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2019-09-16 02:22:58 +01:00
|
|
|
@admin_required
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-11-28 22:30:00 +00:00
|
|
|
def run_users(auth: dict = None, user: User = None):
|
2019-08-03 23:36:14 +01:00
|
|
|
|
2020-05-15 23:25:19 +01:00
|
|
|
update_all_user_playlists()
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'message': 'executed all users', 'status': 'success'}), 200
|
2019-08-03 23:36:14 +01:00
|
|
|
|
|
|
|
|
2020-03-07 21:53:52 +00:00
|
|
|
@blueprint.route('/playlist/image', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2020-08-13 19:50:21 +01:00
|
|
|
@spotify_link_required
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2020-07-29 10:45:40 +01:00
|
|
|
@validate_args(('name', str))
|
2022-11-28 22:30:00 +00:00
|
|
|
def image(auth: dict = None, user: User = None):
|
2020-03-07 21:53:52 +00:00
|
|
|
|
2021-06-16 20:40:14 +01:00
|
|
|
_playlist = user.get_playlist(request.args['name'], raise_error=False)
|
2020-03-07 21:53:52 +00:00
|
|
|
if _playlist is None:
|
|
|
|
return jsonify({'error': "playlist not found"}), 404
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
net = database.get_authed_spotify_network(user)
|
2020-03-07 21:53:52 +00:00
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
return jsonify({'images': net.playlist(uri=_playlist.uri).images, 'status': 'success'}), 200
|
2020-06-22 20:21:54 +01:00
|
|
|
except SpotifyNetworkException as e:
|
2020-07-01 11:03:43 +01:00
|
|
|
logger.exception(f'error occured during {_playlist.name} / {user.username} playlist retrieval')
|
2020-06-22 20:21:54 +01:00
|
|
|
return jsonify({'error': f"spotify error occured: {e.http_code}"}), 404
|