first recommendation draft

This commit is contained in:
cwilvx
2024-10-25 23:26:08 +03:00
parent a26373669d
commit c4a73f0d63
15 changed files with 393 additions and 6 deletions
+10 -3
View File
@@ -66,16 +66,23 @@ def seconds_to_time_string(seconds):
return f"{remaining_seconds} sec"
def get_date_range(duration: str):
def get_date_range(duration: str, units_ago: int = 0):
"""
Returns a tuple of dates representing the start and end of a given duration.
"""
date_range = None
seconds_ago = (
pendulum.now() - pendulum.now().subtract().start_of(duration)
).total_seconds() * units_ago
print("seconds_ago", duration, str(seconds_ago))
match duration:
case "week" | "month" | "year":
case "day" | "week" | "month" | "year":
date_range = (
pendulum.now().subtract().start_of(duration).timestamp(),
pendulum.now()
.subtract(seconds=seconds_ago)
.start_of(duration)
.timestamp(),
pendulum.now().end_of(duration).timestamp(),
)
case "alltime":
+4 -2
View File
@@ -13,7 +13,7 @@ from app.utils.dates import seconds_to_time_string
def get_artists_in_period(start_time: int, end_time: int):
scrobbles = ScrobbleTable.get_all_in_period(start_time, end_time)
artists = defaultdict(lambda: {"playcount": 0, "playduration": 0})
artists: Any = defaultdict(lambda: {"playcount": 0, "playduration": 0})
for scrobble in scrobbles:
track = TrackStore.get_tracks_by_trackhashes([scrobble.trackhash])
@@ -24,11 +24,13 @@ def get_artists_in_period(start_time: int, end_time: int):
for artist in track.artists:
artisthash = artist["artisthash"]
artists[artisthash]["artist"] = artist["name"]
artists[artisthash]["artisthash"] = artist["artisthash"]
artists[artisthash]["playcount"] += 1
artists[artisthash]["playduration"] += scrobble.duration
return list(artists.values())
artists = list(artists.values())
return sorted(artists, key=lambda x: x["playduration"], reverse=True)
def get_albums_in_period(start_time: int, end_time: int):