smal fix & add day to standby

This commit is contained in:
barry 2025-01-11 18:36:26 +01:00
parent f3645368ec
commit 7e1db53a93
2 changed files with 19 additions and 5 deletions

View File

@ -27,12 +27,20 @@ class Api:
req = requests.get(url, headers=self.header) req = requests.get(url, headers=self.header)
if req.status_code == 204: if req.status_code == 204:
return "not-playing" return "not-playing"
if req.status_code != 200: if req.status_code == 401:
warnings.warn(f"Unknown API Error {req.status_code},\n{req.content}") 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 None
return self._format_req(req.json()) return self._format_req(req.json())
def _format_req(self, r): def _format_req(self, r):
if not r["is_playing"]:
return "not-playing"
item, album = r["item"], r["item"]["album"] item, album = r["item"], r["item"]["album"]
res = { res = {
"progress_ms": r["progress_ms"], "progress_ms": r["progress_ms"],

View File

@ -58,7 +58,7 @@ def display_loop(api: Api, cfg: Dict):
last_api_call = 0 last_api_call = 0
last_auth_refresh = 0 last_auth_refresh = 0
current_mode = None current_mode = None # 0: not-playing, 1: playing
last_track = None last_track = None
data = None data = None
auth_interval = 0 auth_interval = 0
@ -72,7 +72,6 @@ def display_loop(api: Api, cfg: Dict):
if time.time() - last_api_call >= cfg["api_interval"]: if time.time() - last_api_call >= cfg["api_interval"]:
data = api.getPlaying() data = api.getPlaying()
last_api_call = time.time() last_api_call = time.time()
if data == None: if data == None:
warnings.warn("No data found") warnings.warn("No data found")
d.ShowImage(error_img) d.ShowImage(error_img)
@ -89,6 +88,13 @@ def display_loop(api: Api, cfg: Dict):
fill=(255, 255, 255), fill=(255, 255, 255),
# fill=cfg["color_theme"]["text"], # 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: if current_mode != 0:
current_mode = 0 current_mode = 0
print("Standby mode") print("Standby mode")
@ -135,7 +141,7 @@ def display_loop(api: Api, cfg: Dict):
progress_time = data["progress_ms"] + int( progress_time = data["progress_ms"] + int(
(time.time() - last_api_call) * 1000 (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) bar_width = int(w * progress)
draw.rectangle( draw.rectangle(
[(8, h - 22), (w + 2, h - 8)], [(8, h - 22), (w + 2, h - 8)],