2019-08-12 00:34:04 +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
|
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-12 00:34:04 +01:00
|
|
|
|
2019-09-15 03:34:00 +01:00
|
|
|
from spotframework.player.player import Player
|
2019-10-19 17:14:11 +01:00
|
|
|
import music.db.database as database
|
|
|
|
from music.db.part_generator import PartGenerator
|
2019-08-26 18:31:21 +01:00
|
|
|
|
2019-08-12 00:34:04 +01:00
|
|
|
db = firestore.Client()
|
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-08-12 00:34:04 +01:00
|
|
|
|
|
|
|
def play_user_playlist(username,
|
|
|
|
playlist_type='default',
|
|
|
|
parts=None,
|
|
|
|
playlists=None,
|
|
|
|
shuffle=False,
|
|
|
|
include_recommendations=True,
|
|
|
|
recommendation_sample=10,
|
2019-08-17 18:30:13 +01:00
|
|
|
day_boundary=10,
|
|
|
|
add_this_month=False,
|
2019-09-25 19:28:38 +01:00
|
|
|
add_last_month=False,
|
|
|
|
device_name=None):
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-10-23 14:44:17 +01:00
|
|
|
user = database.get_user(username)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger.info(f'playing for {username}')
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if user is None:
|
|
|
|
logger.critical(f'{username} not found')
|
|
|
|
return
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if parts is None and playlists is None:
|
|
|
|
logger.critical(f'no playlists to use for creation ({username})')
|
|
|
|
return None
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if parts is None:
|
|
|
|
parts = []
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if playlists is None:
|
|
|
|
playlists = []
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if len(parts) == 0 and len(playlists) == 0:
|
|
|
|
logger.critical(f'no playlists to use for creation ({username})')
|
|
|
|
return None
|
2019-09-25 19:28:38 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
net = database.get_authed_spotify_network(username)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
device = None
|
|
|
|
if device_name:
|
|
|
|
devices = net.get_available_devices()
|
|
|
|
if devices and len(devices) > 0:
|
|
|
|
device = next((i for i in devices if i.name == device_name), None)
|
|
|
|
if device is None:
|
|
|
|
logger.error(f'error selecting device {device_name} to play on')
|
|
|
|
else:
|
|
|
|
logger.warning(f'no available devices to play')
|
2019-09-15 03:34:00 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
engine = PlaylistEngine(net)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
player = Player(net)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
processors = [DeduplicateByID()]
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if shuffle:
|
|
|
|
processors.append(Shuffle())
|
|
|
|
else:
|
|
|
|
processors.append(SortReleaseDate(reverse=True))
|
2019-08-26 18:31:21 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
submit_parts = parts
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
part_generator = PartGenerator(user=user)
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
for part in playlists:
|
|
|
|
submit_parts += part_generator.get_recursive_parts(part)
|
2019-09-23 23:12:26 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
submit_parts = [i for i in {j for j in submit_parts}]
|
2019-09-23 23:12:26 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
params = [
|
|
|
|
PlaylistSource.Params(names=submit_parts, processors=processors)
|
|
|
|
]
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if include_recommendations:
|
|
|
|
params.append(RecommendationSource.Params(recommendation_limit=int(recommendation_sample)))
|
2019-08-12 00:34:04 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if playlist_type == 'recents':
|
|
|
|
boundary_date = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=int(day_boundary))
|
|
|
|
tracks = engine.get_recent_playlist(params=params,
|
|
|
|
boundary_date=boundary_date,
|
|
|
|
add_this_month=add_this_month,
|
|
|
|
add_last_month=add_last_month)
|
2019-08-12 00:34:04 +01:00
|
|
|
else:
|
2020-02-24 18:15:38 +00:00
|
|
|
tracks = engine.make_playlist(params=params)
|
|
|
|
|
|
|
|
player.play(tracks=tracks, device=device)
|