feat: get current month listening time using API
This commit is contained in:
32
stats.py
32
stats.py
@@ -37,7 +37,8 @@ class AudibleStats:
|
|||||||
self.client = audible.Client(auth=self.auth)
|
self.client = audible.Client(auth=self.auth)
|
||||||
return
|
return
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.info("Failed to load existing auth. Re-authenticating.\n")
|
logger.info(
|
||||||
|
"Failed to load existing auth. Re-authenticating.\n")
|
||||||
|
|
||||||
email = input("Email: ")
|
email = input("Email: ")
|
||||||
password = getpass("Password: ")
|
password = getpass("Password: ")
|
||||||
@@ -85,7 +86,8 @@ class AudibleStats:
|
|||||||
monthly_listening_interval_start_date=f"{middle}-01",
|
monthly_listening_interval_start_date=f"{middle}-01",
|
||||||
store="Audible",
|
store="Audible",
|
||||||
)
|
)
|
||||||
monthly_stats = stats.get("aggregated_monthly_listening_stats", [])
|
monthly_stats = stats.get(
|
||||||
|
"aggregated_monthly_listening_stats", [])
|
||||||
has_activity = bool(
|
has_activity = bool(
|
||||||
monthly_stats
|
monthly_stats
|
||||||
and any(stat.get("aggregated_sum", 0) > 0 for stat in monthly_stats)
|
and any(stat.get("aggregated_sum", 0) > 0 for stat in monthly_stats)
|
||||||
@@ -101,12 +103,38 @@ class AudibleStats:
|
|||||||
|
|
||||||
return earliest_year
|
return earliest_year
|
||||||
|
|
||||||
|
def get_current_month_listening_time(self) -> tuple[int, int, int]:
|
||||||
|
"""Get total listening time for the current month as (hours, minutes, seconds)."""
|
||||||
|
try:
|
||||||
|
stats = self.client.get(
|
||||||
|
"1.0/stats/aggregates",
|
||||||
|
monthly_listening_interval_duration="1",
|
||||||
|
monthly_listening_interval_start_date=date.today().strftime("%Y-%m"),
|
||||||
|
store="Audible",
|
||||||
|
)
|
||||||
|
monthly_stats = stats.get("aggregated_monthly_listening_stats", [])
|
||||||
|
if not monthly_stats:
|
||||||
|
return (0, 0, 0)
|
||||||
|
|
||||||
|
total_milliseconds = sum(
|
||||||
|
stat.get("aggregated_sum", 0) for stat in monthly_stats
|
||||||
|
)
|
||||||
|
total_seconds = int(total_milliseconds // 1000)
|
||||||
|
hours, remainder = divmod(total_seconds, 3600)
|
||||||
|
minutes, seconds = divmod(remainder, 60)
|
||||||
|
return (hours, minutes, seconds)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Could not get current month listening time: {e}")
|
||||||
|
return (0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""Main entry point."""
|
"""Main entry point."""
|
||||||
worker = AudibleStats()
|
worker = AudibleStats()
|
||||||
worker.authenticate()
|
worker.authenticate()
|
||||||
print(worker.get_signup_year())
|
print(worker.get_signup_year())
|
||||||
|
hours, minutes, seconds = worker.get_current_month_listening_time()
|
||||||
|
print(f"Total listening time this month: {hours}h {minutes}m {seconds}s")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user