Skip to content

Commit

Permalink
大幅优化了性能
Browse files Browse the repository at this point in the history
  • Loading branch information
AuYang261 committed Nov 12, 2023
1 parent 4b4e3d7 commit dd24b14
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
27 changes: 15 additions & 12 deletions m3u8dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class M3u8Download:
:param base64_key: base64编码的字符串
"""

def __init__(self, url, workDir, name, max_workers=64, num_retries=5, base64_key=None):
def __init__(self, url, workDir, name, max_workers=32, num_retries=999, base64_key=None):

self._url = url
self._token = None
Expand All @@ -68,6 +68,8 @@ def __init__(self, url, workDir, name, max_workers=64, num_retries=5, base64_key
'Origin': 'https://www.yanhekt.cn',
'referer': 'https://www.yanhekt.cn/',
}
self.timestamp = str(int(time.time()))
self.signature = signature_from_js.getSignature(self.timestamp)

urllib3.disable_warnings()

Expand All @@ -76,18 +78,21 @@ def __init__(self, url, workDir, name, max_workers=64, num_retries=5, base64_key
self.get_m3u8_info(self._url, self._num_retries)
print('Downloading: %s' % self._name, 'Save path: %s' % self._file_path, sep='\n')
with ThreadPoolExecutorWithQueueSizeLimit(self._max_workers) as pool:
pool.submit(self.updateSignature)
for k, ts_url in enumerate(self._ts_url_list):
pool.submit(self.download_ts, ts_url, os.path.join(self._file_path, str(k)), self._num_retries)
if self._success_sum == self._ts_sum:
self.output_mp4()
self.delete_file()
print(f"Download successfully --> {self._name}")

def getSignature(self):
timestamp = str(int(time.time()))
signature = signature_from_js.getSignature(timestamp)
# print(timestamp, signature)
return timestamp, signature
def updateSignature(self):
while self._success_sum != self._ts_sum:
self.timestamp = str(int(time.time()))
self.signature = signature_from_js.getSignature(self.timestamp)
# print('Updated signature')
# print(timestamp, signature)
time.sleep(10)

def getToken(self):
if self._token == None:
Expand All @@ -109,9 +114,8 @@ def get_m3u8_info(self, m3u8_url, num_retries):
"""

token = self.getToken()
timestamp, signature = self.getSignature()
url = m3u8_url+"?Xvideo_Token="+token+"&Xclient_Timestamp=" + timestamp+"&Xclient_Signature=" + \
signature+"&Xclient_Version=v1&Platform=yhkt_user"
url = m3u8_url+"?Xvideo_Token="+token+"&Xclient_Timestamp=" + self.timestamp+"&Xclient_Signature=" + \
self.signature+"&Xclient_Version=v1&Platform=yhkt_user"
try:
with requests.get(url, timeout=(3, 30), verify=False, headers=self._headers) as res:
self._front_url = res.url.split(res.request.path_url)[0]
Expand Down Expand Up @@ -175,9 +179,8 @@ def download_ts(self, ts_url, name, num_retries):
下载 .ts 文件
"""
token = self.getToken()
timestamp, signature = self.getSignature()
ts_url = ts_url.split('\n')[0]+"?Xvideo_Token="+token+"&Xclient_Timestamp=" + \
timestamp+"&Xclient_Signature="+signature+"&Xclient_Version=v1&Platform=yhkt_user"
self.timestamp+"&Xclient_Signature="+self.signature+"&Xclient_Version=v1&Platform=yhkt_user"
try:
if not os.path.exists(name):
with requests.get(ts_url, stream=True, timeout=(5, 60), verify=False, headers=self._headers) as res:
Expand All @@ -194,7 +197,7 @@ def download_ts(self, ts_url, name, num_retries):
self.download_ts(ts_url, name, num_retries - 1)
else:
self._success_sum += 1
except Exception:
except Exception as e:
if os.path.exists(name):
os.remove(name)
if num_retries > 0:
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def main():

if __name__ == '__main__':
try:
# main()
cProfile.run('main()', 'profile.txt')
main()
# cProfile.run('main()', 'output/profile.txt')
except Exception as e:
print(e)
print("If the problem is still not solved, you can report an issue in https://github.com/AuYang261/BIT_yanhe_download/issues.")
Expand Down

0 comments on commit dd24b14

Please sign in to comment.