Mixonomer/music/api/api.py

373 lines
13 KiB
Python
Raw Normal View History

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-12 00:34:04 +01:00
import os
import json
import logging
from datetime import datetime
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
from music.cloud.tasks import update_all_user_playlists, update_playlists, run_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
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
blueprint = Blueprint('api', __name__)
db = firestore.Client()
logger = logging.getLogger(__name__)
@blueprint.route('/playlists', methods=['GET'])
@login_or_basic_auth
def all_playlists_route(user=None):
2020-04-30 14:54:05 +01:00
assert user is not None
2019-10-23 14:44:17 +01:00
return jsonify({
2020-04-30 14:54:05 +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
@blueprint.route('/playlist', methods=['GET', 'POST', 'PUT', 'DELETE'])
@login_or_basic_auth
2020-04-30 14:54:05 +01:00
def playlist_route(user=None):
if request.method == 'GET' or request.method == 'DELETE':
playlist_name = request.args.get('name', None)
if playlist_name:
2020-04-30 14:54:05 +01:00
playlist = Playlist.collection.parent(user.key).filter('name', '==', playlist_name).get()
2020-04-30 14:54:05 +01:00
if playlist is None:
return jsonify({'error': f'playlist {playlist_name} not found'}), 404
if request.method == "GET":
2020-04-30 14:54:05 +01:00
return jsonify(playlist.to_dict()), 200
2019-09-12 09:59:00 +01:00
elif request.method == 'DELETE':
2020-04-30 14:54:05 +01:00
Playlist.collection.parent(user.key).delete(key=playlist.key)
return jsonify({"message": 'playlist deleted', "status": "success"}), 200
else:
return jsonify({"error": 'no name requested'}), 400
elif request.method == 'POST' or request.method == 'PUT':
request_json = request.get_json()
if 'name' not in request_json:
return jsonify({'error': "no name provided"}), 400
playlist_name = request_json['name']
playlist_parts = request_json.get('parts', None)
playlist_references = []
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
2020-04-30 14:54:05 +01:00
updating_playlist = Playlist.collection.parent(user.key).filter('name', '==', i).get()
2019-10-23 14:44:17 +01:00
if updating_playlist is not None:
2020-04-30 14:54:05 +01:00
playlist_references.append(db.document(updating_playlist.key))
else:
return jsonify({"message": f'managed playlist {i} not found', "status": "error"}), 400
2019-08-03 21:35:08 +01:00
if len(playlist_references) == 0 and request_json.get('playlist_references', None) != -1:
playlist_references = None
playlist_uri = request_json.get('uri', None)
playlist_shuffle = request_json.get('shuffle', None)
playlist_type = request_json.get('type', None)
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)
playlist_library_tracks = request_json.get('include_library_tracks', None)
playlist_recommendation = request_json.get('include_recommendations', None)
playlist_recommendation_sample = request_json.get('recommendation_sample', None)
playlist_chart_range = request_json.get('chart_range', None)
playlist_chart_limit = request_json.get('chart_limit', None)
2020-04-30 14:54:05 +01:00
playlist = Playlist.collection.parent(user.key).filter('name', '==', playlist_name).get()
if request.method == 'PUT':
2020-04-30 14:54:05 +01:00
if playlist is not None:
return jsonify({'error': 'playlist already exists'}), 400
2019-10-19 17:14:11 +01:00
from music.tasks.create_playlist import create_playlist as create_playlist
2020-04-30 14:54:05 +01:00
new_db_playlist = Playlist(parent=user.key)
new_db_playlist.name = playlist_name
new_db_playlist.parts = playlist_parts
new_db_playlist.playlist_references = playlist_references
new_db_playlist.include_library_tracks = playlist_library_tracks
new_db_playlist.include_recommendations = playlist_recommendation
new_db_playlist.recommendation_sample = playlist_recommendation_sample
new_db_playlist.shuffle = playlist_shuffle
new_db_playlist.type = playlist_type
new_db_playlist.last_updated = datetime.utcnow()
new_db_playlist.lastfm_stat_last_refresh = datetime.utcnow()
new_db_playlist.day_boundary = playlist_day_boundary
new_db_playlist.add_this_month = playlist_add_this_month
new_db_playlist.add_last_month = playlist_add_last_month
new_db_playlist.chart_range = playlist_chart_range
new_db_playlist.chart_limit = playlist_chart_limit
if user.spotify_linked:
new_playlist = create_playlist(user, playlist_name)
new_db_playlist.uri = str(new_playlist.uri)
new_db_playlist.save()
logger.info(f'added {user.username} / {playlist_name}')
return jsonify({"message": 'playlist added', "status": "success"}), 201
elif request.method == 'POST':
2020-04-30 14:54:05 +01:00
if playlist is None:
return jsonify({'error': "playlist doesn't exist"}), 400
2020-04-30 14:54:05 +01:00
updating_playlist = Playlist.collection.parent(user.key).filter('name', '==', playlist_name).get()
2019-08-05 21:43:09 +01:00
if playlist_parts is not None:
if playlist_parts == -1:
2020-04-30 14:54:05 +01:00
updating_playlist.parts = []
else:
2020-04-30 14:54:05 +01:00
updating_playlist.parts = playlist_parts
if playlist_references is not None:
if playlist_references == -1:
2020-04-30 14:54:05 +01:00
updating_playlist.playlist_references = []
else:
2020-04-30 14:54:05 +01:00
updating_playlist.playlist_references = playlist_references
if playlist_uri is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.uri = playlist_uri
if playlist_shuffle is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.shuffle = playlist_shuffle
if playlist_day_boundary is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.day_boundary = playlist_day_boundary
if playlist_add_this_month is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.add_this_month = playlist_add_this_month
if playlist_add_last_month is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.add_last_month = playlist_add_last_month
if playlist_library_tracks is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.include_library_tracks = playlist_library_tracks
if playlist_recommendation is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.include_recommendations = playlist_recommendation
2019-08-03 21:35:08 +01:00
if playlist_recommendation_sample is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.recommendation_sample = playlist_recommendation_sample
2019-08-03 21:35:08 +01:00
if playlist_chart_range is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.chart_range = playlist_chart_range
if playlist_chart_limit is not None:
2020-04-30 14:54:05 +01:00
updating_playlist.chart_limit = playlist_chart_limit
if playlist_type is not None:
playlist_type = playlist_type.strip().lower()
if playlist_type in ['default', 'recents', 'fmchart']:
updating_playlist.type = playlist_type
2019-08-03 21:35:08 +01:00
2020-04-30 14:54:05 +01:00
updating_playlist.update()
logger.info(f'updated {user.username} / {playlist_name}')
2019-08-03 21:35:08 +01:00
return jsonify({"message": 'playlist updated', "status": "success"}), 200
2019-08-03 21:35:08 +01:00
@blueprint.route('/user', methods=['GET', 'POST'])
@login_or_basic_auth
2020-04-30 14:54:05 +01:00
def user_route(user=None):
assert user is not None
2019-08-03 21:35:08 +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
request_json = request.get_json()
2019-08-03 21:35:08 +01:00
if 'username' in request_json:
2020-04-30 14:54:05 +01:00
if request_json['username'].strip().lower() != user.username:
if user.type != "admin":
return jsonify({'status': 'error', 'message': 'unauthorized'}), 401
2019-08-03 21:35:08 +01:00
2020-04-30 14:54:05 +01:00
user = User.collection.filter('username', '==', request_json['username'].strip().lower()).get()
2019-08-03 21:35:08 +01:00
if 'locked' in request_json:
2020-04-30 14:54:05 +01:00
if user.type == "admin":
logger.info(f'updating lock {user.username} / {request_json["locked"]}')
user.locked = request_json['locked']
2019-08-03 21:35:08 +01:00
if 'spotify_linked' in request_json:
2020-04-30 14:54:05 +01:00
logger.info(f'deauthing {user.username}')
if request_json['spotify_linked'] is False:
2020-04-30 14:54:05 +01:00
user.access_token = None
user.refresh_token = None
user.spotify_linked = False
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']
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
return jsonify({'message': 'account updated', 'status': 'succeeded'}), 200
2019-08-03 21:35:08 +01:00
@blueprint.route('/users', methods=['GET'])
@login_or_basic_auth
@admin_required
def all_users_route(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
@blueprint.route('/user/password', methods=['POST'])
@login_required
2020-04-30 14:54:05 +01:00
def change_password(user=None):
request_json = request.get_json()
if 'new_password' in request_json and 'current_password' in request_json:
if len(request_json['new_password']) == 0:
return jsonify({"error": 'zero length password'}), 400
if len(request_json['new_password']) > 30:
return jsonify({"error": 'password too long'}), 400
2020-04-30 14:54:05 +01:00
if user.check_password(request_json['current_password']):
user.password = generate_password_hash(request_json['new_password'])
user.update()
logger.info(f'password udpated {user.username}')
return jsonify({"message": 'password changed', "status": "success"}), 200
else:
2020-04-30 14:54:05 +01:00
logger.warning(f"incorrect password {user.username}")
return jsonify({'error': 'wrong password provided'}), 401
else:
return jsonify({'error': 'malformed request, no old_password/new_password'}), 400
@blueprint.route('/playlist/run', methods=['GET'])
@login_or_basic_auth
2020-04-30 14:54:05 +01:00
def run_playlist(user=None):
playlist_name = request.args.get('name', None)
if playlist_name:
if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD':
run_user_playlist_task(user.username, playlist_name)
else:
2020-04-30 14:54:05 +01:00
run_user_playlist(user.username, playlist_name)
return jsonify({'message': 'execution requested', 'status': 'success'}), 200
else:
logger.warning('no playlist requested')
return jsonify({"error": 'no name requested'}), 400
@blueprint.route('/playlist/run/task', methods=['POST'])
@cloud_task
def run_playlist_task():
payload = request.get_data(as_text=True)
if payload:
payload = json.loads(payload)
logger.info(f'running {payload["username"]} / {payload["name"]}')
run_user_playlist(payload['username'], payload['name'])
return jsonify({'message': 'executed playlist', 'status': 'success'}), 200
@blueprint.route('/playlist/run/user', methods=['GET'])
@login_or_basic_auth
2020-04-30 14:54:05 +01:00
def run_user(user=None):
2020-04-30 14:54:05 +01:00
if user.type == 'admin':
user_name = request.args.get('username', user.username)
else:
2020-04-30 14:54:05 +01:00
user_name = user.username
update_playlists(user_name)
return jsonify({'message': 'executed user', 'status': 'success'}), 200
@blueprint.route('/playlist/run/user/task', methods=['POST'])
@cloud_task
def run_user_task():
payload = request.get_data(as_text=True)
if payload:
update_playlists(payload)
return jsonify({'message': 'executed user', 'status': 'success'}), 200
@blueprint.route('/playlist/run/users', methods=['GET'])
@login_or_basic_auth
@admin_required
2020-04-30 14:54:05 +01:00
def run_users(user=None):
update_all_user_playlists()
return jsonify({'message': 'executed all users', 'status': 'success'}), 200
@blueprint.route('/playlist/run/users/cron', methods=['GET'])
@gae_cron
def run_users_cron():
update_all_user_playlists()
return jsonify({'status': 'success'}), 200
2020-03-07 21:53:52 +00:00
@blueprint.route('/playlist/image', methods=['GET'])
@login_or_basic_auth
2020-04-30 14:54:05 +01:00
def image(user=None):
2020-03-07 21:53:52 +00:00
name = request.args.get('name', None)
if name is None:
return jsonify({'error': "no name provided"}), 400
2020-04-30 14:54:05 +01:00
_playlist = Playlist.collection.parent(user.key).filter('name', '==', name).get()
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
spotify_playlist = net.get_playlist(uri_string=_playlist.uri)
if spotify_playlist is None:
return jsonify({'error': "no spotify playlist returned"}), 404
return jsonify({'images': spotify_playlist.images, 'status': 'success'}), 200