Mixonomer/music/api/spotfm.py

172 lines
5.4 KiB
Python
Raw Normal View History

from flask import Blueprint, jsonify, request
import logging
2019-10-19 20:35:37 +01:00
import json
import os
2020-07-29 10:45:40 +01:00
from music.api.decorators import admin_required, login_or_basic_auth, lastfm_username_required, \
spotify_link_required, cloud_task, gae_cron, validate_args
2019-10-19 17:14:11 +01:00
import music.db.database as database
from music.cloud.tasks import refresh_all_user_playlist_stats, refresh_user_playlist_stats, refresh_playlist_task
from music.tasks.refresh_lastfm_stats import refresh_lastfm_track_stats, \
refresh_lastfm_album_stats, \
refresh_lastfm_artist_stats
from spotfm.maths.counter import Counter
from spotframework.model.uri import Uri
from spotframework.net.network import SpotifyNetworkException
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):
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)
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:
try:
playlists = spotnet.get_playlists()
playlist = next((i for i in playlists if i.name == playlist_name), None)
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:
logger.exception(f'error occured during {user.username} playlists retrieval')
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
@blueprint.route('/playlist/refresh/task/track', methods=['POST'])
2019-10-19 20:35:37 +01:00
@cloud_task
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)
logger.info(f'refreshing tracks {payload["username"]} / {payload["name"]}')
2019-10-19 20:35:37 +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
@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):
refresh_all_user_playlist_stats()
return jsonify({'message': 'executed all users', 'status': 'success'}), 200
@blueprint.route('/playlist/refresh/users/cron', methods=['GET'])
@gae_cron
def run_users_task():
refresh_all_user_playlist_stats()
return jsonify({'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):
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
refresh_user_playlist_stats(user_name)
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:
refresh_user_playlist_stats(payload)
return jsonify({'message': 'executed user', 'status': 'success'}), 200