From cd7f93100bf916df25022caee7aa0bccddb4dd67 Mon Sep 17 00:00:00 2001 From: GitMatt Date: Tue, 18 Feb 2025 18:50:10 -0800 Subject: [PATCH] fix time variable for kexp. --- src/music/__main__.py | 2 ++ src/music/stream.py | 34 ++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/music/__main__.py b/src/music/__main__.py index 3ae723d..a0c2cb2 100644 --- a/src/music/__main__.py +++ b/src/music/__main__.py @@ -28,6 +28,8 @@ def _stream(stream, t: Optional[datetime]): if t is not None: time = t.astimezone(tz=timezone.utc) + else: + time = None play(stream, time) diff --git a/src/music/stream.py b/src/music/stream.py index afee956..56b3107 100644 --- a/src/music/stream.py +++ b/src/music/stream.py @@ -15,23 +15,33 @@ class Stream(str, Enum): CLIS = "clis" +def url_for_time(t) -> str: + default = "https://kexp-mp3-128.streamguys1.com/kexp128.mp3" + tz = t.strftime("%Y-%m-%dT%H:%M:%SZ") + print(f"getting archive: {t}") + check_url = ( + f"https://api.kexp.org/get_streaming_url/?bitrate=128×tamp={tz}&location=1" + ) + response = requests.get(check_url) + if response.status_code == 200: + found = response.json().get("sg-url") + if found is None: + print(f"error getting archive: {t}") + return default + else: + print(f"error getting archive: {t}") + return default + + return found + + def play(stream, t=None): """Play streams using mpv.""" match stream: case Stream.KEXP: - default = "https://kexp-mp3-128.streamguys1.com/kexp128.mp3" + url = "https://kexp-mp3-128.streamguys1.com/kexp128.mp3" if t: - tz = t.strftime("%Y-%m-%dT%H:%M:%SZ") - print(f"getting archive: {t}") - check_url = f"https://api.kexp.org/get_streaming_url/?bitrate=128×tamp={tz}&location=1" - response = requests.get(check_url) - if response.status_code == 200: - url = response.json().get("sg-url") - if url is None: - print(f"error getting archive: {t}") - url = default - else: - url = default + url = url_for_time(t) case Stream.KUGS: url = "https://peridot.streamguys1.com:7175/kugs-mp3" case Stream.CLIS: