updated spotframework network actions to be tried and caught

This commit is contained in:
aj 2020-06-22 20:21:54 +01:00
parent 3f142e140e
commit c2b537e39f
8 changed files with 115 additions and 73 deletions

View File

@ -16,6 +16,8 @@ from music.model.playlist import Playlist
import music.db.database as database import music.db.database as database
from spotframework.net.network import SpotifyNetworkException
blueprint = Blueprint('api', __name__) blueprint = Blueprint('api', __name__)
db = firestore.Client() db = firestore.Client()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -352,7 +354,6 @@ def run_users_cron():
@blueprint.route('/playlist/image', methods=['GET']) @blueprint.route('/playlist/image', methods=['GET'])
@login_or_basic_auth @login_or_basic_auth
def image(user=None): def image(user=None):
name = request.args.get('name', None) name = request.args.get('name', None)
if name is None: if name is None:
@ -364,9 +365,7 @@ def image(user=None):
net = database.get_authed_spotify_network(user) net = database.get_authed_spotify_network(user)
spotify_playlist = net.get_playlist(uri_string=_playlist.uri) try:
return jsonify({'images': net.get_playlist(uri_string=_playlist.uri).images, 'status': 'success'}), 200
if spotify_playlist is None: except SpotifyNetworkException as e:
return jsonify({'error': "no spotify playlist returned"}), 404 return jsonify({'error': f"spotify error occured: {e.http_code}"}), 404
return jsonify({'images': spotify_playlist.images, 'status': 'success'}), 200

View File

@ -5,7 +5,8 @@ import logging
from music.api.decorators import login_or_basic_auth, spotify_link_required from music.api.decorators import login_or_basic_auth, spotify_link_required
import music.db.database as database import music.db.database as database
from spotframework.model.track import TrackFull, Context from spotframework.net.network import SpotifyNetworkException
from spotframework.model.track import Context
from spotframework.model.uri import Uri from spotframework.model.uri import Uri
from spotframework.player.player import Player from spotframework.player.player import Player
@ -39,8 +40,8 @@ def play(user=None):
return jsonify({'error': "malformed uri provided"}), 400 return jsonify({'error': "malformed uri provided"}), 400
elif 'playlist_name' in request_json: elif 'playlist_name' in request_json:
net = database.get_authed_spotify_network(user) net = database.get_authed_spotify_network(user)
playlists = net.get_playlists() try:
if playlists is not None: playlists = net.get_playlists()
playlist_to_play = next((i for i in playlists if i.name == request_json['playlist_name']), None) playlist_to_play = next((i for i in playlists if i.name == request_json['playlist_name']), None)
if playlist_to_play is not None: if playlist_to_play is not None:
@ -51,8 +52,9 @@ def play(user=None):
return jsonify({'message': 'played', 'status': 'success'}), 200 return jsonify({'message': 'played', 'status': 'success'}), 200
else: else:
return jsonify({'error': f"playlist {request_json['playlist_name']} not found"}), 404 return jsonify({'error': f"playlist {request_json['playlist_name']} not found"}), 404
else: except SpotifyNetworkException:
return jsonify({'error': "playlists not returned"}), 400 return jsonify({'error': "playlists not returned"}), 400
elif 'tracks' in request_json: elif 'tracks' in request_json:
try: try:
uris = [Uri(i) for i in request_json['tracks']] uris = [Uri(i) for i in request_json['tracks']]

View File

@ -12,6 +12,7 @@ from music.tasks.refresh_lastfm_stats import refresh_lastfm_track_stats, \
from spotfm.maths.counter import Counter from spotfm.maths.counter import Counter
from spotframework.model.uri import Uri from spotframework.model.uri import Uri
from spotframework.net.network import SpotifyNetworkException
blueprint = Blueprint('spotfm-api', __name__) blueprint = Blueprint('spotfm-api', __name__)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -48,18 +49,20 @@ def count(user=None):
'last.fm_username': fmnet.username 'last.fm_username': fmnet.username
}), 200 }), 200
elif playlist_name: elif playlist_name:
try:
playlists = spotnet.get_playlists()
playlist = next((i for i in playlists if i.name == playlist_name), None)
playlists = spotnet.get_playlists() if playlist is not None:
playlist = next((i for i in playlists if i.name == playlist_name), None) playlist_count = counter.count_playlist(playlist=playlist)
return jsonify({
if playlist is not None: "count": playlist_count,
playlist_count = counter.count_playlist(playlist=playlist) 'playlist_name': playlist_name,
return jsonify({ 'last.fm_username': fmnet.username
"count": playlist_count, }), 200
'playlist_name': playlist_name, else:
'last.fm_username': fmnet.username return jsonify({'error': f'playlist {playlist_name} not found'}), 404
}), 200 except SpotifyNetworkException:
else:
return jsonify({'error': f'playlist {playlist_name} not found'}), 404 return jsonify({'error': f'playlist {playlist_name} not found'}), 404

View File

@ -14,7 +14,7 @@ logger = logging.getLogger(__name__)
@blueprint.route('/sort', methods=['POST']) @blueprint.route('/sort', methods=['POST'])
@login_or_basic_auth @login_or_basic_auth
@spotify_link_required @spotify_link_required
def play(user=None): def sort(user=None):
request_json = request.get_json() request_json = request.get_json()
net = database.get_authed_spotify_network(user) net = database.get_authed_spotify_network(user)

View File

@ -2,7 +2,7 @@ from google.cloud import firestore
import logging import logging
from datetime import timedelta, datetime, timezone from datetime import timedelta, datetime, timezone
from spotframework.net.network import Network as SpotifyNetwork from spotframework.net.network import Network as SpotifyNetwork, SpotifyNetworkException
from fmframework.net.network import Network as FmNetwork from fmframework.net.network import Network as FmNetwork
from music.db.user import DatabaseUser from music.db.user import DatabaseUser
from music.model.user import User from music.model.user import User
@ -42,15 +42,21 @@ def get_authed_spotify_network(user):
access_token=user.access_token) access_token=user.access_token)
user_obj.on_refresh.append(refresh_token_database_callback) user_obj.on_refresh.append(refresh_token_database_callback)
net = SpotifyNetwork(user_obj)
if user.last_refreshed is not None and user.token_expiry is not None: if user.last_refreshed is not None and user.token_expiry is not None:
if user.last_refreshed + timedelta(seconds=user.token_expiry - 1) \ if user.last_refreshed + timedelta(seconds=user.token_expiry - 1) \
< datetime.now(timezone.utc): < datetime.now(timezone.utc):
user_obj.refresh_access_token() net.refresh_access_token()
else: else:
user_obj.refresh_access_token() net.refresh_access_token()
user_obj.refresh_info() try:
return SpotifyNetwork(user_obj) net.refresh_user_info()
except SpotifyNetworkException as e:
logger.error(f'error refreshing user info for {user.username} = {e}')
return net
else: else:
logger.error('user spotify not linked') logger.error('user spotify not linked')
else: else:

View File

@ -3,6 +3,7 @@ from google.cloud import firestore
import logging import logging
import music.db.database as database import music.db.database as database
from spotframework.net.network import SpotifyNetworkException
db = firestore.Client() db = firestore.Client()
@ -18,10 +19,8 @@ def create_playlist(user, name):
logger.info(f'creating spotify playlist for {user.username} / {name}') logger.info(f'creating spotify playlist for {user.username} / {name}')
net = database.get_authed_spotify_network(user) net = database.get_authed_spotify_network(user)
playlist = net.create_playlist(net.user.username, name) try:
return net.create_playlist(net.user.user.display_name, name)
if playlist is not None: except SpotifyNetworkException as e:
return playlist logger.error(f'error ocurred {user.username} / {name} - {e}')
else:
logger.error(f'no response received {user.username} / {name}')
return return

View File

@ -9,6 +9,7 @@ from music.model.playlist import Playlist
from spotfm.maths.counter import Counter from spotfm.maths.counter import Counter
from spotframework.model.uri import Uri from spotframework.model.uri import Uri
from spotframework.net.network import SpotifyNetworkException
db = firestore.Client() db = firestore.Client()
@ -37,7 +38,11 @@ def refresh_lastfm_track_stats(username, playlist_name):
logger.critical(f'playlist {playlist_name} for {username} has no spotify uri') logger.critical(f'playlist {playlist_name} for {username} has no spotify uri')
return return
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri)) try:
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri))
except SpotifyNetworkException as e:
logger.error(f'error retrieving spotify playlist {username} / {playlist_name} - {e}')
return
track_count = counter.count_playlist(playlist=spotify_playlist) track_count = counter.count_playlist(playlist=spotify_playlist)
user_count = fmnet.get_user_scrobble_count() user_count = fmnet.get_user_scrobble_count()
@ -75,7 +80,11 @@ def refresh_lastfm_album_stats(username, playlist_name):
logger.critical(f'playlist {playlist_name} for {username} has no spotify uri') logger.critical(f'playlist {playlist_name} for {username} has no spotify uri')
return return
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri)) try:
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri))
except SpotifyNetworkException as e:
logger.error(f'error retrieving spotify playlist {username} / {playlist_name} - {e}')
return
album_count = counter.count_playlist(playlist=spotify_playlist, query_album=True) album_count = counter.count_playlist(playlist=spotify_playlist, query_album=True)
user_count = fmnet.get_user_scrobble_count() user_count = fmnet.get_user_scrobble_count()
@ -113,7 +122,11 @@ def refresh_lastfm_artist_stats(username, playlist_name):
logger.critical(f'playlist {playlist_name} for {username} has no spotify uri') logger.critical(f'playlist {playlist_name} for {username} has no spotify uri')
return return
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri)) try:
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri))
except SpotifyNetworkException as e:
logger.error(f'error retrieving spotify playlist {username} / {playlist_name} - {e}')
return
artist_count = counter.count_playlist(playlist=spotify_playlist, query_artist=True) artist_count = counter.count_playlist(playlist=spotify_playlist, query_artist=True)
user_count = fmnet.get_user_scrobble_count() user_count = fmnet.get_user_scrobble_count()

View File

@ -8,6 +8,7 @@ from spotframework.filter import remove_local, get_track_objects
from spotframework.filter.added import added_after from spotframework.filter.added import added_after
from spotframework.filter.sort import sort_by_release_date from spotframework.filter.sort import sort_by_release_date
from spotframework.filter.deduplicate import deduplicate_by_name from spotframework.filter.deduplicate import deduplicate_by_name
from spotframework.net.network import SpotifyNetworkException
from fmframework.net.network import Network from fmframework.net.network import Network
from spotfm.charts.chart import get_chart_of_spotify_tracks from spotfm.charts.chart import get_chart_of_spotify_tracks
@ -49,7 +50,11 @@ def run_user_playlist(username, playlist_name):
logger.error(f'no spotify network returned for {username} / {playlist_name}') logger.error(f'no spotify network returned for {username} / {playlist_name}')
return return
user_playlists = net.get_user_playlists() try:
user_playlists = net.get_user_playlists()
except SpotifyNetworkException as e:
logger.error(f'error occured while retrieving playlists {username} / {playlist_name} - {e}')
return
part_generator = PartGenerator(user=user) part_generator = PartGenerator(user=user)
part_names = part_generator.get_recursive_parts(playlist.name) part_names = part_generator.get_recursive_parts(playlist.name)
@ -66,11 +71,14 @@ def run_user_playlist(username, playlist_name):
try: # attempt to cast to uri try: # attempt to cast to uri
uri = Uri(part_name) uri = Uri(part_name)
_tracks = net.get_playlist_tracks(uri=uri) try:
if _tracks and len(_tracks) > 0: _tracks = net.get_playlist_tracks(uri=uri)
playlist_tracks += _tracks if _tracks and len(_tracks) > 0:
else: playlist_tracks += _tracks
logger.warning(f'no tracks returned for {uri} {username} / {playlist_name}') else:
logger.warning(f'no tracks returned for {uri} {username} / {playlist_name}')
except SpotifyNetworkException as e:
logger.error(f'error occured while retrieving {uri} {username} / {playlist_name} - {e}')
except ValueError: # is a playlist name except ValueError: # is a playlist name
part_playlist = next((i for i in user_playlists if i.name == part_name), None) part_playlist = next((i for i in user_playlists if i.name == part_name), None)
@ -78,21 +86,27 @@ def run_user_playlist(username, playlist_name):
logger.warning(f'playlist {part_name} not found {username} / {playlist_name}') logger.warning(f'playlist {part_name} not found {username} / {playlist_name}')
continue continue
part_playlist_tracks = net.get_playlist_tracks(uri=part_playlist.uri) try:
if part_playlist_tracks and len(part_playlist_tracks) > 0: part_playlist_tracks = net.get_playlist_tracks(uri=part_playlist.uri)
playlist_tracks += part_playlist_tracks if part_playlist_tracks and len(part_playlist_tracks) > 0:
else: playlist_tracks += part_playlist_tracks
logger.warning(f'no tracks returned for {part_playlist.name} {username} / {playlist_name}') else:
logger.warning(f'no tracks returned for {part_playlist.name} {username} / {playlist_name}')
except SpotifyNetworkException as e:
logger.error(f'error occured while retrieving {part_name} {username} / {playlist_name} - {e}')
playlist_tracks = remove_local(playlist_tracks) playlist_tracks = remove_local(playlist_tracks)
# LIBRARY # LIBRARY
if playlist.include_library_tracks: if playlist.include_library_tracks:
library_tracks = net.get_library_tracks() try:
if library_tracks and len(library_tracks) > 0: library_tracks = net.get_library_tracks()
playlist_tracks += library_tracks if library_tracks and len(library_tracks) > 0:
else: playlist_tracks += library_tracks
logger.error(f'error getting library tracks {username} / {playlist_name}') else:
logger.error(f'error getting library tracks {username} / {playlist_name}')
except SpotifyNetworkException as e:
logger.error(f'error occured while retrieving library tracks {username} / {playlist_name} - {e}')
# PLAYLIST TYPE SPECIFIC # PLAYLIST TYPE SPECIFIC
if playlist.type == 'recents': if playlist.type == 'recents':
@ -132,26 +146,30 @@ def run_user_playlist(username, playlist_name):
# RECOMMENDATIONS # RECOMMENDATIONS
if playlist.include_recommendations: if playlist.include_recommendations:
recommendations = net.get_recommendations(tracks=[i.uri.object_id for i, j try:
in zip(*get_track_objects( recommendations = net.get_recommendations(tracks=[i.uri.object_id for i, j
random.sample(playlist_tracks, in zip(*get_track_objects(
k=min(5, len(playlist_tracks)) random.sample(playlist_tracks,
) k=min(5, len(playlist_tracks))
)) )
if i.uri.object_type == Uri.ObjectType.track], ))
response_limit=playlist.recommendation_sample) if i.uri.object_type == Uri.ObjectType.track],
if recommendations and len(recommendations.tracks) > 0: response_limit=playlist.recommendation_sample)
playlist_tracks += recommendations.tracks if recommendations and len(recommendations.tracks) > 0:
else: playlist_tracks += recommendations.tracks
logger.error(f'error getting recommendations {username} / {playlist_name}') else:
logger.error(f'error getting recommendations {username} / {playlist_name}')
except SpotifyNetworkException as e:
logger.error(f'error occured while generating recommendations {username} / {playlist_name} - {e}')
# DEDUPLICATE # DEDUPLICATE
playlist_tracks = deduplicate_by_name(playlist_tracks) playlist_tracks = deduplicate_by_name(playlist_tracks)
# EXECUTE # EXECUTE
resp = net.replace_playlist_tracks(uri_string=playlist.uri, uris=[i.uri for i, j try:
in zip(*get_track_objects(playlist_tracks))]) net.replace_playlist_tracks(uri_string=playlist.uri, uris=[i.uri for i, j
if resp: in zip(*get_track_objects(playlist_tracks))])
if playlist.description_overwrite: if playlist.description_overwrite:
string = playlist.description_overwrite string = playlist.description_overwrite
else: else:
@ -164,11 +182,13 @@ def run_user_playlist(username, playlist_name):
logger.error(f'no string generated {username} / {playlist_name}') logger.error(f'no string generated {username} / {playlist_name}')
return None return None
resp = net.change_playlist_details(Uri(playlist.uri), description=string) try:
if resp is None: net.change_playlist_details(Uri(playlist.uri), description=string)
logger.error(f'error changing description {username} / {playlist_name}') except SpotifyNetworkException as e:
else: logger.error(f'error changing description for {username} / {playlist_name} - {e}')
logger.error(f'error executing {username} / {playlist_name}')
except SpotifyNetworkException as e:
logger.error(f'error executing {username} / {playlist_name} - {e}')
playlist.last_updated = datetime.datetime.utcnow() playlist.last_updated = datetime.datetime.utcnow()
playlist.update() playlist.update()