本帖最后由 Teachers 于 2021-9-7 18:21 编辑
前言
只能手机上下载,我想下载到电脑上,于是就用Python写了这个工具,废话不多说上代码,需要用的身份验证内容得去app上抓。
如果违规请管理员直接删帖!
代码
import datetime
import os
import requests
from Crypto.Cipher import AES
headers = {
'sign': '75bfff8efsfsfsfsffsfsfsfsd3e6cbbc',
'nonce-str': 'JJJJx4eI',
'sign-info': '5gXQCJbfusgbffhskfhksfhisfjshfjshhfgdushgjdghdjghdjAQo5C3OVvbUfst1plSO5V5ny63tt4xr/o+Q==',
'app-type': '1',
'api-version': '2.36.1',
'app-api': '29',
'is-tablet': '0',
'channel': 'oppo',
'authorization': 'BeareJgfydbfhjdgyhfKJKHJHBelp9YFN_c',
'is-night': '0',
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'okhttp/4.5.0',
}
data = {
'video_id': '111374',
'video_type': '1'
}
def download(url, download_path, c_file_name):
print('正在下载:', c_file_name)
download_path = download_path
if not os.path.exists(download_path):
os.mkdir(download_path)
all_content = requests.get(url).text # 获取第一层M3U8文件内容
if "#EXTM3U" not in all_content:
raise BaseException("非M3U8的链接")
if "EXT-X-STREAM-INF" in all_content: # 第一层
file_line = all_content.split("\n")
for line in file_line:
if '.m3u8' in line:
url = url.rsplit("/", 1)[0] + "/" + line # 拼出第二层m3u8的URL
all_content = requests.get(url).text
file_line = all_content.split("\n")
unknowns = True
key = ""
for index, line in enumerate(file_line): # 第二层
if "#EXT-X-KEY" in line: # 找解密Key
method_pos = line.find("METHOD")
comma_pos = line.find(",")
method = line[method_pos:comma_pos].split('=')[1]
print("Decode Method:", method)
uri_pos = line.find("URI")
quotation_mark_pos = line.rfind('"')
key_path = line[uri_pos:quotation_mark_pos].split('"')[1]
key_url = key_path # 拼出key解密密钥URL
res = requests.get(key_url)
key = res.content
print("key:", key)
if "EXTINF" in line: # 找ts地址并下载
unknowns = False
pd_url = 'https://m3u8.huke88.com' + file_line[index + 1] # 拼出ts片段的URL
res = requests.get(pd_url)
c_file_name = c_file_name
if len(key): # AES 解密
cryptos = AES.new(key, AES.MODE_CBC, key)
with open(os.path.join(download_path, c_file_name + ".mp4"), 'ab+') as f:
f.write(cryptos.decrypt(res.content))
else:
with open(os.path.join(download_path, c_file_name + ".mp4"), 'ab+') as f:
f.write(res.content)
f.flush()
if unknowns:
raise BaseException("未找到对应的下载链接")
else:
print("下载完成")
response = requests.post('https://api.huke88.com/v5/video/route-batch-download', headers=headers, data=data)
def dw_url(m3u8_list):
index_make = m3u8_list['data']['dir_data']['title']
if not os.path.exists('某课旺视频下载/' + index_make):
os.makedirs('某课旺视频下载/' + index_make)
for i, o in enumerate(response.json()['data']['dir_list']):
if not os.path.exists('某课旺视频下载/' + index_make + '/' + str(i + 1) + '-' + o['title']):
os.makedirs('某课旺视频下载/' + index_make + '/' + str(i + 1) + '-' + o['title'])
# print(str(i + 1) + '-' + o['title'])
for item, value in enumerate(o['children']):
# print(value['video_url'] + '----' + str(item + 1) + '-' + value['title'])
download(value['video_url'], '某课旺视频下载/' + index_make + '/' + str(i + 1) + '-' + o['title'] + '/',
str(item + 1) + '-' + value[
'title'])
dw_url(response.json())
目录清晰!
|