use artist tracks in period to generate mix

This commit is contained in:
cwilvx
2024-12-01 22:00:22 +03:00
parent ee0f6c7646
commit 37374d6c82
2 changed files with 26 additions and 9 deletions
+9 -1
View File
@@ -15,12 +15,15 @@ def get_artists_in_period(
start_time: int | float, end_time: int | float, userid: int | None = None
):
scrobbles = ScrobbleTable.get_all_in_period(start_time, end_time, userid)
artists: Any = defaultdict(lambda: {"playcount": 0, "playduration": 0})
artists: Any = defaultdict(
lambda: {"playcount": 0, "playduration": 0, "tracks": {}}
)
for scrobble in scrobbles:
track = TrackStore.get_tracks_by_trackhashes([scrobble.trackhash])
if not track:
continue
track = track[0]
for artist in track.artists:
@@ -31,6 +34,11 @@ def get_artists_in_period(
artists[artisthash]["playcount"] += 1
artists[artisthash]["playduration"] += scrobble.duration
# index the track counts too
artists[artisthash]["tracks"][track.trackhash] = (
artists[artisthash]["tracks"].get(track.trackhash, 0) + 1
)
artists = list(artists.values())
return sorted(artists, key=lambda x: x["playduration"], reverse=True)