2019-08-10 17:53:50 +01:00
|
|
|
from google.cloud import firestore
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
|
2019-09-23 23:12:26 +01:00
|
|
|
from spotframework.engine.playlistengine import PlaylistEngine, PlaylistSource, RecommendationSource, LibraryTrackSource
|
2019-09-05 15:51:42 +01:00
|
|
|
from spotframework.engine.processor.shuffle import Shuffle
|
|
|
|
from spotframework.engine.processor.sort import SortReleaseDate
|
|
|
|
from spotframework.engine.processor.deduplicate import DeduplicateByID
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-09-15 15:33:29 +01:00
|
|
|
from spotframework.model.uri import Uri
|
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
from spotfm.engine.chart_source import ChartSource
|
|
|
|
|
2019-10-19 17:14:11 +01:00
|
|
|
import music.db.database as database
|
|
|
|
from music.db.part_generator import PartGenerator
|
2019-11-01 23:46:49 +00:00
|
|
|
from music.model.playlist import RecentsPlaylist, LastFMChartPlaylist
|
2019-08-10 17:53:50 +01:00
|
|
|
|
|
|
|
db = firestore.Client()
|
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
def run_user_playlist(username, playlist_name):
|
2019-10-23 14:44:17 +01:00
|
|
|
user = database.get_user(username)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger.info(f'running {username} / {playlist_name}')
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
if user:
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
playlist = database.get_playlist(username=username, name=playlist_name)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
if playlist is not None:
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
if playlist.uri is None:
|
2019-08-10 17:53:50 +01:00
|
|
|
logger.critical(f'no playlist id to populate ({username}/{playlist_name})')
|
2019-08-17 18:30:13 +01:00
|
|
|
return None
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-07 12:21:26 +01:00
|
|
|
net = database.get_authed_spotify_network(username)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
|
|
|
engine = PlaylistEngine(net)
|
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
if isinstance(playlist, LastFMChartPlaylist) and user.lastfm_username is not None:
|
|
|
|
engine.sources.append(ChartSource(spotnet=net, fmnet=database.get_authed_lastfm_network(user.username)))
|
|
|
|
|
2019-08-10 17:53:50 +01:00
|
|
|
processors = [DeduplicateByID()]
|
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
if not isinstance(playlist, LastFMChartPlaylist):
|
|
|
|
if playlist.shuffle is True:
|
|
|
|
processors.append(Shuffle())
|
|
|
|
else:
|
|
|
|
processors.append(SortReleaseDate(reverse=True))
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
part_generator = PartGenerator(user=user)
|
|
|
|
submit_parts = part_generator.get_recursive_parts(playlist.name)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-09-23 23:12:26 +01:00
|
|
|
params = [
|
|
|
|
PlaylistSource.Params(names=submit_parts)
|
|
|
|
]
|
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
if playlist.include_recommendations:
|
|
|
|
params.append(RecommendationSource.Params(recommendation_limit=playlist.recommendation_sample))
|
2019-09-23 23:12:26 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
if playlist.include_library_tracks:
|
2019-09-23 23:12:26 +01:00
|
|
|
params.append(LibraryTrackSource.Params())
|
|
|
|
|
2019-11-01 23:46:49 +00:00
|
|
|
if isinstance(playlist, LastFMChartPlaylist):
|
|
|
|
params.append(ChartSource.Params(chart_range=playlist.chart_range, limit=playlist.chart_limit))
|
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
if isinstance(playlist, RecentsPlaylist):
|
2019-09-15 15:33:29 +01:00
|
|
|
boundary_date = datetime.datetime.now(datetime.timezone.utc) - \
|
2019-10-23 14:44:17 +01:00
|
|
|
datetime.timedelta(days=int(playlist.day_boundary))
|
2019-09-23 23:12:26 +01:00
|
|
|
tracks = engine.get_recent_playlist(params=params,
|
|
|
|
processors=processors,
|
|
|
|
boundary_date=boundary_date,
|
2019-10-23 14:44:17 +01:00
|
|
|
add_this_month=playlist.add_this_month,
|
|
|
|
add_last_month=playlist.add_last_month)
|
2019-08-10 17:53:50 +01:00
|
|
|
else:
|
2019-09-23 23:12:26 +01:00
|
|
|
tracks = engine.make_playlist(params=params,
|
|
|
|
processors=processors)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
engine.execute_playlist(tracks, Uri(playlist.uri))
|
2019-08-19 00:52:02 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
overwrite = playlist.description_overwrite
|
|
|
|
suffix = playlist.description_suffix
|
2019-08-19 00:52:02 +01:00
|
|
|
|
|
|
|
engine.change_description(sorted(submit_parts),
|
2019-10-23 14:44:17 +01:00
|
|
|
uri=Uri(playlist.uri),
|
2019-08-19 00:52:02 +01:00
|
|
|
overwrite=overwrite,
|
|
|
|
suffix=suffix)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
|
|
|
else:
|
2019-10-23 14:44:17 +01:00
|
|
|
logger.critical(f'playlist not found ({username}/{playlist_name})')
|
2019-08-17 18:30:13 +01:00
|
|
|
return None
|
2019-08-10 17:53:50 +01:00
|
|
|
|
|
|
|
else:
|
2019-10-23 14:44:17 +01:00
|
|
|
logger.critical(f'{username} not found')
|