From 5909229d696711523b9da5f103f260bc2ae15e6d Mon Sep 17 00:00:00 2001 From: aj Date: Mon, 23 Sep 2019 23:12:26 +0100 Subject: [PATCH] updated to playlist engine sources framework, added library tracks backend support --- spotify/auth/auth.py | 2 +- spotify/tasks/play_user_playlist.py | 22 +++++++++++----------- spotify/tasks/run_user_playlist.py | 27 ++++++++++++++++----------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/spotify/auth/auth.py b/spotify/auth/auth.py index 0bd46ae..f471c91 100644 --- a/spotify/auth/auth.py +++ b/spotify/auth/auth.py @@ -136,7 +136,7 @@ def auth(): { 'client_id': client_id, 'response_type': 'code', - 'scope': 'playlist-modify-public playlist-modify-private playlist-read-private user-modify-playback-state', + 'scope': 'playlist-modify-public playlist-modify-private playlist-read-private user-modify-playback-state user-library-read', 'redirect_uri': 'https://spotify.sarsoo.xyz/auth/spotify/token' } ) diff --git a/spotify/tasks/play_user_playlist.py b/spotify/tasks/play_user_playlist.py index edb6c3b..fa8cb2a 100644 --- a/spotify/tasks/play_user_playlist.py +++ b/spotify/tasks/play_user_playlist.py @@ -3,7 +3,7 @@ from google.cloud import firestore import datetime import logging -from spotframework.engine.playlistengine import PlaylistEngine +from spotframework.engine.playlistengine import PlaylistEngine, PlaylistSource, RecommendationSource from spotframework.engine.processor.shuffle import Shuffle from spotframework.engine.processor.sort import SortReleaseDate from spotframework.engine.processor.deduplicate import DeduplicateByID @@ -60,7 +60,6 @@ def play_user_playlist(username, user_dict['access_token'])) engine = PlaylistEngine(net) - engine.load_user_playlists() player = Player(net) @@ -80,20 +79,21 @@ def play_user_playlist(username, submit_parts = [i for i in {j for j in submit_parts}] + params = [ + PlaylistSource.Params(names=submit_parts, processors=processors) + ] + + if include_recommendations: + params.append(RecommendationSource.Params(recommendation_limit=int(recommendation_sample))) + if playlist_type == 'recents': boundary_date = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=int(day_boundary)) - tracks = engine.get_recent_playlist(boundary_date, - submit_parts, - processors, - include_recommendations=include_recommendations, - recommendation_limit=int(recommendation_sample), + tracks = engine.get_recent_playlist(params=params, + boundary_date=boundary_date, add_this_month=add_this_month, add_last_month=add_last_month) else: - tracks = engine.make_playlist(submit_parts, - processors, - include_recommendations=include_recommendations, - recommendation_limit=int(recommendation_sample)) + tracks = engine.make_playlist(params=params) player.play(tracks=tracks) diff --git a/spotify/tasks/run_user_playlist.py b/spotify/tasks/run_user_playlist.py index 813692c..b12ad7d 100644 --- a/spotify/tasks/run_user_playlist.py +++ b/spotify/tasks/run_user_playlist.py @@ -3,7 +3,7 @@ from google.cloud import firestore import datetime import logging -from spotframework.engine.playlistengine import PlaylistEngine +from spotframework.engine.playlistengine import PlaylistEngine, PlaylistSource, RecommendationSource, LibraryTrackSource from spotframework.engine.processor.shuffle import Shuffle from spotframework.engine.processor.sort import SortReleaseDate from spotframework.engine.processor.deduplicate import DeduplicateByID @@ -54,7 +54,6 @@ def run_user_playlist(username, playlist_name): user_dict['access_token'])) engine = PlaylistEngine(net) - engine.load_user_playlists() processors = [DeduplicateByID()] @@ -66,21 +65,27 @@ def run_user_playlist(username, playlist_name): part_generator = PartGenerator(user_id=users[0].id) submit_parts = part_generator.get_recursive_parts(playlist_dict['name']) + params = [ + PlaylistSource.Params(names=submit_parts) + ] + + if playlist_dict['include_recommendations']: + params.append(RecommendationSource.Params(recommendation_limit=playlist_dict['recommendation_sample'])) + + if playlist_dict.get('include_library_tracks', False): + params.append(LibraryTrackSource.Params()) + if playlist_dict['type'] == 'recents': boundary_date = datetime.datetime.now(datetime.timezone.utc) - \ datetime.timedelta(days=int(playlist_dict['day_boundary'])) - tracks = engine.get_recent_playlist(boundary_date, - submit_parts, - processors, - include_recommendations=playlist_dict['include_recommendations'], - recommendation_limit=int(playlist_dict['recommendation_sample']), + tracks = engine.get_recent_playlist(params=params, + processors=processors, + boundary_date=boundary_date, add_this_month=playlist_dict.get('add_this_month', False), add_last_month=playlist_dict.get('add_last_month', False)) else: - tracks = engine.make_playlist(submit_parts, - processors, - include_recommendations=playlist_dict['include_recommendations'], - recommendation_limit=int(playlist_dict['recommendation_sample'])) + tracks = engine.make_playlist(params=params, + processors=processors) engine.execute_playlist(tracks, Uri(playlist_dict['uri']))