2019-08-10 17:53:50 +01:00
|
|
|
import datetime
|
|
|
|
import logging
|
2020-06-16 20:45:26 +01:00
|
|
|
import random
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
import spotframework.util.monthstrings as monthstrings
|
2019-09-15 15:33:29 +01:00
|
|
|
from spotframework.model.uri import Uri
|
2020-06-21 15:30:51 +01:00
|
|
|
from spotframework.filter import remove_local, get_track_objects
|
2020-06-16 20:45:26 +01:00
|
|
|
from spotframework.filter.added import added_after
|
|
|
|
from spotframework.filter.sort import sort_by_release_date
|
|
|
|
from spotframework.filter.deduplicate import deduplicate_by_name
|
2020-06-22 20:21:54 +01:00
|
|
|
from spotframework.net.network import SpotifyNetworkException
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2021-03-24 10:06:54 +00:00
|
|
|
from spotframework.net.network import Network as SpotNetwork
|
2020-05-04 22:03:06 +01:00
|
|
|
from fmframework.net.network import Network
|
2020-08-12 09:30:26 +01:00
|
|
|
from spotfm.chart import map_lastfm_track_chart_to_spotify
|
2020-05-04 22:03:06 +01:00
|
|
|
|
2019-10-19 17:14:11 +01:00
|
|
|
import music.db.database as database
|
|
|
|
from music.db.part_generator import PartGenerator
|
2020-04-30 14:54:05 +01:00
|
|
|
from music.model.user import User
|
|
|
|
from music.model.playlist import Playlist
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2022-12-09 08:37:05 +00:00
|
|
|
from music.notif.notifier import notify_user_playlist_update
|
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2019-08-10 17:53:50 +01:00
|
|
|
|
|
|
|
|
2021-03-24 10:06:54 +00:00
|
|
|
def run_user_playlist(user: User, playlist: Playlist, spotnet: SpotNetwork = None, fmnet: Network = None) -> None:
|
|
|
|
"""Generate and upadate a user's smart playlist
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user (User): Subject user
|
|
|
|
playlist (Playlist): User's subject playlist
|
|
|
|
spotnet (SpotNetwork, optional): Spotframework network for Spotify operations. Defaults to None.
|
|
|
|
fmnet (Network, optional): Fmframework network for Last.fm operations. Defaults to None.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
NameError: No user provided
|
|
|
|
NameError: No playlist provided
|
|
|
|
AttributeError: Playlist has no URI
|
|
|
|
NameError: No spotframework network available
|
|
|
|
e: spotframework error when retrieving user playlists
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
[type]: [description]
|
|
|
|
"""
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
# PRE-RUN CHECKS
|
2020-05-08 15:19:27 +01:00
|
|
|
|
2021-02-08 16:18:16 +00:00
|
|
|
if isinstance(user, str):
|
|
|
|
username = user
|
|
|
|
user = User.collection.filter('username', '==', username.strip().lower()).get()
|
|
|
|
else:
|
|
|
|
username = user.username
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2021-02-08 16:18:16 +00:00
|
|
|
if user is None:
|
|
|
|
logger.error(f'user {username} not found')
|
|
|
|
raise NameError(f'User {username} not found')
|
|
|
|
|
|
|
|
if isinstance(playlist, str):
|
|
|
|
playlist_name = playlist
|
2021-06-16 20:40:14 +01:00
|
|
|
playlist = user.get_playlist(playlist_name)
|
|
|
|
|
2021-02-08 16:18:16 +00:00
|
|
|
else:
|
2021-06-16 20:40:14 +01:00
|
|
|
playlist_name = playlist.name
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if playlist.uri is None:
|
2020-06-16 20:45:26 +01:00
|
|
|
logger.critical(f'no playlist id to populate {username} / {playlist_name}')
|
2021-02-08 16:18:16 +00:00
|
|
|
raise AttributeError(f'No URI for {playlist_name} ({username})')
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
# END CHECKS
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2021-02-08 16:18:16 +00:00
|
|
|
logger.info(f'running {username} / {playlist_name}')
|
|
|
|
|
|
|
|
if spotnet is None:
|
|
|
|
spotnet = database.get_authed_spotify_network(user)
|
2020-05-08 15:19:27 +01:00
|
|
|
|
2021-02-08 16:18:16 +00:00
|
|
|
if spotnet is None:
|
2020-06-16 20:45:26 +01:00
|
|
|
logger.error(f'no spotify network returned for {username} / {playlist_name}')
|
2021-02-08 16:18:16 +00:00
|
|
|
raise NameError(f'No Spotify network returned ({username} / {playlist_name})')
|
2020-05-08 15:19:27 +01:00
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2021-07-10 16:00:30 +01:00
|
|
|
if not playlist.include_spotify_owned:
|
2022-11-09 08:56:23 +00:00
|
|
|
user_playlists = {i.name: i.uri for i in spotnet.playlists() if 'spotify' not in i.owner.display_name.lower()}
|
2021-07-10 16:00:30 +01:00
|
|
|
else:
|
2022-11-09 08:56:23 +00:00
|
|
|
user_playlists = {i.name: i.uri for i in spotnet.playlists()}
|
2021-02-08 16:18:16 +00:00
|
|
|
except SpotifyNetworkException as e:
|
2020-07-01 11:03:43 +01:00
|
|
|
logger.exception(f'error occured while retrieving playlists {username} / {playlist_name}')
|
2021-02-08 16:18:16 +00:00
|
|
|
raise e
|
2020-06-16 20:45:26 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
part_generator = PartGenerator(user=user)
|
2020-06-16 20:45:26 +01:00
|
|
|
part_names = part_generator.get_recursive_parts(playlist.name)
|
2019-09-23 23:12:26 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
playlist_tracks = []
|
2019-09-23 23:12:26 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
if playlist.add_last_month:
|
|
|
|
part_names.append(monthstrings.get_last_month())
|
|
|
|
if playlist.add_this_month:
|
|
|
|
part_names.append(monthstrings.get_this_month())
|
2019-09-23 23:12:26 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
# LOAD PLAYLIST TRACKS
|
|
|
|
for part_name in part_names:
|
|
|
|
try: # attempt to cast to uri
|
|
|
|
uri = Uri(part_name)
|
2020-08-12 09:30:26 +01:00
|
|
|
log_name = uri
|
2019-11-01 23:46:49 +00:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
except ValueError: # is a playlist name
|
2022-11-09 22:22:33 +00:00
|
|
|
uri = user_playlists.get(part_name)
|
|
|
|
if uri is None:
|
2020-06-16 20:45:26 +01:00
|
|
|
logger.warning(f'playlist {part_name} not found {username} / {playlist_name}')
|
|
|
|
continue
|
2022-11-09 22:22:33 +00:00
|
|
|
|
|
|
|
log_name = part_name
|
2020-08-12 09:30:26 +01:00
|
|
|
|
|
|
|
try:
|
2021-06-05 09:52:31 +01:00
|
|
|
_tracks = spotnet.playlist_tracks(uri=uri, reduced_mem=True)
|
2020-08-12 09:30:26 +01:00
|
|
|
if _tracks and len(_tracks) > 0:
|
|
|
|
playlist_tracks += _tracks
|
|
|
|
else:
|
|
|
|
logger.warning(f'no tracks returned for {log_name} {username} / {playlist_name}')
|
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error occured while retrieving {log_name} {username} / {playlist_name}')
|
2020-06-16 20:45:26 +01:00
|
|
|
|
2020-08-12 09:30:26 +01:00
|
|
|
playlist_tracks = list(remove_local(playlist_tracks))
|
2020-06-16 20:45:26 +01:00
|
|
|
|
|
|
|
# LIBRARY
|
2020-02-24 18:15:38 +00:00
|
|
|
if playlist.include_library_tracks:
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2021-02-08 16:18:16 +00:00
|
|
|
library_tracks = spotnet.saved_tracks()
|
2020-06-22 20:21:54 +01:00
|
|
|
if library_tracks and len(library_tracks) > 0:
|
|
|
|
playlist_tracks += library_tracks
|
|
|
|
else:
|
|
|
|
logger.error(f'error getting library tracks {username} / {playlist_name}')
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error occured while retrieving library tracks {username} / {playlist_name}')
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
# PLAYLIST TYPE SPECIFIC
|
|
|
|
if playlist.type == 'recents':
|
|
|
|
boundary_date = datetime.datetime.now(datetime.timezone.utc) - \
|
|
|
|
datetime.timedelta(days=int(playlist.day_boundary))
|
2020-08-12 09:30:26 +01:00
|
|
|
playlist_tracks = list(added_after(playlist_tracks, boundary_date))
|
2020-06-16 20:45:26 +01:00
|
|
|
elif playlist.type == 'fmchart':
|
2020-02-24 18:15:38 +00:00
|
|
|
if user.lastfm_username is None:
|
2020-06-16 20:45:26 +01:00
|
|
|
logger.error(f'no associated last.fm username, chart source skipped {username} / {playlist_name}')
|
2020-02-24 18:15:38 +00:00
|
|
|
else:
|
2020-05-04 22:03:06 +01:00
|
|
|
chart_range = Network.Range.MONTH
|
|
|
|
try:
|
|
|
|
chart_range = Network.Range[playlist.chart_range]
|
|
|
|
except KeyError:
|
2020-06-16 20:45:26 +01:00
|
|
|
logger.error(f'invalid last.fm chart range found {playlist.chart_range}, '
|
|
|
|
f'defaulting to 1 month {username} / {playlist_name}')
|
2021-02-08 16:18:16 +00:00
|
|
|
|
|
|
|
if fmnet is None:
|
|
|
|
fmnet = database.get_authed_lastfm_network(user)
|
2020-06-16 20:45:26 +01:00
|
|
|
|
|
|
|
if fmnet is not None:
|
2021-02-08 16:18:16 +00:00
|
|
|
chart_tracks = map_lastfm_track_chart_to_spotify(spotnet=spotnet,
|
2020-08-12 09:30:26 +01:00
|
|
|
fmnet=fmnet,
|
|
|
|
period=chart_range,
|
|
|
|
limit=playlist.chart_limit)
|
2020-06-16 20:45:26 +01:00
|
|
|
|
|
|
|
if chart_tracks is not None and len(chart_tracks) > 0:
|
|
|
|
playlist_tracks += chart_tracks
|
|
|
|
else:
|
|
|
|
logger.error(f'no tracks returned {username} / {playlist_name}')
|
|
|
|
else:
|
|
|
|
logger.error(f'no last.fm network returned {username} / {playlist_name}')
|
|
|
|
|
|
|
|
# SORT METHOD
|
|
|
|
if playlist.shuffle:
|
|
|
|
random.shuffle(playlist_tracks)
|
|
|
|
elif playlist.type != 'fmchart':
|
|
|
|
playlist_tracks = sort_by_release_date(tracks=playlist_tracks, reverse=True)
|
|
|
|
|
|
|
|
# RECOMMENDATIONS
|
|
|
|
if playlist.include_recommendations:
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2021-02-08 16:18:16 +00:00
|
|
|
recommendations = spotnet.recommendations(tracks=[i.uri.object_id for i, j
|
2020-08-12 09:30:26 +01:00
|
|
|
in get_track_objects(
|
|
|
|
random.sample(playlist_tracks,
|
|
|
|
k=min(5, len(playlist_tracks))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if i.uri.object_type == Uri.ObjectType.track],
|
|
|
|
response_limit=playlist.recommendation_sample)
|
2020-06-22 20:21:54 +01:00
|
|
|
if recommendations and len(recommendations.tracks) > 0:
|
|
|
|
playlist_tracks += recommendations.tracks
|
|
|
|
else:
|
|
|
|
logger.error(f'error getting recommendations {username} / {playlist_name}')
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error occured while generating recommendations {username} / {playlist_name}')
|
2020-05-04 22:03:06 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
# DEDUPLICATE
|
|
|
|
playlist_tracks = deduplicate_by_name(playlist_tracks)
|
2019-08-19 00:52:02 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
# EXECUTE
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2021-02-08 16:18:16 +00:00
|
|
|
spotnet.replace_playlist_tracks(uri=playlist.uri, uris=[i.uri for i, j in get_track_objects(playlist_tracks)])
|
2020-06-22 20:21:54 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
if playlist.description_overwrite:
|
|
|
|
string = playlist.description_overwrite
|
2020-02-24 18:15:38 +00:00
|
|
|
else:
|
2020-06-16 20:45:26 +01:00
|
|
|
string = ' / '.join(sorted(part_names))
|
2020-02-24 18:15:38 +00:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
if playlist.description_suffix:
|
|
|
|
string += f' - {str(playlist.description_suffix)}'
|
2019-08-19 00:52:02 +01:00
|
|
|
|
2020-06-16 20:45:26 +01:00
|
|
|
if string is None or len(string) == 0:
|
|
|
|
logger.error(f'no string generated {username} / {playlist_name}')
|
|
|
|
return None
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2021-02-08 16:18:16 +00:00
|
|
|
spotnet.change_playlist_details(uri=playlist.uri, description=string)
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error changing description for {username} / {playlist_name}')
|
2020-06-22 20:21:54 +01:00
|
|
|
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error executing {username} / {playlist_name}')
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
playlist.last_updated = datetime.datetime.utcnow()
|
2020-04-30 14:54:05 +01:00
|
|
|
playlist.update()
|
2022-12-09 08:37:05 +00:00
|
|
|
|
|
|
|
notify_user_playlist_update(user=user, playlist=playlist)
|