两种不同类型的m3u8要区分开互不干涉单独处理保证先处理所有以https开头的m3u8文件然后处理所有以#YUMINGhttps开头的m3u8文件以https开头的m3u8文件从https开始到-替换为返回url以https开头的m3u8文件中不存在以media_ 开头的行请务重复处理以#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' }
dir_path = input('请输入.m3u8文件所在目录的路径:')
https_files = [] yuming_files = []
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()
if lines[0].startswith('#YUMING|https'):
yuming_files.append(file_path)
elif 'https' in lines[1]:
https_files.append(file_path)
for file_path in https_files: with open(file_path, 'r') as f: lines = f.readlines()
new_lines = []
for line in lines:
if line.startswith('#'):
new_lines.append(line)
elif 'media_' in line:
new_lines.append(line)
else:
new_lines.append(line.replace('https', '返回url', 1))
with open(file_path, 'w') as f:
for line in new_lines:
f.write(line)
for file_path in yuming_files: with open(file_path, 'r') as f: lines = f.readlines()
url = lines[0][lines[0].index('https'):lines[0].index('/-/')+3]
response = requests.post(url + 'playlist.m3u8', headers=headers)
if response.status_code == 200:
print(f'成功:{response.status_code}')
new_url = response.url
new_url = new_url[:new_url.index('playlist.m3u8')]
new_lines = []
for line in lines:
if line.startswith('#'):
new_lines.append(line)
elif 'media_' in line:
new_lines.append(new_url + line)
else:
continue
with open(file_path, 'w') as f:
for line in new_lines:
f.write(line
原文地址: https://www.cveoy.top/t/topic/dnA0 著作权归作者所有。请勿转载和采集!