Mixonomer/music/tasks/refresh_lastfm_stats.py
2020-04-30 14:54:05 +01:00

130 lines
4.1 KiB
Python

from google.cloud import firestore
import logging
from datetime import datetime
import music.db.database as database
from music.model.user import User
from music.model.playlist import Playlist
from spotfm.maths.counter import Counter
from spotframework.model.uri import Uri
db = firestore.Client()
logger = logging.getLogger(__name__)
def refresh_lastfm_track_stats(username, playlist_name):
logger.info(f'refreshing {playlist_name} stats for {username}')
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)
counter = Counter(fmnet=fmnet, spotnet=spotnet)
playlist = Playlist.collection.parent(user.key).filter('name', '==', playlist_name).get()
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
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri))
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:
percent = 0
playlist.lastfm_stat_count = track_count
playlist.lastfm_stat_percent = percent
playlist.lastfm_stat_last_refresh = datetime.utcnow()
playlist.update()
def refresh_lastfm_album_stats(username, playlist_name):
logger.info(f'refreshing {playlist_name} stats for {username}')
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)
counter = Counter(fmnet=fmnet, spotnet=spotnet)
playlist = Playlist.collection.parent(user.key).filter('name', '==', playlist_name).get()
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
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri))
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:
album_percent = 0
playlist.lastfm_stat_album_count = album_count
playlist.lastfm_stat_album_percent = album_percent
playlist.lastfm_stat_last_refresh = datetime.utcnow()
playlist.update()
def refresh_lastfm_artist_stats(username, playlist_name):
logger.info(f'refreshing {playlist_name} stats for {username}')
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)
counter = Counter(fmnet=fmnet, spotnet=spotnet)
playlist = Playlist.collection.parent(user.key).filter('name', '==', playlist_name).get()
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
spotify_playlist = spotnet.get_playlist(uri=Uri(playlist.uri))
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:
artist_percent = 0
playlist.lastfm_stat_artist_count = artist_count
playlist.lastfm_stat_artist_percent = artist_percent
playlist.lastfm_stat_last_refresh = datetime.utcnow()
playlist.update()