updated with new service reflecting spotframework model

This commit is contained in:
aj 2020-06-21 15:30:51 +01:00
parent 2b009eb6df
commit 3f142e140e
5 changed files with 19 additions and 15 deletions

View File

@ -20,7 +20,7 @@ logger = logging.getLogger(__name__)
@admin_required
def get_tasks(user=None):
tasks = [i for i in tasker.list_tasks(task_path)]
tasks = list(tasker.list_tasks(task_path))
urls = {}
for task in tasks:

View File

@ -9,7 +9,7 @@ from datetime import datetime
from music.api.decorators import login_required, login_or_basic_auth, admin_required, gae_cron, cloud_task
from music.cloud.tasks import update_all_user_playlists, update_playlists, run_user_playlist_task
from music.tasks.run_user_playlist import run_user_playlist as run_user_playlist
from music.tasks.run_user_playlist import run_user_playlist
from music.model.user import User
from music.model.playlist import Playlist
@ -102,7 +102,7 @@ def playlist_route(user=None):
if playlist is not None:
return jsonify({'error': 'playlist already exists'}), 400
from music.tasks.create_playlist import create_playlist as create_playlist
from music.tasks.create_playlist import create_playlist
new_db_playlist = Playlist(parent=user.key)

View File

@ -5,9 +5,8 @@ import logging
from music.api.decorators import login_or_basic_auth, spotify_link_required
import music.db.database as database
from spotframework.model.track import SpotifyTrack
from spotframework.model.track import TrackFull, Context
from spotframework.model.uri import Uri
from spotframework.model.service import Context
from spotframework.player.player import Player
blueprint = Blueprint('player_api', __name__)
@ -57,13 +56,13 @@ def play(user=None):
elif 'tracks' in request_json:
try:
uris = [Uri(i) for i in request_json['tracks']]
uris = [SpotifyTrack.wrap(uri=i) for i in uris if i.object_type == Uri.ObjectType.track]
# TODO check uri object type
if len(uris) > 0:
net = database.get_authed_spotify_network(user)
player = Player(net)
player.play(tracks=uris, device_name=request_json.get('device_name', None))
player.play(uris=uris, device_name=request_json.get('device_name', None))
logger.info(f'played tracks')
return jsonify({'message': 'played', 'status': 'success'}), 200

View File

@ -10,12 +10,12 @@ logger = logging.getLogger(__name__)
def create_playlist(user, name):
logger.info(f'creating spotify playlist for {user.username} / {name}')
if user is None:
logger.error(f'{user.username} not provided')
logger.error(f'username not provided')
return
logger.info(f'creating spotify playlist for {user.username} / {name}')
net = database.get_authed_spotify_network(user)
playlist = net.create_playlist(net.user.username, name)

View File

@ -4,7 +4,7 @@ import random
import spotframework.util.monthstrings as monthstrings
from spotframework.model.uri import Uri
from spotframework.filter import remove_local
from spotframework.filter import remove_local, get_track_objects
from spotframework.filter.added import added_after
from spotframework.filter.sort import sort_by_release_date
from spotframework.filter.deduplicate import deduplicate_by_name
@ -132,12 +132,16 @@ def run_user_playlist(username, playlist_name):
# RECOMMENDATIONS
if playlist.include_recommendations:
recommendations = net.get_recommendations(tracks=[i.uri.object_id for i
in random.sample(playlist_tracks, k=min(5, len(playlist_tracks)))
recommendations = net.get_recommendations(tracks=[i.uri.object_id for i, j
in zip(*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)
if recommendations and len(recommendations) > 0:
playlist_tracks += recommendations
if recommendations and len(recommendations.tracks) > 0:
playlist_tracks += recommendations.tracks
else:
logger.error(f'error getting recommendations {username} / {playlist_name}')
@ -145,7 +149,8 @@ def run_user_playlist(username, playlist_name):
playlist_tracks = deduplicate_by_name(playlist_tracks)
# EXECUTE
resp = net.replace_playlist_tracks(uri_string=playlist.uri, uris=[i.uri for i in playlist_tracks])
resp = net.replace_playlist_tracks(uri_string=playlist.uri, uris=[i.uri for i, j
in zip(*get_track_objects(playlist_tracks))])
if resp:
if playlist.description_overwrite:
string = playlist.description_overwrite