Python 代码优化:替换 m3u8 文件中的 'media_' 和 '#YUMING|https'

这段代码旨在处理 m3u8 文件,解决用户在代码中遇到的关于 'media_' 和 '#YUMING|https' 替换的问题。以下是修改后的代码,包含详细的注释以方便理解和修改:

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')]

                # 替换所有 'media_' 为新的 URL
                new_lines = []
                for line in lines:
                    if 'media_' in line:
                        line = line.replace('media_', new_url + 'media_')
                    # 删除 '#YUMING|https' 并追加到新列表
                    new_lines.append(line.replace('#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')]

                # 替换所有 'http' 和 '/-/' 之间的 URL 为新的 URL
                new_lines = []
                for line in lines:
                    if 'http' in line and '/-/' in line:
                        line = line[:line.index('http')] + new_url + line[line.index('/-/')+3:]
                    new_lines.append(line)

                # 将新内容写入原始文件
                with open(file_path, 'w') as f:
                    for line in new_lines:
                        f.write(line)
            else:
                print('失效:' + str(response.status_code))

代码说明:

  1. 读取文件:代码首先读取用户输入的 m3u8 文件目录,然后遍历目录中的所有以 .m3u8 结尾的文件。
  2. 处理 #YUMING|https:如果文件第一行包含 '#YUMING|https',则提取该行中的 URL,发送请求获取新的 URL,并将新 URL 替换所有 'media_',同时删除所有 '#YUMING|https'。
  3. 处理其他 URL:如果文件第一行不包含 '#YUMING|https',则从文件第二行或第八行提取 URL,发送请求获取新的 URL,并将新 URL 替换所有 'http' 和 '/-/' 之间的 URL。
  4. 写入文件:将修改后的内容写入原始文件。

注意:

  • 代码中使用 requests 库发送 HTTP 请求,需要先安装该库:pip install requests
  • 代码假设 m3u8 文件中的 URL 结构类似,如果结构不同,可能需要修改代码中的索引值。
  • 代码会直接修改原始文件,请在执行前做好备份。

希望以上修改和说明能帮助你解决问题。如有其他问题,请随时提出。

Python 代码优化:替换 m3u8 文件中的 media_ 和 #YUMING|https

原文地址: https://www.cveoy.top/t/topic/nEnr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录