From 3f142e140e0f4756bada04e326e175ee69727b75 Mon Sep 17 00:00:00 2001 From: aj Date: Sun, 21 Jun 2020 15:30:51 +0100 Subject: [PATCH] updated with new service reflecting spotframework model --- music/api/admin.py | 2 +- music/api/api.py | 4 ++-- music/api/player.py | 7 +++---- music/tasks/create_playlist.py | 4 ++-- music/tasks/run_user_playlist.py | 17 +++++++++++------ 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/music/api/admin.py b/music/api/admin.py index 1b0e292..63a7e61 100644 --- a/music/api/admin.py +++ b/music/api/admin.py @@ -20,7 +20,7 @@ logger = logging.getLogger(__name__) @admin_required def get_tasks(user=None): - tasks = [i for i in tasker.list_tasks(task_path)] + tasks = list(tasker.list_tasks(task_path)) urls = {} for task in tasks: diff --git a/music/api/api.py b/music/api/api.py index 49aa914..3c33856 100644 --- a/music/api/api.py +++ b/music/api/api.py @@ -9,7 +9,7 @@ from datetime import datetime 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 -from music.tasks.run_user_playlist import run_user_playlist as run_user_playlist +from music.tasks.run_user_playlist import run_user_playlist from music.model.user import User from music.model.playlist import Playlist @@ -102,7 +102,7 @@ def playlist_route(user=None): if playlist is not None: return jsonify({'error': 'playlist already exists'}), 400 - from music.tasks.create_playlist import create_playlist as create_playlist + from music.tasks.create_playlist import create_playlist new_db_playlist = Playlist(parent=user.key) diff --git a/music/api/player.py b/music/api/player.py index 8e39751..744c6db 100644 --- a/music/api/player.py +++ b/music/api/player.py @@ -5,9 +5,8 @@ import logging from music.api.decorators import login_or_basic_auth, spotify_link_required import music.db.database as database -from spotframework.model.track import SpotifyTrack +from spotframework.model.track import TrackFull, Context from spotframework.model.uri import Uri -from spotframework.model.service import Context from spotframework.player.player import Player blueprint = Blueprint('player_api', __name__) @@ -57,13 +56,13 @@ def play(user=None): elif 'tracks' in request_json: try: uris = [Uri(i) for i in request_json['tracks']] - uris = [SpotifyTrack.wrap(uri=i) for i in uris if i.object_type == Uri.ObjectType.track] + # TODO check uri object type if len(uris) > 0: net = database.get_authed_spotify_network(user) player = Player(net) - player.play(tracks=uris, device_name=request_json.get('device_name', None)) + player.play(uris=uris, device_name=request_json.get('device_name', None)) logger.info(f'played tracks') return jsonify({'message': 'played', 'status': 'success'}), 200 diff --git a/music/tasks/create_playlist.py b/music/tasks/create_playlist.py index 5ce9184..bee11b2 100644 --- a/music/tasks/create_playlist.py +++ b/music/tasks/create_playlist.py @@ -10,12 +10,12 @@ logger = logging.getLogger(__name__) def create_playlist(user, name): - logger.info(f'creating spotify playlist for {user.username} / {name}') if user is None: - logger.error(f'{user.username} not provided') + logger.error(f'username not provided') return + logger.info(f'creating spotify playlist for {user.username} / {name}') net = database.get_authed_spotify_network(user) playlist = net.create_playlist(net.user.username, name) diff --git a/music/tasks/run_user_playlist.py b/music/tasks/run_user_playlist.py index fc2a61f..567b9e7 100644 --- a/music/tasks/run_user_playlist.py +++ b/music/tasks/run_user_playlist.py @@ -4,7 +4,7 @@ import random import spotframework.util.monthstrings as monthstrings from spotframework.model.uri import Uri -from spotframework.filter import remove_local +from spotframework.filter import remove_local, get_track_objects from spotframework.filter.added import added_after from spotframework.filter.sort import sort_by_release_date from spotframework.filter.deduplicate import deduplicate_by_name @@ -132,12 +132,16 @@ def run_user_playlist(username, playlist_name): # RECOMMENDATIONS if playlist.include_recommendations: - recommendations = net.get_recommendations(tracks=[i.uri.object_id for i - in random.sample(playlist_tracks, k=min(5, len(playlist_tracks))) + recommendations = net.get_recommendations(tracks=[i.uri.object_id for i, j + in zip(*get_track_objects( + random.sample(playlist_tracks, + k=min(5, len(playlist_tracks)) + ) + )) if i.uri.object_type == Uri.ObjectType.track], response_limit=playlist.recommendation_sample) - if recommendations and len(recommendations) > 0: - playlist_tracks += recommendations + if recommendations and len(recommendations.tracks) > 0: + playlist_tracks += recommendations.tracks else: logger.error(f'error getting recommendations {username} / {playlist_name}') @@ -145,7 +149,8 @@ def run_user_playlist(username, playlist_name): playlist_tracks = deduplicate_by_name(playlist_tracks) # EXECUTE - resp = net.replace_playlist_tracks(uri_string=playlist.uri, uris=[i.uri for i in playlist_tracks]) + resp = net.replace_playlist_tracks(uri_string=playlist.uri, uris=[i.uri for i, j + in zip(*get_track_objects(playlist_tracks))]) if resp: if playlist.description_overwrite: string = playlist.description_overwrite