2019-10-07 12:21:26 +01:00
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
import logging
|
2019-10-19 20:35:37 +01:00
|
|
|
import json
|
|
|
|
import os
|
2019-10-07 12:21:26 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
from music.api.decorators import admin_required, login_or_basic_auth, lastfm_username_required, \
|
2020-08-13 19:50:21 +01:00
|
|
|
spotify_link_required, cloud_task, validate_args
|
2019-10-19 17:14:11 +01:00
|
|
|
import music.db.database as database
|
2020-05-15 23:25:19 +01:00
|
|
|
from music.cloud.tasks import refresh_all_user_playlist_stats, refresh_user_playlist_stats, refresh_playlist_task
|
2019-10-20 17:28:05 +01:00
|
|
|
from music.tasks.refresh_lastfm_stats import refresh_lastfm_track_stats, \
|
|
|
|
refresh_lastfm_album_stats, \
|
|
|
|
refresh_lastfm_artist_stats
|
2019-10-07 12:21:26 +01:00
|
|
|
|
|
|
|
from spotfm.maths.counter import Counter
|
|
|
|
from spotframework.model.uri import Uri
|
2020-06-22 20:21:54 +01:00
|
|
|
from spotframework.net.network import SpotifyNetworkException
|
2019-10-07 12:21:26 +01:00
|
|
|
|
|
|
|
blueprint = Blueprint('spotfm-api', __name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/count', methods=['GET'])
|
|
|
|
@login_or_basic_auth
|
|
|
|
@spotify_link_required
|
|
|
|
@lastfm_username_required
|
2020-04-30 14:54:05 +01:00
|
|
|
def count(user=None):
|
2019-10-07 12:21:26 +01:00
|
|
|
|
|
|
|
uri = request.args.get('uri', None)
|
|
|
|
playlist_name = request.args.get('playlist_name', None)
|
|
|
|
|
|
|
|
if uri is None and playlist_name is None:
|
|
|
|
return jsonify({'error': 'no input provided'}), 401
|
|
|
|
|
|
|
|
if uri:
|
|
|
|
try:
|
|
|
|
uri = Uri(uri)
|
|
|
|
except ValueError:
|
|
|
|
return jsonify({'error': 'malformed uri provided'}), 401
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
spotnet = database.get_authed_spotify_network(user)
|
|
|
|
fmnet = database.get_authed_lastfm_network(user)
|
2019-10-07 12:21:26 +01:00
|
|
|
counter = Counter(fmnet=fmnet, spotnet=spotnet)
|
|
|
|
|
|
|
|
if uri:
|
|
|
|
uri_count = counter.count(uri=uri)
|
|
|
|
return jsonify({
|
|
|
|
"uri": str(uri),
|
|
|
|
"count": uri_count,
|
|
|
|
'uri_type': str(uri.object_type),
|
|
|
|
'last.fm_username': fmnet.username
|
|
|
|
}), 200
|
|
|
|
elif playlist_name:
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
playlists = spotnet.playlists()
|
2020-06-22 20:21:54 +01:00
|
|
|
playlist = next((i for i in playlists if i.name == playlist_name), None)
|
2019-10-07 12:21:26 +01:00
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
if playlist is not None:
|
|
|
|
playlist_count = counter.count_playlist(playlist=playlist)
|
|
|
|
return jsonify({
|
|
|
|
"count": playlist_count,
|
|
|
|
'playlist_name': playlist_name,
|
|
|
|
'last.fm_username': fmnet.username
|
|
|
|
}), 200
|
|
|
|
else:
|
|
|
|
return jsonify({'error': f'playlist {playlist_name} not found'}), 404
|
|
|
|
except SpotifyNetworkException:
|
2020-07-01 11:03:43 +01:00
|
|
|
logger.exception(f'error occured during {user.username} playlists retrieval')
|
2019-10-07 12:21:26 +01:00
|
|
|
return jsonify({'error': f'playlist {playlist_name} not found'}), 404
|
2019-10-19 20:35:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/playlist/refresh', methods=['GET'])
|
|
|
|
@login_or_basic_auth
|
|
|
|
@spotify_link_required
|
|
|
|
@lastfm_username_required
|
2020-07-29 10:45:40 +01:00
|
|
|
@validate_args(('name', str))
|
2020-04-30 14:54:05 +01:00
|
|
|
def playlist_refresh(user=None):
|
2019-10-19 20:35:37 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
playlist_name = request.args['name']
|
2019-10-19 20:35:37 +01:00
|
|
|
|
2020-07-29 10:45:40 +01:00
|
|
|
if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD':
|
|
|
|
refresh_playlist_task(user.username, playlist_name)
|
2019-10-19 20:35:37 +01:00
|
|
|
else:
|
2020-07-29 10:45:40 +01:00
|
|
|
refresh_lastfm_track_stats(user.username, playlist_name)
|
|
|
|
refresh_lastfm_album_stats(user.username, playlist_name)
|
|
|
|
refresh_lastfm_artist_stats(user.username, playlist_name)
|
|
|
|
|
|
|
|
return jsonify({'message': 'execution requested', 'status': 'success'}), 200
|
2019-10-19 20:35:37 +01:00
|
|
|
|
|
|
|
|
2019-10-20 17:28:05 +01:00
|
|
|
@blueprint.route('/playlist/refresh/task/track', methods=['POST'])
|
2019-10-19 20:35:37 +01:00
|
|
|
@cloud_task
|
2019-10-20 17:28:05 +01:00
|
|
|
def run_playlist_track_task():
|
2019-10-19 20:35:37 +01:00
|
|
|
|
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
|
|
|
payload = json.loads(payload)
|
|
|
|
|
2019-10-20 17:28:05 +01:00
|
|
|
logger.info(f'refreshing tracks {payload["username"]} / {payload["name"]}')
|
2019-10-19 20:35:37 +01:00
|
|
|
|
2019-10-20 17:28:05 +01:00
|
|
|
refresh_lastfm_track_stats(payload['username'], payload['name'])
|
|
|
|
|
|
|
|
return jsonify({'message': 'executed playlist', 'status': 'success'}), 200
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/playlist/refresh/task/album', methods=['POST'])
|
|
|
|
@cloud_task
|
|
|
|
def run_playlist_album_task():
|
|
|
|
|
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
|
|
|
payload = json.loads(payload)
|
|
|
|
|
|
|
|
logger.info(f'refreshing albums {payload["username"]} / {payload["name"]}')
|
|
|
|
|
|
|
|
refresh_lastfm_album_stats(payload['username'], payload['name'])
|
|
|
|
|
|
|
|
return jsonify({'message': 'executed playlist', 'status': 'success'}), 200
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/playlist/refresh/task/artist', methods=['POST'])
|
|
|
|
@cloud_task
|
|
|
|
def run_playlist_artist_task():
|
|
|
|
|
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
|
|
|
payload = json.loads(payload)
|
|
|
|
|
|
|
|
logger.info(f'refreshing artists {payload["username"]} / {payload["name"]}')
|
|
|
|
|
|
|
|
refresh_lastfm_artist_stats(payload['username'], payload['name'])
|
2019-10-19 20:35:37 +01:00
|
|
|
|
|
|
|
return jsonify({'message': 'executed playlist', 'status': 'success'}), 200
|
|
|
|
|
|
|
|
|
2019-10-20 01:27:59 +01:00
|
|
|
@blueprint.route('/playlist/refresh/users', methods=['GET'])
|
|
|
|
@login_or_basic_auth
|
|
|
|
@admin_required
|
2020-04-30 14:54:05 +01:00
|
|
|
def run_users(user=None):
|
2020-05-15 23:25:19 +01:00
|
|
|
refresh_all_user_playlist_stats()
|
2019-10-20 01:27:59 +01:00
|
|
|
return jsonify({'message': 'executed all users', 'status': 'success'}), 200
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/playlist/refresh/user', methods=['GET'])
|
|
|
|
@login_or_basic_auth
|
2020-04-30 14:54:05 +01:00
|
|
|
def run_user(user=None):
|
2019-10-20 01:27:59 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
if user.type == 'admin':
|
|
|
|
user_name = request.args.get('username', user.username)
|
2019-10-20 01:27:59 +01:00
|
|
|
else:
|
2020-04-30 14:54:05 +01:00
|
|
|
user_name = user.username
|
2019-10-20 01:27:59 +01:00
|
|
|
|
2020-05-15 23:25:19 +01:00
|
|
|
refresh_user_playlist_stats(user_name)
|
2019-10-20 01:27:59 +01:00
|
|
|
|
|
|
|
return jsonify({'message': 'executed user', 'status': 'success'}), 200
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/playlist/refresh/user/task', methods=['POST'])
|
|
|
|
@cloud_task
|
|
|
|
def run_user_task():
|
|
|
|
|
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
2020-05-15 23:25:19 +01:00
|
|
|
refresh_user_playlist_stats(payload)
|
2019-10-20 01:27:59 +01:00
|
|
|
return jsonify({'message': 'executed user', 'status': 'success'}), 200
|