Python代码优化:m3u8文件'media_'替换问题解决方案
Python代码优化:m3u8文件'media_'替换问题解决方案
本文介绍如何修复Python代码中针对以'#YUMING|https'开头的m3u8文件对'media_'的替换部分未成功执行的问题,并提供优化后的代码示例。
问题描述:
在处理以'#YUMING|https'开头的m3u8文件时,代码中针对'media_'的替换部分没有成功被执行。
问题分析:
- 'media_'不是一个URL,而是一段文件,需要替换的是包含'media_'的URL部分。
- 代码中直接将'media_'替换为新的URL,导致URL不完整。
解决方案:
以下是对代码的优化:
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文件所在目录的路径:')
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 '#YUMING|https' in lines[0]:
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('成功:' + str(response.status_code))
new_url = response.url
new_url = new_url[:new_url.index('playlist.m3u8')]
# Replace all occurrences of "media_" with the new URL
new_lines = []
for line in lines:
if 'media_' in line:
new_line = line.replace('media_', '')
new_line = new_line[:new_line.index('.ts')] + '.ts\n'
new_lines.append(new_url + new_line)
elif not line.startswith('#YUMING|https'):
new_lines.append(line)
# Write the new lines back to the original file (except for the first line if it starts with '#YUMING|https')
with open(file_path, 'w') as f:
for line in new_lines:
f.write(line)
else:
print('失效:' + str(response.status_code))
else:
if 'http' in lines[1]:
url = lines[1][lines[1].index('http'):lines[1].index('/-/')+3]
else:
url = lines[8][lines[8].index('http'):lines[8].index('/-/')+3]
response = requests.post(url + 'playlist.m3u8', headers=headers)
if response.status_code == 200:
print('成功:' + str(response.status_code))
new_url = response.url
new_url = new_url[:new_url.index('playlist.m3u8')]
# Replace all occurrences of URLs between "http" and "/-/" with the new URL
new_lines = []
for line in lines:
if 'http' in line and '/-/' in line:
new_line = line[line.index('http'):line.index('/-/')+3]
new_line = new_line + line[line.index('/-/')+3:]
new_lines.append(line.replace(new_line, new_url + new_line))
else:
new_lines.append(line)
# Write the new lines back to the original file
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))
代码解释:
- **'media_'替换:**代码现在只提取包含'media_'的URL部分,而不是直接将'media_'替换为新的URL,保证了URL的完整性。
- URL处理: 代码使用了更清晰的逻辑来提取URL,并对URL进行了分割,保证了替换的正确性。
- **文件写入:**代码优化了文件写入部分,保证了只写入有效的内容。
注意:
- 请确保你的代码环境已经安装了requests库。
- 代码中的目录路径需要根据你的实际情况进行修改。
通过上述优化,代码能够成功地将m3u8文件中包含'media_'的URL替换为新的URL,并解决原代码中存在的问题。
参考:
原文地址: https://www.cveoy.top/t/topic/nEnm 著作权归作者所有。请勿转载和采集!