Update 酒店源.py

This commit is contained in:
frxz751113
2024-08-29 20:35:57 +08:00
committed by GitHub
parent 14b711853b
commit 1ac63bea30
+23 -28
View File
@@ -130,35 +130,30 @@ for url in valid_urls:
# 遍历网址列表,获取JSON文件并解析 # 遍历网址列表,获取JSON文件并解析
for url in valid_urls: for url in valid_urls:
try: try:
# 发送GET请求获取JSON文件,设置超时时间为0.5秒 json_url = f"{url}"
response = requests.get(url, timeout=0.5) response = requests.get(json_url, timeout=1)
json_data = response.content.decode('utf-8') json_data = response.content.decode('utf-8')
try:
# 按行分割数据 lines = json_data.split('\n')
lines = json_data.split('\n') for line in lines:
for line in lines: if 'hls' in line and ('udp' not in line or 'rtp' not in line):
# 行中需包含hls,但排除udp和rtp line = line.strip()
if 'hls' in line and ('udp' not in line and 'rtp' not in line): if line:
line = line.strip() name, channel_url = line.split(',')
if line: urls = channel_url.split('/', 3)
# 分割行以获取频道名和原始URL url_data = json_url.split('/', 3)
name, channel_url = line.split(',') if len(urls) >= 4:
# 获取 json_url 中的 ip 和 port
# 解析json_url以提取IP和端口 ip_port = json_url.split('//')[1].split(':')[0] + ':' + json_url.split('//')[1].split(':')[1]
parsed_json_url = urlparse(url) urld = (f"{urls[0]}//{ip_port}/{urls[3]}")
json_ip_with_port = parsed_json_url.hostname + ':' + parsed_json_url.port else:
# 获取 json_url 中的 ip 和 port
# 仅提取JSON中的IP部分用于替换,假设channel_url中的IP是直接跟在"http://"或"https://"之后 ip_port = json_url.split('//')[1].split(':')[0] + ':' + json_url.split('//')[1].split(':')[1]
channel_ip = channel_url.split('/')[2] urld = (f"{urls[0]}//{ip_port}")
except:
# 替换原始URL中的IP地址为json_url中的IP地址和端口 pass
new_channel_url = channel_url.replace(channel_ip, json_ip_with_port) except:
pass
# 构造新的行
new_line = f"{name},{new_channel_url}"
# 打印新的行
print(new_line) # 打印新的行
# 写入到文件中 # 写入到文件中
with open('iptv.txt', 'a', encoding='utf-8') as outfile: with open('iptv.txt', 'a', encoding='utf-8') as outfile: