added recommendations to network and playlist engine

This commit is contained in:
aj 2019-08-05 18:31:00 +01:00
parent 5d285a3241
commit f17ac71168
4 changed files with 41 additions and 15 deletions

View File

@ -1,8 +1,3 @@
import main from spotframework.google.run_user_playlist import run_user_playlist as run_user_playlist
main.run_user_playlist({ run_user_playlist('andy', 'DNB')
'data': 'ELECTRONIC',
'attributes': {
'username': 'andy'
}
}, None)

View File

@ -23,7 +23,7 @@ class PlaylistEngine:
log.log(f"pulling tracks for {playlist.name}") log.log(f"pulling tracks for {playlist.name}")
playlist.tracks = self.net.get_playlist_tracks(playlist.playlistid) 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 = [] tracks = []
@ -51,10 +51,19 @@ class PlaylistEngine:
for processor in [i for i in processors if len(i.playlist_names) <= 0]: for processor in [i for i in processors if len(i.playlist_names) <= 0]:
tracks = processor.process(tracks) 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) # print(tracks)
return 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() this_month = monthstrings.get_this_month()
last_month = monthstrings.get_last_month() last_month = monthstrings.get_last_month()
@ -62,10 +71,13 @@ class PlaylistEngine:
processors.append(datefilter) 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): 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): def change_description(self, playlistparts, playlist_id):
self.net.change_playlist_details(playlist_id, description=' / '.join(playlistparts)) self.net.change_playlist_details(playlist_id, description=' / '.join(playlistparts))

View File

@ -25,8 +25,6 @@ def run_user_playlist(username, playlist_name):
playlist_collection = db.collection(u'spotify_users', u'{}'.format(users[0].id), 'playlists') 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()] playlists = [i for i in playlist_collection.where(u'name', u'==', playlist_name).stream()]
if len(playlists) == 1: if len(playlists) == 1:
@ -63,9 +61,16 @@ def run_user_playlist(username, playlist_name):
if playlist_dict['type'] == 'recents': if playlist_dict['type'] == 'recents':
boundary_date = datetime.datetime.now() - datetime.timedelta(days=int(playlist_dict['day_boundary'])) 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: 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.execute_playlist(tracks, playlist_dict['playlist_id'])
engine.change_description({i for i in submit_parts}, playlist_dict['playlist_id']) engine.change_description({i for i in submit_parts}, playlist_dict['playlist_id'])

View File

@ -1,4 +1,5 @@
import requests import requests
import random
from . import const from . import const
from spotframework.model.playlist import Playlist from spotframework.model.playlist import Playlist
import spotframework.log.log as log import spotframework.log.log as log
@ -277,3 +278,16 @@ class Network:
if len(uris) > 100: if len(uris) > 100:
self.add_playlist_tracks(playlistid, 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)