From 7e1db53a9362d98a4d5881d934d7d73afde1fcfd Mon Sep 17 00:00:00 2001 From: barry Date: Sat, 11 Jan 2025 18:36:26 +0100 Subject: [PATCH] smal fix & add day to standby --- spotiplayer_pi/api.py | 12 ++++++++++-- spotiplayer_pi/main.py | 12 +++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spotiplayer_pi/api.py b/spotiplayer_pi/api.py index 6c9d8b4..a74dc61 100644 --- a/spotiplayer_pi/api.py +++ b/spotiplayer_pi/api.py @@ -27,12 +27,20 @@ class Api: req = requests.get(url, headers=self.header) if req.status_code == 204: return "not-playing" - if req.status_code != 200: - warnings.warn(f"Unknown API Error {req.status_code},\n{req.content}") + if req.status_code == 401: + warnings.warn("API Error: Bad or Expired token") + elif req.status_code == 403: + warnings.warn("API Error: Bad OAuth request, re-authenticating won't help") + elif req.status_code == 429: + warnings.warn("API Error: API rate limit exceeded") + elif req.status_code != 200: + warnings.warn(f"{req.status_code},\n{req.content}") return None return self._format_req(req.json()) def _format_req(self, r): + if not r["is_playing"]: + return "not-playing" item, album = r["item"], r["item"]["album"] res = { "progress_ms": r["progress_ms"], diff --git a/spotiplayer_pi/main.py b/spotiplayer_pi/main.py index 2146091..bc614b2 100644 --- a/spotiplayer_pi/main.py +++ b/spotiplayer_pi/main.py @@ -58,7 +58,7 @@ def display_loop(api: Api, cfg: Dict): last_api_call = 0 last_auth_refresh = 0 - current_mode = None + current_mode = None # 0: not-playing, 1: playing last_track = None data = None auth_interval = 0 @@ -72,7 +72,6 @@ def display_loop(api: Api, cfg: Dict): if time.time() - last_api_call >= cfg["api_interval"]: data = api.getPlaying() last_api_call = time.time() - if data == None: warnings.warn("No data found") d.ShowImage(error_img) @@ -89,6 +88,13 @@ def display_loop(api: Api, cfg: Dict): fill=(255, 255, 255), # fill=cfg["color_theme"]["text"], ) + draw.text( + (20, 152), + current_time.strftime("%a %d"), + font=Font2, + fill=(255, 255, 255), + # fill=cfg["color_theme"]["text"], + ) if current_mode != 0: current_mode = 0 print("Standby mode") @@ -135,7 +141,7 @@ def display_loop(api: Api, cfg: Dict): progress_time = data["progress_ms"] + int( (time.time() - last_api_call) * 1000 ) - progress = progress_time / data["duration_ms"] + progress = min(1, progress_time / data["duration_ms"]) bar_width = int(w * progress) draw.rectangle( [(8, h - 22), (w + 2, h - 8)],