Implement a `raise_for_status()` on `HTTPError`
This commit is contained in:
parent
a108135141
commit
477edc4910
|
@ -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:
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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, _)
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue