2019-10-19 20:35:37 +01:00
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import music.db.database as database
|
2020-04-30 14:54:05 +01:00
|
|
|
from music.model.user import User
|
|
|
|
from music.model.playlist import Playlist
|
2019-10-19 20:35:37 +01:00
|
|
|
|
|
|
|
from spotfm.maths.counter import Counter
|
2020-06-22 20:21:54 +01:00
|
|
|
from spotframework.net.network import SpotifyNetworkException
|
2019-10-19 20:35:37 +01:00
|
|
|
|
2020-06-28 10:07:43 +01:00
|
|
|
from fmframework.net.network import LastFMNetworkException
|
|
|
|
|
2019-10-19 20:35:37 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-10-20 17:28:05 +01:00
|
|
|
def refresh_lastfm_track_stats(username, playlist_name):
|
2019-10-19 20:35:37 +01:00
|
|
|
|
|
|
|
logger.info(f'refreshing {playlist_name} stats for {username}')
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
user = User.collection.filter('username', '==', username.strip().lower()).get()
|
|
|
|
if user is None:
|
|
|
|
logger.error(f'user {username} not found')
|
|
|
|
|
|
|
|
fmnet = database.get_authed_lastfm_network(user)
|
|
|
|
spotnet = database.get_authed_spotify_network(user)
|
2019-10-19 20:35:37 +01:00
|
|
|
counter = Counter(fmnet=fmnet, spotnet=spotnet)
|
|
|
|
|
2021-06-16 20:40:14 +01:00
|
|
|
playlist = user.get_playlist(playlist_name)
|
2019-10-19 20:35:37 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if playlist is None:
|
|
|
|
logger.critical(f'playlist {playlist_name} for {username} not found')
|
|
|
|
return
|
|
|
|
|
|
|
|
if playlist.uri is None:
|
|
|
|
logger.critical(f'playlist {playlist_name} for {username} has no spotify uri')
|
|
|
|
return
|
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
spotify_playlist = spotnet.playlist(uri=playlist.uri)
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error retrieving spotify playlist {username} / {playlist_name}')
|
2020-06-22 20:21:54 +01:00
|
|
|
return
|
2019-10-20 16:32:10 +01:00
|
|
|
track_count = counter.count_playlist(playlist=spotify_playlist)
|
2019-10-19 20:35:37 +01:00
|
|
|
|
2020-06-28 10:07:43 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
user_count = fmnet.user_scrobble_count()
|
2020-06-28 10:07:43 +01:00
|
|
|
if user_count > 0:
|
|
|
|
percent = round((track_count * 100) / user_count, 2)
|
|
|
|
else:
|
|
|
|
percent = 0
|
2020-07-01 11:03:43 +01:00
|
|
|
except LastFMNetworkException:
|
|
|
|
logger.exception(f'error while retrieving user scrobble count {username} / {playlist_name}')
|
2019-10-23 14:44:17 +01:00
|
|
|
percent = 0
|
2019-10-19 20:35:37 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
playlist.lastfm_stat_count = track_count
|
|
|
|
playlist.lastfm_stat_percent = percent
|
|
|
|
playlist.lastfm_stat_last_refresh = datetime.utcnow()
|
2019-10-20 17:28:05 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
playlist.update()
|
2019-10-20 17:28:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def refresh_lastfm_album_stats(username, playlist_name):
|
|
|
|
|
|
|
|
logger.info(f'refreshing {playlist_name} stats for {username}')
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
user = User.collection.filter('username', '==', username.strip().lower()).get()
|
|
|
|
if user is None:
|
|
|
|
logger.error(f'user {username} not found')
|
|
|
|
|
|
|
|
fmnet = database.get_authed_lastfm_network(user)
|
|
|
|
spotnet = database.get_authed_spotify_network(user)
|
2019-10-20 17:28:05 +01:00
|
|
|
counter = Counter(fmnet=fmnet, spotnet=spotnet)
|
|
|
|
|
2021-06-16 20:40:14 +01:00
|
|
|
playlist = user.get_playlist(playlist_name)
|
2019-10-20 17:28:05 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if playlist is None:
|
|
|
|
logger.critical(f'playlist {playlist_name} for {username} not found')
|
|
|
|
return
|
|
|
|
|
|
|
|
if playlist.uri is None:
|
|
|
|
logger.critical(f'playlist {playlist_name} for {username} has no spotify uri')
|
|
|
|
return
|
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
spotify_playlist = spotnet.playlist(uri=playlist.uri)
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error retrieving spotify playlist {username} / {playlist_name}')
|
2020-06-22 20:21:54 +01:00
|
|
|
return
|
2019-10-20 17:28:05 +01:00
|
|
|
album_count = counter.count_playlist(playlist=spotify_playlist, query_album=True)
|
|
|
|
|
2020-06-28 10:07:43 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
user_count = fmnet.user_scrobble_count()
|
2020-06-28 10:07:43 +01:00
|
|
|
if user_count > 0:
|
|
|
|
album_percent = round((album_count * 100) / user_count, 2)
|
|
|
|
else:
|
|
|
|
album_percent = 0
|
2020-07-01 11:03:43 +01:00
|
|
|
except LastFMNetworkException:
|
|
|
|
logger.exception(f'error while retrieving user scrobble count {username} / {playlist_name}')
|
2019-10-23 14:44:17 +01:00
|
|
|
album_percent = 0
|
2019-10-20 17:28:05 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
playlist.lastfm_stat_album_count = album_count
|
|
|
|
playlist.lastfm_stat_album_percent = album_percent
|
|
|
|
playlist.lastfm_stat_last_refresh = datetime.utcnow()
|
2019-10-20 17:28:05 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
playlist.update()
|
2019-10-20 17:28:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
def refresh_lastfm_artist_stats(username, playlist_name):
|
|
|
|
|
|
|
|
logger.info(f'refreshing {playlist_name} stats for {username}')
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
user = User.collection.filter('username', '==', username.strip().lower()).get()
|
|
|
|
if user is None:
|
|
|
|
logger.error(f'user {username} not found')
|
|
|
|
|
|
|
|
fmnet = database.get_authed_lastfm_network(user)
|
|
|
|
spotnet = database.get_authed_spotify_network(user)
|
2019-10-20 17:28:05 +01:00
|
|
|
counter = Counter(fmnet=fmnet, spotnet=spotnet)
|
|
|
|
|
2021-06-16 20:40:14 +01:00
|
|
|
playlist = user.get_playlist(playlist_name)
|
2019-10-20 17:28:05 +01:00
|
|
|
|
2020-02-24 18:15:38 +00:00
|
|
|
if playlist is None:
|
|
|
|
logger.critical(f'playlist {playlist_name} for {username} not found')
|
|
|
|
return
|
|
|
|
|
|
|
|
if playlist.uri is None:
|
|
|
|
logger.critical(f'playlist {playlist_name} for {username} has no spotify uri')
|
|
|
|
return
|
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
spotify_playlist = spotnet.playlist(uri=playlist.uri)
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error retrieving spotify playlist {username} / {playlist_name}')
|
2020-06-22 20:21:54 +01:00
|
|
|
return
|
2019-10-20 17:28:05 +01:00
|
|
|
artist_count = counter.count_playlist(playlist=spotify_playlist, query_artist=True)
|
|
|
|
|
2020-06-28 10:07:43 +01:00
|
|
|
try:
|
2020-08-12 09:30:26 +01:00
|
|
|
user_count = fmnet.user_scrobble_count()
|
2020-06-28 10:07:43 +01:00
|
|
|
if user_count > 0:
|
|
|
|
artist_percent = round((artist_count * 100) / user_count, 2)
|
|
|
|
else:
|
|
|
|
artist_percent = 0
|
2020-07-01 11:03:43 +01:00
|
|
|
except LastFMNetworkException:
|
|
|
|
logger.exception(f'error while retrieving user scrobble count {username} / {playlist_name}')
|
2019-10-23 14:44:17 +01:00
|
|
|
artist_percent = 0
|
2019-10-20 17:28:05 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
playlist.lastfm_stat_artist_count = artist_count
|
|
|
|
playlist.lastfm_stat_artist_percent = artist_percent
|
|
|
|
playlist.lastfm_stat_last_refresh = datetime.utcnow()
|
2019-10-20 16:32:10 +01:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
playlist.update()
|