-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip_head.py
76 lines (71 loc) · 3.58 KB
/
ip_head.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import re
import requests
from requests.exceptions import Timeout
load_urls = [
"http://mywlkj.ddns.net:754/tv.php?id=63",
]
file_contents = []
with open("mywlkj_gt.txt", 'r', encoding='utf-8') as file:
url_list = file.readlines()
def get_redirected_urls(url_list):
session = requests.Session()
redirected_urls = []
for line in url_list:
try:
line = line.strip()
count = line.count(',')
if count == 1:
if line:
channel_name, channel_url = line.split(',')
print("==========>>>>>",channel_name, channel_url)
if '#' not in channel_url and 'mp4' not in channel_url:
try:
response = requests.head(channel_url, allow_redirects=False, timeout=1.5)
# 如果初始请求返回200,但之后服务器又发出了302重定向,我们需要处理这种情况
if response.status_code == 200 and 'Location' in response.headers:
redirected_url = response.headers['Location']
redirected_response = session.head(redirected_url)
new_url = f"{channel_name},{redirected_url}"
print("--------------再次定向------》",redirected_url,redirected_response.url)
redirected_urls.append(new_url)
# 如果初始请求直接返回了重定向,我们直接返回重定向的URL
elif response.status_code in [301, 302, 303, 307, 308]:
print("--------------直接定向------》",response.headers['Location'])
_url = response.headers['Location']
new_url = f"{channel_name},{_url}"
redirected_urls.append(new_url)
else:
# 如果没有重定向,返回原始URL
new_url = channel_name, channel_url
redirected_urls.append(line)
except Timeout:
new_url = f"timeout_{channel_name}", channel_url
redirected_urls.append(line)
print("请求超时")
except requests.RequestException as e:
new_url = f"error_{channel_name}", channel_url
redirected_urls.append(line)
print(f"请求发生错误: {e}")
else:
# 如果没有重定向,返回原始URL
new_url = channel_name, channel_url
redirected_urls.append(line)
else:
redirected_urls.append(line)
else:
redirected_urls.append(line)
except:
print(url_list)
return redirected_urls
# 示例用法
redirected_urls = get_redirected_urls(url_list)
with open("mywlkj_all_gt.txt", 'w', encoding='utf-8') as file:
for line in redirected_urls:
if len(line) > 0:
name, name_url = line.split(',')
channel_url =(f"{name_url}")
channel_url = channel_url.replace("https://gitee.com/tv2785/tvbox/raw/master/gg.mp4", "https://gitee.com/guoqi8899/ipvideo/raw/master/gg.mp4")
file.write(f"{name},{channel_url}\n")
print(line)
file.close()