请认真阅读要求不要落下每一点:1 先处理以https开头的m3u8文件然后再以#YUMINGhttps开头的m3u8文件处理以https开头的m3u8文件的代码也要写在前面2 两种不同类型的m3u8文件以https开头的m3u8文件获取第8行从https开始到-替换为返回的新url3 以#YUMINGhttps开头的m3u8文件将每行开头的media_替换为返回的新url + media_注意:一
import os import requests
设置请求头
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8', 'Accept-Language': 'en-US,en;q=0.9,ja-JP;q=0.8,ja;q=0.7', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Upgrade-Insecure-Requests': '1' }
输入.m3u8文件所在目录的路径
dir_path = input('请输入.m3u8文件所在目录的路径:')
遍历目录下的所有.m3u8文件
for file_name in os.listdir(dir_path): if file_name.endswith('.m3u8'): print(f'处理文件: {file_name}') file_path = os.path.join(dir_path, file_name) with open(file_path, 'r') as f: lines = f.readlines()
# 处理以'#YUMING|https'开头的m3u8文件
if '#YUMING|https' in lines[0]:
# 获取请求地址并发送请求
url = lines[0][lines[0].index('https'):lines[0].index('/-/')+3]
response = requests.post(url + 'playlist.m3u8', headers=headers)
# 如果请求成功则替换URL并删除第一行
if response.status_code == 200:
print('成功:' + str(response.status_code))
new_url = response.url
new_url = new_url[:new_url.index('playlist.m3u8')]
# 替换所有以"media_"开头的行,并删除第一行
new_lines = []
for line in lines:
if line.startswith('media_'):
new_lines.append(new_url + line)
elif not line.startswith('#YUMING|https'):
new_lines.append(line)
# 将新行写回原文件
with open(file_path, 'w') as f:
for line in new_lines[1:]:
f.write(line)
# 如果请求失败则输出状态码
else:
print('失效:' + str(response.status_code))
# 处理以'https'开头的m3u8文件
else:
# 获取请求地址并发送请求
if 'https' in lines[1]:
url = lines[1][lines[1].index('https'):lines[1].index('/-/')+3]
else:
url = lines[8][lines[8].index('https'):lines[8].index('/-/')+3]
response = requests.post(url + 'playlist.m3u8', headers=headers)
# 如果请求成功则替换URL
if response.status_code == 200:
print('成功:' + str(response.status_code))
new_url = response.url
new_url = new_url[:new_url.index('playlist.m3u8')]
# 替换所有以"https"和"/-/"之间的URL,并保留第一行
new_lines = []
for line in lines:
if 'https' in line and '/-/' in line:
new_lines.append(line[:line.index('https')] + new_url + line[line.index('/-/')+3:])
else:
new_lines.append(line)
# 将新行写回原文件
with open(file_path, 'w') as f:
for line in new_lines:
if not line.startswith('#YUMING|https'):
f.write(line)
# 如果请求失败则输出状态码
else:
print('失效:' + str(response.status_code)
原文地址: https://www.cveoy.top/t/topic/dn5Z 著作权归作者所有。请勿转载和采集!