Implement a `raise_for_status()` on `HTTPError`

This commit is contained in:
Barbagus 2023-02-13 18:44:32 +01:00
parent a108135141
commit 477edc4910
5 changed files with 10 additions and 10 deletions

View File

@ -15,8 +15,7 @@ def _fetch_api_object(http, url, object_type):
# Fetch an API object.
r = http.request("GET", url)
if not 200 <= r.status < 300:
raise HTTPError(r)
HTTPError.raise_for_status(r)
mime_type = r.getheader("content-type")
if mime_type != MIME_TYPE:

View File

@ -27,8 +27,7 @@ def download_mp4_media(url, file_name, http, on_progress):
headers={"Range": f"bytes={f.tell()}-"},
preload_content=False,
)
if not 200 <= r.status < 300:
raise HTTPError(r)
HTTPError.raise_for_status(r)
_, total = r.getheader("content-range").split("/")
total = int(total)
@ -54,8 +53,7 @@ def download_vtt_media(url, file_name, http, on_progress):
with open(temp_file, "w", encoding="utf-8") as f:
r = http.request("GET", url)
if not 200 <= r.status < 300:
raise HTTPError(r)
HTTPError.raise_for_status(r)
subtitles.convert(r.data.decode("utf-8"), f)
on_progress(file_name, f.tell(), f.tell())

View File

@ -27,6 +27,11 @@ class UnexpectedError(ModuleError):
class HTTPError(Exception):
"""A wrapper around a filed HTTP response."""
@classmethod
def raise_for_status(self, r):
if not 200 <= r.status < 300:
raise self(r)
#
# www

View File

@ -30,8 +30,7 @@ MIME_TYPE = "application/x-mpegURL"
def _fetch_index(http, url):
# Fetch a M3U8 playlist
r = http.request("GET", url)
if not 200 <= r.status < 300:
raise HTTPError(r)
HTTPError.raise_for_status(r)
if (_ := r.getheader("content-type")) != MIME_TYPE:
raise UnexpectedHLSResponse("MIME_TYPE", url, MIME_TYPE, _)

View File

@ -101,8 +101,7 @@ def iter_programs(page_url, http):
# special handling of 404
if r.status == 404:
raise PageNotFound(page_url)
if not 200 <= r.status < 300:
raise HTTPError(r)
HTTPError.raise_for_status(r)
# no HTML parsing required, whe just find the mark
html = r.data.decode("utf-8")