diff --git a/google_test.py b/google_test.py index 84ee011..ce78e93 100644 --- a/google_test.py +++ b/google_test.py @@ -1,8 +1,3 @@ -import main +from spotframework.google.run_user_playlist import run_user_playlist as run_user_playlist -main.run_user_playlist({ - 'data': 'ELECTRONIC', - 'attributes': { - 'username': 'andy' - } -}, None) \ No newline at end of file +run_user_playlist('andy', 'DNB') \ No newline at end of file diff --git a/spotframework/engine/playlistengine.py b/spotframework/engine/playlistengine.py index 6848f5f..9643181 100644 --- a/spotframework/engine/playlistengine.py +++ b/spotframework/engine/playlistengine.py @@ -23,7 +23,7 @@ class PlaylistEngine: log.log(f"pulling tracks for {playlist.name}") playlist.tracks = self.net.get_playlist_tracks(playlist.playlistid) - def make_playlist(self, playlist_parts, processors=[]): + def make_playlist(self, playlist_parts, processors=[], include_recommendations=False, recommendation_limit=10): tracks = [] @@ -51,10 +51,19 @@ class PlaylistEngine: for processor in [i for i in processors if len(i.playlist_names) <= 0]: tracks = processor.process(tracks) + tracks = [i['track'] for i in tracks] + + if include_recommendations: + try: + tracks += self.net.get_recommendations(tracks=[i['id'] for i in tracks], + response_limit=recommendation_limit)['tracks'] + except Exception as e: + print(e) + # print(tracks) return tracks - def get_recent_playlist(self, boundary_date, recent_playlist_parts, processors=[]): + def get_recent_playlist(self, boundary_date, recent_playlist_parts, processors=[], include_recommendations=False, recommendation_limit=10): this_month = monthstrings.get_this_month() last_month = monthstrings.get_last_month() @@ -62,10 +71,13 @@ class PlaylistEngine: processors.append(datefilter) - return self.make_playlist(recent_playlist_parts + [this_month, last_month], processors) + return self.make_playlist(recent_playlist_parts + [this_month, last_month], + processors, + include_recommendations=include_recommendations, + recommendation_limit=recommendation_limit) def execute_playlist(self, tracks, playlist_id): - self.net.replace_playlist_tracks(playlist_id, [i['track']['uri'] for i in tracks]) + self.net.replace_playlist_tracks(playlist_id, [i['uri'] for i in tracks]) def change_description(self, playlistparts, playlist_id): self.net.change_playlist_details(playlist_id, description=' / '.join(playlistparts)) diff --git a/spotframework/google/run_user_playlist.py b/spotframework/google/run_user_playlist.py index 22c5225..47737d6 100644 --- a/spotframework/google/run_user_playlist.py +++ b/spotframework/google/run_user_playlist.py @@ -25,8 +25,6 @@ def run_user_playlist(username, playlist_name): playlist_collection = db.collection(u'spotify_users', u'{}'.format(users[0].id), 'playlists') - print(user_dict['access_token'], user_dict['refresh_token']) - playlists = [i for i in playlist_collection.where(u'name', u'==', playlist_name).stream()] if len(playlists) == 1: @@ -63,9 +61,16 @@ def run_user_playlist(username, playlist_name): if playlist_dict['type'] == 'recents': boundary_date = datetime.datetime.now() - datetime.timedelta(days=int(playlist_dict['day_boundary'])) - tracks = engine.get_recent_playlist(boundary_date, submit_parts, processors) + tracks = engine.get_recent_playlist(boundary_date, + submit_parts, + processors, + include_recommendations=playlist_dict['include_recommendations'], + recommendation_limit=playlist_dict['recommendation_sample']) else: - tracks = engine.make_playlist(submit_parts, processors) + tracks = engine.make_playlist(submit_parts, + processors, + include_recommendations=playlist_dict['include_recommendations'], + recommendation_limit=playlist_dict['recommendation_sample']) engine.execute_playlist(tracks, playlist_dict['playlist_id']) engine.change_description({i for i in submit_parts}, playlist_dict['playlist_id']) diff --git a/spotframework/net/network.py b/spotframework/net/network.py index ce74fb8..cdc52ff 100644 --- a/spotframework/net/network.py +++ b/spotframework/net/network.py @@ -1,4 +1,5 @@ import requests +import random from . import const from spotframework.model.playlist import Playlist import spotframework.log.log as log @@ -277,3 +278,16 @@ class Network: if len(uris) > 100: self.add_playlist_tracks(playlistid, uris[100:]) + + def get_recommendations(self, tracks=None, artists=None, response_limit=10): + + params = {'limit': response_limit} + + if tracks: + random.shuffle(tracks) + params['seed_tracks'] = tracks[:100] + if artists: + random.shuffle(artists) + params['seed_artists'] = artists[:100] + + return self._make_get_request('getRecommendations', 'recommendations', params=params)