single time method, ms to string formatter, fixed track timing

This commit is contained in:
aj 2020-08-11 18:27:58 +01:00
parent 39da38b6f9
commit 42d7158f27

View File

@ -10,34 +10,93 @@ from fmframework.net.scrape import LibraryScraper
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def seconds_to_time_str(milliseconds: int = None, seconds: int = None, clip_days=True):
if milliseconds is not None:
seconds = milliseconds / 1000
days = seconds // (24 * 60 * 60)
seconds %= (24 * 60 * 60)
hour = seconds // (60 * 60)
seconds %= (60 * 60)
minutes = seconds // 60
seconds %= 60
if days > 0 or not clip_days:
return "%d:%02d:%02d:%02d" % (days, hour, minutes, seconds)
return "%02d:%02d:%02d" % (hour, minutes, seconds)
def time(spotnet: SpotNet, artist: str, album: str = None, track: str = None, username: str = None,
fmnet: FmNet = None, from_date: datetime = None, to_date: datetime = None, date_preset: str = None,
return_tracks: bool = False) -> int:
if fmnet is None and username is None:
raise NameError('no username')
if track is not None:
return time_track(spotnet=spotnet, fmnet=fmnet,
artist=artist, track=track,
username=username or fmnet.username,
from_date=from_date, to_date=to_date, date_preset=date_preset,
return_tracks=return_tracks)
elif album is not None:
return time_album(spotnet=spotnet, fmnet=fmnet,
artist=artist, album=album,
username=username or fmnet.username,
from_date=from_date, to_date=to_date, date_preset=date_preset,
return_tracks=return_tracks)
else:
return time_artist(spotnet=spotnet, fmnet=fmnet,
artist=artist,
username=username or fmnet.username,
from_date=from_date, to_date=to_date, date_preset=date_preset,
return_tracks=return_tracks)
def time_artist(spotnet: SpotNet, artist: str, username: str, fmnet: FmNet = None, def time_artist(spotnet: SpotNet, artist: str, username: str, fmnet: FmNet = None,
from_date: datetime = None, to_date: datetime = None, date_preset: str = None) -> int: from_date: datetime = None, to_date: datetime = None, date_preset: str = None,
return_tracks: bool = False) -> int:
logger.info(f'timing {artist} for {username}') logger.info(f'timing {artist} for {username}')
fmtracks = LibraryScraper.get_scrobbled_tracks(username=username, artist=artist, whole_track=False, fmtracks = LibraryScraper.get_scrobbled_tracks(username=username, artist=artist, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset) from_date=from_date, to_date=to_date, date_preset=date_preset)
return time_track_collection(tracks=fmtracks, spotnet=spotnet, username=username, fmnet=fmnet) return time_track_collection(tracks=fmtracks, spotnet=spotnet, username=username, fmnet=fmnet,
return_tracks=return_tracks)
def time_album(spotnet: SpotNet, artist: str, album: str, username: str, fmnet: FmNet = None, def time_album(spotnet: SpotNet, artist: str, album: str, username: str, fmnet: FmNet = None,
from_date: datetime = None, to_date: datetime = None, date_preset: str = None) -> int: from_date: datetime = None, to_date: datetime = None, date_preset: str = None,
return_tracks: bool = False) -> int:
logger.info(f'timing {album} / {artist} for {username}') logger.info(f'timing {album} / {artist} for {username}')
fmtracks = LibraryScraper.get_albums_tracks(username=username, artist=artist, album=album, whole_track=False, fmtracks = LibraryScraper.get_albums_tracks(username=username, artist=artist, album=album, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset) from_date=from_date, to_date=to_date, date_preset=date_preset)
return time_track_collection(tracks=fmtracks, spotnet=spotnet, username=username, fmnet=fmnet) return time_track_collection(tracks=fmtracks, spotnet=spotnet, username=username, fmnet=fmnet,
return_tracks=return_tracks)
def time_track(spotnet: SpotNet, artist: str, track: str, username: str, fmnet: FmNet = None, def time_track(spotnet: SpotNet, artist: str, track: str, username: str, fmnet: FmNet = None,
from_date: datetime = None, to_date: datetime = None, date_preset: str = None) -> int: from_date: datetime = None, to_date: datetime = None, date_preset: str = None,
return_tracks: bool = False) -> int:
logger.info(f'timing {track} / {artist} for {username}') logger.info(f'timing {track} / {artist} for {username}')
fmtracks = LibraryScraper.get_track_scrobbles(username=username, artist=artist, track=track, whole_track=False, fmtracks = LibraryScraper.get_track_scrobbles(username=username, artist=artist, track=track, whole_track=False,
from_date=from_date, to_date=to_date, date_preset=date_preset) from_date=from_date, to_date=to_date, date_preset=date_preset)
if fmtracks is not None:
if len(fmtracks) == 0:
return 0
else:
return time_track_collection(tracks=[fmtracks[0].track], spotnet=spotnet, username=username, fmnet=fmnet,
return_tracks=return_tracks)
return 0
return time_track_collection(tracks=fmtracks, spotnet=spotnet, username=username, fmnet=fmnet)
def time_track_collection(tracks, spotnet: SpotNet, username: str, fmnet:FmNet = None): def time_track_collection(tracks, spotnet: SpotNet, username: str, fmnet: FmNet = None, return_tracks: bool = False):
track_pairs = [] track_pairs = []
if tracks is not None: if tracks is not None:
for track in tracks: for track in tracks:
@ -76,4 +135,7 @@ def time_track_collection(tracks, spotnet: SpotNet, username: str, fmnet:FmNet =
total_ms += duration * track_pair[0].user_scrobbles total_ms += duration * track_pair[0].user_scrobbles
if return_tracks:
return total_ms, track_pairs
else:
return total_ms return total_ms