Skip to content

Commit

Permalink
请求m3u8时判断返回状态码
Browse files Browse the repository at this point in the history
  • Loading branch information
AuYang261 committed Aug 13, 2024
1 parent dbbaa37 commit ff4a2db
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions m3u8dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def get_m3u8_info(self, m3u8_url, num_retries):
with requests.get(
url, timeout=(3, 30), verify=False, headers=self._headers
) as res:
if res.status_code != 200:
raise Exception(f"Failed to get m3u8 info: {res.status_code}")
self._front_url = res.url.split(res.request.path_url)[0]
if "EXT-X-STREAM-INF" in res.text: # 判定为顶级M3U8文件
for line in res.text.split("\n"):
Expand Down Expand Up @@ -188,15 +190,15 @@ def get_ts_url(self, m3u8_text_str):
else:
f.write(new_m3u8_str.encode("utf-8"))

def download_ts(self, ts_url, name, num_retries):
def download_ts(self, ts_url_original, name, num_retries):
"""
下载 .ts 文件
"""
if not self._token:
self._token = utils.getToken()
token = self._token
ts_url = utils.add_signature_for_url(
ts_url.split("\n")[0], token, self.timestamp, self.signature
ts_url_original.split("\n")[0], token, self.timestamp, self.signature
)
try:
if not os.path.exists(name):
Expand All @@ -223,7 +225,7 @@ def download_ts(self, ts_url, name, num_retries):
)
sys.stdout.flush()
else:
self.download_ts(ts_url, name, num_retries - 1)
self.download_ts(ts_url_original, name, num_retries - 1)
else:
self._success_sum += 1

Expand All @@ -232,7 +234,7 @@ def download_ts(self, ts_url, name, num_retries):
if os.path.exists(name):
os.remove(name)
if num_retries > 0:
self.download_ts(ts_url, name, num_retries - 1)
self.download_ts(ts_url_original, name, num_retries - 1)

def download_key(self, key_line, num_retries):
"""
Expand Down

0 comments on commit ff4a2db

Please sign in to comment.