From 7ebcff48e58d95d7b12fcfa175ee90d185ae639e Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Tue, 22 Oct 2024 23:44:08 -0400 Subject: [PATCH] Refactor download function to handle HTTP errors and return appropriate messages --- spinget.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/spinget.py b/spinget.py index 70e3e09..8fa5c46 100755 --- a/spinget.py +++ b/spinget.py @@ -21,6 +21,7 @@ import os import subprocess import sys +from urllib.error import HTTPError import concurrent.futures @@ -172,9 +173,16 @@ def loadsegs(stamp, duration_hours): while accum < required: showtime = curts.strftime("%Y%m%dT%H%M00Z") print(f"Fetching index for {showtime}") - playlist = m3u8.load(INDEXURL.format(STATION_SHORTCODE, showtime)) - if len(playlist.segments) == 0: - print("No playlist data found!") + try: + playlist = m3u8.load(INDEXURL.format(STATION_SHORTCODE, showtime)) + if len(playlist.segments) == 0: + print("No playlist data found!") + return [] + except HTTPError as e: + if e.code == 404: + print(f"404 Error: Playlist for {showtime} not found. Try waiting an hour...") + return [] + print(f"HTTPError occurred: {e}") return [] total_secs = 0 for seg in playlist.segments: