updated last.fm actions to catch fmframework exceptions

This commit is contained in:
aj 2020-06-28 10:07:43 +01:00
parent c2b537e39f
commit d6c66efdab
3 changed files with 62 additions and 33 deletions

View File

@ -11,6 +11,8 @@ from spotfm.maths.counter import Counter
from spotframework.model.uri import Uri
from spotframework.net.network import SpotifyNetworkException
from fmframework.net.network import LastFMNetworkException
db = firestore.Client()
logger = logging.getLogger(__name__)
@ -45,10 +47,14 @@ def refresh_lastfm_track_stats(username, playlist_name):
return
track_count = counter.count_playlist(playlist=spotify_playlist)
user_count = fmnet.get_user_scrobble_count()
if user_count > 0:
percent = round((track_count * 100) / user_count, 2)
else:
try:
user_count = fmnet.get_user_scrobble_count()
if user_count > 0:
percent = round((track_count * 100) / user_count, 2)
else:
percent = 0
except LastFMNetworkException as e:
logger.error(f'error while retrieving user scrobble count - {e}')
percent = 0
playlist.lastfm_stat_count = track_count
@ -87,10 +93,14 @@ def refresh_lastfm_album_stats(username, playlist_name):
return
album_count = counter.count_playlist(playlist=spotify_playlist, query_album=True)
user_count = fmnet.get_user_scrobble_count()
if user_count > 0:
album_percent = round((album_count * 100) / user_count, 2)
else:
try:
user_count = fmnet.get_user_scrobble_count()
if user_count > 0:
album_percent = round((album_count * 100) / user_count, 2)
else:
album_percent = 0
except LastFMNetworkException as e:
logger.error(f'error while retrieving user scrobble count - {e}')
album_percent = 0
playlist.lastfm_stat_album_count = album_count
@ -129,10 +139,14 @@ def refresh_lastfm_artist_stats(username, playlist_name):
return
artist_count = counter.count_playlist(playlist=spotify_playlist, query_artist=True)
user_count = fmnet.get_user_scrobble_count()
if user_count > 0:
artist_percent = round((artist_count * 100) / user_count, 2)
else:
try:
user_count = fmnet.get_user_scrobble_count()
if user_count > 0:
artist_percent = round((artist_count * 100) / user_count, 2)
else:
artist_percent = 0
except LastFMNetworkException as e:
logger.error(f'error while retrieving user scrobble count - {e}')
artist_percent = 0
playlist.lastfm_stat_artist_count = artist_count

View File

@ -5,6 +5,8 @@ import music.db.database as database
from music.model.user import User
from music.model.tag import Tag
from fmframework.net.network import LastFMNetworkException
logger = logging.getLogger(__name__)
@ -33,39 +35,52 @@ def update_tag(username, tag_id):
return
tag_count = 0
user_scrobbles = net.get_user_scrobble_count()
try:
user_scrobbles = net.get_user_scrobble_count()
except LastFMNetworkException as e:
logger.error(f'error retrieving scrobble count - {e}')
user_scrobbles = 0
artists = []
for artist in tag.artists:
net_artist = net.get_artist(name=artist['name'])
try:
net_artist = net.get_artist(name=artist['name'])
if net_artist is not None:
artist['count'] = net_artist.user_scrobbles
tag_count += net_artist.user_scrobbles
if net_artist is not None:
artist['count'] = net_artist.user_scrobbles
tag_count += net_artist.user_scrobbles
except LastFMNetworkException as e:
logger.error(f'error during artist retrieval - {e}')
artists.append(artist)
albums = []
for album in tag.albums:
net_album = net.get_album(name=album['name'], artist=album['artist'])
try:
net_album = net.get_album(name=album['name'], artist=album['artist'])
if net_album is not None:
album['count'] = net_album.user_scrobbles
if net_album is not None:
album['count'] = net_album.user_scrobbles
if album['artist'].lower() not in [i.lower() for i in [j['name'] for j in artists]]:
tag_count += net_album.user_scrobbles
if album['artist'].lower() not in [i.lower() for i in [j['name'] for j in artists]]:
tag_count += net_album.user_scrobbles
except LastFMNetworkException as e:
logger.error(f'error during album retrieval - {e}')
albums.append(album)
tracks = []
for track in tag.tracks:
net_track = net.get_track(name=track['name'], artist=track['artist'])
try:
net_track = net.get_track(name=track['name'], artist=track['artist'])
if net_track is not None:
track['count'] = net_track.user_scrobbles
if net_track is not None:
track['count'] = net_track.user_scrobbles
if track['artist'].lower() not in [i.lower() for i in [j['name'] for j in artists]]:
tag_count += net_track.user_scrobbles
if track['artist'].lower() not in [i.lower() for i in [j['name'] for j in artists]]:
tag_count += net_track.user_scrobbles
except LastFMNetworkException as e:
logger.error(f'error during track retrieval - {e}')
tracks.append(track)

View File

@ -1,12 +1,12 @@
astroid==2.4.2
cachetools==4.1.0
certifi==2020.4.5.2
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
fireo==1.3.3
Flask==1.1.2
google-api-core==1.20.1
google-auth==1.17.2
google-api-core==1.21.0
google-auth==1.18.0
google-cloud-core==1.3.0
google-cloud-firestore==1.7.0
google-cloud-logging==1.15.0
@ -14,7 +14,7 @@ google-cloud-pubsub==1.6.0
google-cloud-tasks==1.5.0
googleapis-common-protos==1.52.0
grpc-google-iam-v1==0.12.3
grpcio==1.29.0
grpcio==1.30.0
idna==2.9
isort==4.3.21
itsdangerous==1.1.0
@ -22,14 +22,14 @@ Jinja2==2.11.2
lazy-object-proxy==1.5.0
MarkupSafe==1.1.1
mccabe==0.6.1
numpy==1.18.5
numpy==1.19.0
opencv-python==4.2.0.34
protobuf==3.12.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pylint==2.5.3
pytz==2020.1
requests==2.23.0
requests==2.24.0
rsa==4.6
six==1.15.0
tabulate==0.8.7