Update iptv.py

This commit is contained in:
frxz751113
2024-08-09 16:07:11 +08:00
committed by GitHub
parent 63055aa0d9
commit 86226ddcd5
+20 -61
View File
@@ -112,52 +112,31 @@ for url in urls:
valid_urls.append(result)
for url in valid_urls:
print(url)
# 遍历由变量valid_urls持有的URL列表
for url in valid_urls:
# 遍历网址列表,获取JSON文件并解析
for url in valid_urls:
try:
# 尝试执行以下代码块,如果发生异常则跳转到except块
# 构造请求的URL,这里直接使用遍历得到的url变量
# 发送GET请求获取JSON文件,设置超时时间为0.5秒
json_url = f"{url}"
# 发送GET请求到json_url,超时时间设置为3秒
response = requests.get(json_url, timeout=3)
# 获取响应的内容,并以utf-8编码解码
response = requests.get(json_url, timeout=3)################################
json_data = response.content.decode('utf-8')
try:
# 按行分割解码后的数据,每行可能包含一个JSON对象
# 按行分割数据
lines = json_data.split('\n')
for line in lines:
# 去除行首尾的空白字符
line = line.strip()
# 检查去除空白后的行是否为空
if not line:
continue # 如果行是空的,则跳过当前循环迭代
# 检查当前行是否包含'udp'或'rtp'字符串
if 'udp' in line or 'rtp' in line:
continue # 如果行包含'udp'或'rtp',则跳过当前循环迭代
# 假设每行包含name和channel_url,以逗号分隔
line = line.strip()
if line:
name, channel_url = line.split(',')
# 将channel_url以'/'分割成多个部分
urls = channel_url.split('/', 3)
# 将json_url以'/'分割成多个部分,这里只取前三个部分
url_data = json_url.split('/', 3)
# 检查urls的长度是否至少为4
if len(urls) >= 4:
# 构造一个新的URL,格式为scheme://netloc/path
urld = (f"{urls[0]}//{url_data[2]}/{urls[3]}")
else:
# 如果urls长度小于4,则只使用前两个部分构造URL
urld = (f"{urls[0]}//{url_data[2]}")
print(f"{name},{urld}")
except Exception as e:
# 这里应该处理解析JSON数据时可能发生的异常,例如格式错误等
# 打印异常信息,实际使用中可能需要更详细的异常处理
print(f"Error processing line: {e}")
except requests.exceptions.RequestException as e:
# 处理请求过程中可能发生的异常,例如网络问题、超时等
# 打印异常信息,实际使用中可能需要更详细的异常处理
print(f"Request failed: {e}")
if name and urld:
name = name.replace("高清电影", "影迷电影")
name = name.replace("中央", "CCTV")
@@ -384,53 +363,33 @@ for url in urls:
valid_urls.append(result)
for url in valid_urls:
print(url)
# 遍历由变量valid_urls持有的URL列表
for url in valid_urls:
# 遍历网址列表,获取JSON文件并解析
for url in valid_urls:
try:
# 尝试执行以下代码块,如果发生异常则跳转到except块
# 发送GET请求获取JSON文件,设置超时时间为0.5秒(这里的注释可能是误写,因为代码中设置的超时时间是3秒)
# 找到URL中"//"之后开始的索引位置,这通常用来确定协议之后的部分
# 发送GET请求获取JSON文件,设置超时时间为0.5秒
ip_start_index = url.find("//") + 2
# 找到URL中第一个"."之后开始的索引位置,这通常用来确定域名的开始
ip_dot_start = url.find(".") + 1
# 找到URL中从域名开始之后的第一个"/"的索引位置,这通常用来确定路径的开始
ip_index_second = url.find("/", ip_dot_start)
# 提取base_url,即协议部分,例如http://https://
base_url = url[:ip_start_index]
# 提取IP地址部分,即域名部分
base_url = url[:ip_start_index] # http:// or https://
ip_address = url[ip_start_index:ip_index_second]
# 构造一个新的URL,这个URL似乎是用来构造请求的URL,但是这里看起来有些逻辑错误,因为它重复使用了base_url和ip_address
url_x = f"{base_url}{ip_address}"
# 这里应该是要设置请求的URL,但是实际上它只是重新赋值了url变量,并没有变化
json_url = f"{url}"
# 发送GET请求到json_url,超时时间设置为3秒
response = requests.get(json_url, timeout=3)
# 将响应的内容解析为JSON格式
response = requests.get(json_url, timeout=3) #///////////////
json_data = response.json()
try:
# 尝试解析JSON数据中的'data'字段
# 遍历'data'字段中的每个元素
# 解析JSON文件,获取name和url字段
for item in json_data['data']:
# 确保当前元素是字典类型
if isinstance(item, dict):
# 从字典中获取'name'字段的值
name = item.get('name')
# 从字典中获取'url'字段的值
urlx = item.get('url')
# 如果url字段中包含','、'udp'、'rtp'、':1111',则将urlx设置为"aaaaaaaa"
if ',' in urlx or 'udp' not in urlx and 'rtp' not in urlx:
if ',' in urlx:
if 'udp' not in urlx and 'rtp' not in urlx and ':1111' not in urlx:
urlx = f"aaaaaaaa"
# 这里有几个条件判断,但被注释掉了,看起来是想根据urlx的内容来决定urld的值
# 判断urlx是否包含'http',如果包含,则直接使用urlx作为urld
#if 'http' in urlx or 'udp' in urlx or 'rtp' in urlx:
if 'http' in urlx:
urld = f"{urlx}"
# 如果urlx不包含'http',则将base_url和urlx拼接作为urld
else:
urld = f"{url_x}{urlx}"
except:
# 如果在try块中发生异常,则执行这里的代码
# 这里没有具体的异常处理代码,只是占位符
pass
if name and urld:
name = name.replace("高清电影", "影迷电影")
name = name.replace("中央", "CCTV")
@@ -578,9 +537,9 @@ def filter_lines(input_file, output_file):
lines = file.readlines()
filtered_lines = []
for line in lines:
if 'hls' in line or 'tsfile' in line:
#if '2223' in line:
#if 'udp' not in line and 'rtp' not in line: # and 'CCTV' not in line and '卫视' not in line
#if 'hls' in line or 'tsfile' in line:
if '2223' in line:
if 'udp' not in line and 'rtp' not in line: # and 'CCTV' not in line and '卫视' not in line
filtered_lines.append(line)
with open(output_file, 'w', encoding='utf-8') as output_file:
output_file.writelines(filtered_lines)