2020-02-03 23:37:18 +00:00
|
|
|
from flask import Blueprint, request, jsonify
|
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
|
2019-08-08 12:25:53 +01:00
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
from google.cloud import firestore
|
|
|
|
|
2019-10-19 17:14:11 +01:00
|
|
|
from music.api.decorators import login_required, login_or_basic_auth, admin_required, gae_cron, cloud_task
|
2020-02-03 23:37:18 +00:00
|
|
|
from music.cloud.tasks import execute_all_user_playlists, execute_user_playlists, create_run_user_playlist_task, \
|
|
|
|
create_play_user_playlist_task
|
2019-10-19 17:14:11 +01:00
|
|
|
from music.tasks.run_user_playlist import run_user_playlist as run_user_playlist
|
|
|
|
from music.tasks.play_user_playlist import play_user_playlist as play_user_playlist
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-19 17:14:11 +01:00
|
|
|
import music.db.database as database
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-07-29 11:44:10 +01:00
|
|
|
blueprint = Blueprint('api', __name__)
|
|
|
|
db = firestore.Client()
|
2019-08-08 12:25:53 +01:00
|
|
|
|
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'])
|
2019-09-25 19:28:38 +01:00
|
|
|
@login_or_basic_auth
|
|
|
|
def get_playlists(username=None):
|
2019-10-23 14:44:17 +01:00
|
|
|
return jsonify({
|
|
|
|
'playlists': [i.to_dict() for i in database.get_user_playlists(username)]
|
|
|
|
}), 200
|
2019-07-31 20:31:01 +01:00
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/playlist', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
2019-09-25 19:28:38 +01:00
|
|
|
@login_or_basic_auth
|
|
|
|
def playlist(username=None):
|
2019-07-31 20:31:01 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
user_playlists = database.get_user_playlists(username)
|
|
|
|
|
2019-10-27 19:05:50 +00:00
|
|
|
user_ref = database.get_user(username).db_ref
|
2019-09-16 02:22:58 +01:00
|
|
|
playlists = user_ref.collection(u'playlists')
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if request.method == 'GET' or request.method == 'DELETE':
|
|
|
|
playlist_name = request.args.get('name', None)
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_name:
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
queried_playlist = next((i for i in user_playlists if i.name == playlist_name), None)
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
if queried_playlist is None:
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'error': 'no playlist found'}), 404
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if request.method == "GET":
|
2019-10-23 14:44:17 +01:00
|
|
|
return jsonify(queried_playlist.to_dict()), 200
|
2019-09-12 09:59:00 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
elif request.method == 'DELETE':
|
2019-10-23 14:44:17 +01:00
|
|
|
database.delete_playlist(username=username, name=playlist_name)
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({"message": 'playlist deleted', "status": "success"}), 200
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
else:
|
|
|
|
return jsonify({"error": 'no name requested'}), 400
|
2019-08-05 18:29:46 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
elif request.method == 'POST' or request.method == 'PUT':
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
request_json = request.get_json()
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if 'name' not in request_json:
|
|
|
|
return jsonify({'error': "no name provided"}), 400
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlist_name = request_json['name']
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlist_parts = request_json.get('parts', None)
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlist_references = []
|
2019-08-03 12:10:24 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if request_json.get('playlist_references', None):
|
|
|
|
if request_json['playlist_references'] != -1:
|
|
|
|
for i in request_json['playlist_references']:
|
2019-10-23 14:44:17 +01:00
|
|
|
|
|
|
|
updating_playlist = database.get_playlist(username=username, name=i)
|
|
|
|
if updating_playlist is not None:
|
|
|
|
playlist_references.append(updating_playlist.db_ref)
|
2019-09-16 02:22:58 +01:00
|
|
|
else:
|
|
|
|
return jsonify({"message": f'managed playlist {i} not found', "status": "error"}), 400
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if len(playlist_references) == 0 and request_json.get('playlist_references', None) != -1:
|
|
|
|
playlist_references = None
|
2019-08-03 12:10:24 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlist_uri = request_json.get('uri', None)
|
|
|
|
playlist_shuffle = request_json.get('shuffle', None)
|
|
|
|
playlist_type = request_json.get('type', None)
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlist_day_boundary = request_json.get('day_boundary', None)
|
|
|
|
playlist_add_this_month = request_json.get('add_this_month', None)
|
|
|
|
playlist_add_last_month = request_json.get('add_last_month', None)
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
playlist_library_tracks = request_json.get('include_library_tracks', None)
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlist_recommendation = request_json.get('include_recommendations', None)
|
|
|
|
playlist_recommendation_sample = request_json.get('recommendation_sample', None)
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
playlist_chart_range = request_json.get('chart_range', None)
|
|
|
|
playlist_chart_limit = request_json.get('chart_limit', None)
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
queried_playlist = [i for i in playlists.where(u'name', u'==', playlist_name).stream()]
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if request.method == 'PUT':
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if len(queried_playlist) != 0:
|
|
|
|
return jsonify({'error': 'playlist already exists'}), 400
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-10-19 17:14:11 +01:00
|
|
|
from music.tasks.create_playlist import create_playlist as create_playlist
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
to_add = {
|
|
|
|
'name': playlist_name,
|
|
|
|
'parts': playlist_parts if playlist_parts is not None else [],
|
|
|
|
'playlist_references': playlist_references if playlist_references is not None else [],
|
2019-11-01 23:46:49 +00:00
|
|
|
'include_library_tracks': playlist_library_tracks if playlist_library_tracks is not None else False,
|
2019-09-16 02:22:58 +01:00
|
|
|
'include_recommendations': playlist_recommendation if playlist_recommendation is not None else False,
|
|
|
|
'recommendation_sample': playlist_recommendation_sample if playlist_recommendation_sample is not None else 10,
|
|
|
|
'uri': None,
|
|
|
|
'shuffle': playlist_shuffle if playlist_shuffle is not None else False,
|
|
|
|
'type': playlist_type if playlist_type is not None else 'default'
|
|
|
|
}
|
2019-08-04 02:11:33 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if user_ref.get().to_dict()['spotify_linked']:
|
2019-09-25 19:28:38 +01:00
|
|
|
new_playlist = create_playlist(username, playlist_name)
|
2019-09-16 02:22:58 +01:00
|
|
|
to_add['uri'] = str(new_playlist.uri) if new_playlist is not None else None
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_type == 'recents':
|
|
|
|
to_add['day_boundary'] = playlist_day_boundary if playlist_day_boundary is not None else 21
|
|
|
|
to_add['add_this_month'] = playlist_add_this_month if playlist_add_this_month is not None else False
|
|
|
|
to_add['add_last_month'] = playlist_add_last_month if playlist_add_last_month is not None else False
|
2019-07-31 20:31:01 +01:00
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
if playlist_type == 'fmchart':
|
|
|
|
to_add['chart_range'] = playlist_chart_range
|
|
|
|
to_add['chart_limit'] = playlist_chart_limit if playlist_chart_limit is not None else 50
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlists.document().set(to_add)
|
2019-09-25 19:28:38 +01:00
|
|
|
logger.info(f'added {username} / {playlist_name}')
|
2019-08-03 12:10:24 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({"message": 'playlist added', "status": "success"}), 201
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
elif request.method == 'POST':
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if len(queried_playlist) == 0:
|
|
|
|
return jsonify({'error': "playlist doesn't exist"}), 400
|
2019-08-05 18:29:46 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if len(queried_playlist) > 1:
|
|
|
|
return jsonify({'error': "multiple playlists exist"}), 500
|
2019-08-05 18:29:46 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
updating_playlist = database.get_playlist(username=username, name=playlist_name)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
dic = {}
|
2019-08-05 21:43:09 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_parts is not None:
|
|
|
|
if playlist_parts == -1:
|
|
|
|
dic['parts'] = []
|
|
|
|
else:
|
|
|
|
dic['parts'] = playlist_parts
|
2019-07-31 12:24:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_references is not None:
|
|
|
|
if playlist_references == -1:
|
|
|
|
dic['playlist_references'] = []
|
|
|
|
else:
|
|
|
|
dic['playlist_references'] = playlist_references
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_uri is not None:
|
|
|
|
dic['uri'] = playlist_uri
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_shuffle is not None:
|
|
|
|
dic['shuffle'] = playlist_shuffle
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_day_boundary is not None:
|
|
|
|
dic['day_boundary'] = playlist_day_boundary
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_add_this_month is not None:
|
|
|
|
dic['add_this_month'] = playlist_add_this_month
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_add_last_month is not None:
|
|
|
|
dic['add_last_month'] = playlist_add_last_month
|
2019-07-29 11:44:10 +01:00
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
if playlist_library_tracks is not None:
|
|
|
|
dic['include_library_tracks'] = playlist_library_tracks
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_recommendation is not None:
|
|
|
|
dic['include_recommendations'] = playlist_recommendation
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_recommendation_sample is not None:
|
|
|
|
dic['recommendation_sample'] = playlist_recommendation_sample
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
if playlist_chart_range is not None:
|
|
|
|
dic['chart_range'] = playlist_chart_range
|
|
|
|
|
|
|
|
if playlist_chart_limit is not None:
|
|
|
|
dic['chart_limit'] = playlist_chart_limit
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_type is not None:
|
|
|
|
dic['type'] = playlist_type
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
if playlist_type == 'fmchart':
|
|
|
|
dic['chart_range'] = 'YEAR'
|
|
|
|
dic['chart_limit'] = 50
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if len(dic) == 0:
|
2019-09-25 19:28:38 +01:00
|
|
|
logger.warning(f'no changes to make for {username} / {playlist_name}')
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({"message": 'no changes to make', "status": "error"}), 400
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
updating_playlist.update_database(dic)
|
2019-09-25 19:28:38 +01:00
|
|
|
logger.info(f'updated {username} / {playlist_name}')
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({"message": 'playlist updated', "status": "success"}), 200
|
2019-08-03 21:35:08 +01:00
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/user', methods=['GET', 'POST'])
|
2019-09-25 19:28:38 +01:00
|
|
|
@login_or_basic_auth
|
|
|
|
def user(username=None):
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if request.method == 'GET':
|
2019-08-05 21:43:09 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
database_user = database.get_user(username)
|
|
|
|
return jsonify(database_user.to_dict()), 200
|
2019-08-05 21:43:09 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
else:
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
db_user = database.get_user(username)
|
|
|
|
|
|
|
|
if db_user.user_type != db_user.Type.admin:
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'status': 'error', 'message': 'unauthorized'}), 401
|
2019-08-05 21:43:09 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
request_json = request.get_json()
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-10-19 17:57:56 +01:00
|
|
|
if 'username' in request_json:
|
|
|
|
username = request_json['username']
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
actionable_user = database.get_user(username)
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if 'locked' in request_json:
|
2019-10-23 14:44:17 +01:00
|
|
|
logger.info(f'updating lock {username} / {request_json["locked"]}')
|
|
|
|
actionable_user.locked = request_json['locked']
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if 'spotify_linked' in request_json:
|
2019-10-23 14:44:17 +01:00
|
|
|
logger.info(f'deauthing {username}')
|
2019-09-16 02:22:58 +01:00
|
|
|
if request_json['spotify_linked'] is False:
|
2019-10-23 14:44:17 +01:00
|
|
|
actionable_user.update_database({
|
2019-09-16 02:22:58 +01:00
|
|
|
'access_token': None,
|
|
|
|
'refresh_token': None,
|
|
|
|
'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:
|
|
|
|
logger.info(f'updating lastfm username {username} -> {request_json["lastfm_username"]}')
|
2019-10-23 14:44:17 +01:00
|
|
|
actionable_user.lastfm_username = request_json['lastfm_username']
|
2019-08-03 21:35:08 +01:00
|
|
|
|
2019-10-19 17:57:56 +01:00
|
|
|
logger.info(f'updated {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
|
|
|
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/users', methods=['GET'])
|
2019-09-30 14:19:00 +01:00
|
|
|
@login_or_basic_auth
|
2019-09-16 02:22:58 +01:00
|
|
|
@admin_required
|
2019-09-30 14:19:00 +01:00
|
|
|
def users(username=None):
|
2019-10-23 14:44:17 +01:00
|
|
|
return jsonify({
|
|
|
|
'accounts': [i.to_dict() for i in database.get_users()]
|
|
|
|
}), 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
|
2019-10-19 17:57:56 +01:00
|
|
|
def change_password(username=None):
|
2019-07-30 16:25:01 +01:00
|
|
|
|
|
|
|
request_json = request.get_json()
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if 'new_password' in request_json and 'current_password' in request_json:
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if len(request_json['new_password']) == 0:
|
|
|
|
return jsonify({"error": 'zero length password'}), 400
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if len(request_json['new_password']) > 30:
|
|
|
|
return jsonify({"error": 'password too long'}), 400
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
db_user = database.get_user(username)
|
|
|
|
if db_user.check_password(request_json['current_password']):
|
|
|
|
db_user.password = request_json['new_password']
|
2019-10-19 17:57:56 +01:00
|
|
|
logger.info(f'password udpated {username}')
|
2019-07-30 16:25:01 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({"message": 'password changed', "status": "success"}), 200
|
2019-07-30 16:25:01 +01:00
|
|
|
else:
|
2019-10-19 17:57:56 +01:00
|
|
|
logger.warning(f"incorrect password {username}")
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'error': 'wrong password provided'}), 401
|
2019-07-29 11:44:10 +01:00
|
|
|
|
|
|
|
else:
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'error': 'malformed request, no old_password/new_password'}), 400
|
2019-08-02 12:54:18 +01:00
|
|
|
|
|
|
|
|
2019-08-12 00:34:04 +01:00
|
|
|
@blueprint.route('/playlist/play', methods=['POST'])
|
2019-09-25 19:28:38 +01:00
|
|
|
@login_or_basic_auth
|
|
|
|
def play_playlist(username=None):
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
request_json = request.get_json()
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
request_parts = request_json.get('parts', None)
|
|
|
|
request_playlist_type = request_json.get('playlist_type', 'default')
|
|
|
|
request_playlists = request_json.get('playlists', None)
|
|
|
|
request_shuffle = request_json.get('shuffle', False)
|
|
|
|
request_include_recommendations = request_json.get('include_recommendations', True)
|
|
|
|
request_recommendation_sample = request_json.get('recommendation_sample', 10)
|
|
|
|
request_day_boundary = request_json.get('day_boundary', 10)
|
|
|
|
request_add_this_month = request_json.get('add_this_month', False)
|
|
|
|
request_add_last_month = request_json.get('add_last_month', False)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-09-25 19:28:38 +01:00
|
|
|
request_device_name = request_json.get('device_name', None)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-09-25 19:28:38 +01:00
|
|
|
logger.info(f'playing {username}')
|
|
|
|
|
|
|
|
if (request_parts and len(request_parts) > 0) or (request_playlists and len(request_playlists) > 0):
|
|
|
|
|
|
|
|
if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD':
|
|
|
|
create_play_user_playlist_task(username,
|
|
|
|
parts=request_parts,
|
|
|
|
playlist_type=request_playlist_type,
|
|
|
|
playlists=request_playlists,
|
|
|
|
shuffle=request_shuffle,
|
|
|
|
include_recommendations=request_include_recommendations,
|
|
|
|
recommendation_sample=request_recommendation_sample,
|
|
|
|
day_boundary=request_day_boundary,
|
|
|
|
add_this_month=request_add_this_month,
|
|
|
|
add_last_month=request_add_last_month,
|
|
|
|
device_name=request_device_name)
|
2019-08-12 00:34:04 +01:00
|
|
|
else:
|
2019-09-25 19:28:38 +01:00
|
|
|
play_user_playlist(username,
|
|
|
|
parts=request_parts,
|
|
|
|
playlist_type=request_playlist_type,
|
|
|
|
playlists=request_playlists,
|
|
|
|
shuffle=request_shuffle,
|
|
|
|
include_recommendations=request_include_recommendations,
|
|
|
|
recommendation_sample=request_recommendation_sample,
|
|
|
|
day_boundary=request_day_boundary,
|
|
|
|
add_this_month=request_add_this_month,
|
|
|
|
add_last_month=request_add_last_month,
|
|
|
|
device_name=request_device_name)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-09-25 19:28:38 +01:00
|
|
|
return jsonify({'message': 'execution requested', 'status': 'success'}), 200
|
2019-08-12 00:34:04 +01:00
|
|
|
else:
|
2019-09-25 19:28:38 +01:00
|
|
|
logger.error(f'no playlists/parts {username}')
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'error': 'insufficient playlist sources'}), 400
|
2019-08-12 00:34:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/playlist/play/task', methods=['POST'])
|
2019-09-16 02:22:58 +01:00
|
|
|
@cloud_task
|
2019-08-12 00:34:04 +01:00
|
|
|
def play_playlist_task():
|
2019-09-16 02:22:58 +01:00
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
|
|
|
payload = json.loads(payload)
|
|
|
|
logger.info(f'playing {payload["username"]}')
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
play_user_playlist(payload['username'],
|
|
|
|
parts=payload['parts'],
|
|
|
|
playlist_type=payload['playlist_type'],
|
|
|
|
playlists=payload['playlists'],
|
|
|
|
shuffle=payload['shuffle'],
|
|
|
|
include_recommendations=payload['include_recommendations'],
|
|
|
|
recommendation_sample=payload['recommendation_sample'],
|
|
|
|
day_boundary=payload['day_boundary'],
|
|
|
|
add_this_month=payload['add_this_month'],
|
2019-09-25 19:28:38 +01:00
|
|
|
add_last_month=payload['add_last_month'],
|
|
|
|
device_name=payload['device_name'])
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'message': 'executed playlist', 'status': 'success'}), 200
|
2019-08-02 12:54:18 +01:00
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
@blueprint.route('/playlist/run', methods=['GET'])
|
2019-09-25 19:28:38 +01:00
|
|
|
@login_or_basic_auth
|
|
|
|
def run_playlist(username=None):
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
playlist_name = request.args.get('name', None)
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if playlist_name:
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD':
|
2019-09-25 19:28:38 +01:00
|
|
|
create_run_user_playlist_task(username, playlist_name)
|
2019-08-02 12:54:18 +01:00
|
|
|
else:
|
2019-09-25 19:28:38 +01:00
|
|
|
run_user_playlist(username, playlist_name)
|
2019-09-16 02:22:58 +01:00
|
|
|
|
|
|
|
return jsonify({'message': 'execution requested', 'status': 'success'}), 200
|
2019-08-02 12:54:18 +01:00
|
|
|
|
|
|
|
else:
|
2019-09-16 02:22:58 +01:00
|
|
|
logger.warning('no playlist requested')
|
|
|
|
return jsonify({"error": 'no name requested'}), 400
|
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
|
2019-08-08 12:25:53 +01:00
|
|
|
def run_playlist_task():
|
|
|
|
|
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
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
run_user_playlist(payload['username'], payload['name'])
|
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
|
|
|
|
|
|
|
|
2019-08-03 23:36:14 +01:00
|
|
|
@blueprint.route('/playlist/run/user', methods=['GET'])
|
2019-09-25 19:28:38 +01:00
|
|
|
@login_or_basic_auth
|
|
|
|
def run_user(username=None):
|
2019-08-03 23:36:14 +01:00
|
|
|
|
2019-10-27 19:05:50 +00:00
|
|
|
db_user = database.get_user(username)
|
2020-02-03 23:37:18 +00:00
|
|
|
if db_user.user_type == db_user.Type.admin:
|
2019-09-25 19:28:38 +01:00
|
|
|
user_name = request.args.get('username', username)
|
2019-09-16 02:22:58 +01:00
|
|
|
else:
|
2019-09-25 19:28:38 +01:00
|
|
|
user_name = username
|
2019-08-03 23:36:14 +01:00
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
execute_user_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-02-03 23:37:18 +00:00
|
|
|
execute_user_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'])
|
2019-09-30 14:19:00 +01:00
|
|
|
@login_or_basic_auth
|
2019-09-16 02:22:58 +01:00
|
|
|
@admin_required
|
2019-09-30 14:19:00 +01:00
|
|
|
def run_users(username=None):
|
2019-08-03 23:36:14 +01:00
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
execute_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
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/playlist/run/users/cron', methods=['GET'])
|
2019-09-16 02:22:58 +01:00
|
|
|
@gae_cron
|
2019-08-03 23:36:14 +01:00
|
|
|
def run_users_cron():
|
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
execute_all_user_playlists()
|
2019-09-16 02:22:58 +01:00
|
|
|
return jsonify({'status': 'success'}), 200
|