forked from deverzh/ai-api-proxy-list
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_latency.py
More file actions
193 lines (157 loc) · 6.01 KB
/
Copy pathupdate_latency.py
File metadata and controls
193 lines (157 loc) · 6.01 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
Update latency values in README.md for the 7-column format.
Sorting: first by API Base URL latency, then by homepage latency.
"""
import re
import subprocess
import platform
import time
import socket
import urllib.request
import urllib.error
from pathlib import Path
README_PATH = Path(__file__).resolve().parent / "README.md"
with open(README_PATH, "r", encoding="utf-8") as f:
content = f.read()
table_start = content.find("| # | 名称 | 官网 |")
table_end = content.find("\n\n---\n\n## 📚", table_start)
if table_end == -1:
table_end = content.find("\n\n## 📚", table_start)
table_text = content[table_start:table_end]
lines = table_text.split("\n")
row_re = re.compile(
r"^\|\s*(\d+)\s*\|\s*(.*?)\s*\|\s*\[([^\]]+)\]\((https?://[^)]+)\)\s*"
r"\|\s*(.*?)\s*\|\s*(.*?)\s*\|\s*(.*?)\s*\|\s*(.*?)\s*\|$",
re.MULTILINE,
)
old_row_re = re.compile(
r"^\|\s*(\d+)\s*\|\s*(.*?)\s*\|\s*\[([^\]]+)\]\((https?://[^)]+)\)\s*"
r"\|\s*(.*?)\s*\|\s*(.*?)\s*\|$",
re.MULTILINE,
)
providers = []
for match in row_re.finditer(table_text):
providers.append({
"index": int(match.group(1)),
"name": match.group(2).strip(),
"domain": match.group(3).strip(),
"homepage": match.group(4).strip(),
"models": match.group(5).strip(),
"api_url": match.group(6).strip(),
"api_latency": match.group(7).strip(),
"latency": match.group(8).strip(),
})
is_new_format = len(providers) > 0
if not providers:
for match in old_row_re.finditer(table_text):
providers.append({
"index": int(match.group(1)),
"name": match.group(2).strip(),
"domain": match.group(3).strip(),
"homepage": match.group(4).strip(),
"models": match.group(5).strip(),
"api_url": "待确认",
"api_latency": "-",
"latency": match.group(6).strip(),
})
print(f"Found {len(providers)} rows to test")
def test_domain(domain):
"""Test domain using ping, TCP, and HTTP."""
try:
param = "-n" if platform.system().lower() == "windows" else "-c"
timeout_param = "-w" if platform.system().lower() == "windows" else "-W"
timeout_val = "3000" if platform.system().lower() == "windows" else "3"
result = subprocess.run(
["ping", param, "1", timeout_param, timeout_val, domain],
capture_output=True, text=True, timeout=8
)
output = result.stdout + result.stderr
time_match = re.search(r'时间[=<]\s*(\d+)\s*ms', output, re.IGNORECASE)
if not time_match:
time_match = re.search(r'time[=<]\s*([\d.]+)\s*ms', output, re.IGNORECASE)
if not time_match:
time_match = re.search(r'(?:平均|Average|最大|Maximum)\s*[=:]\s*(\d+)\s*ms', output, re.IGNORECASE)
if result.returncode == 0 and time_match:
return int(float(time_match.group(1))), True
except:
pass
try:
start = time.time()
sock = socket.create_connection((domain, 443), timeout=5)
elapsed = int((time.time() - start) * 1000)
sock.close()
return elapsed, True
except:
pass
try:
start = time.time()
sock = socket.create_connection((domain, 80), timeout=5)
elapsed = int((time.time() - start) * 1000)
sock.close()
return elapsed, True
except:
pass
for scheme in ['https', 'http']:
try:
start = time.time()
req = urllib.request.Request(f"{scheme}://{domain}", method='HEAD')
req.add_header('User-Agent', 'Mozilla/5.0')
resp = urllib.request.urlopen(req, timeout=5)
elapsed = int((time.time() - start) * 1000)
return elapsed, True
except urllib.error.HTTPError as e:
elapsed = int((time.time() - start) * 1000)
return elapsed, True
except:
continue
return None, False
new_providers = []
for i, p in enumerate(providers):
domain = p['domain'].split('/')[0].replace('www.', '')
name_clean = p['name'].lstrip("❌ ").strip()
while name_clean.startswith("❌"):
name_clean = name_clean[1:].strip()
latency, ok = test_domain(domain)
if latency is not None:
latency_str = f"{latency}ms"
status = "✅"
else:
latency_str = "超时"
name_clean = f"❌ {name_clean}"
status = "❌"
p['name'] = name_clean
p['latency'] = latency_str
new_providers.append(p)
print(f"{i+1:3d}. {status} {name_clean:<30s} {domain:<35s} {latency if latency else '超时'}ms")
time.sleep(0.05)
def sort_key(p):
api_latency_val = 999999
if p['api_latency'] and p['api_latency'] != "-" and p['api_latency'] != "未知":
try:
api_latency_val = int(p['api_latency'].replace("ms", ""))
except ValueError:
pass
latency_val = 999999
if p['latency'] and p['latency'] != "超时" and p['latency'] != "-":
try:
latency_val = int(p['latency'].replace("ms", ""))
except ValueError:
pass
api_priority = 0 if api_latency_val < 999999 else 1
homepage_priority = 0 if latency_val < 999999 else 1
return (api_priority, api_latency_val, homepage_priority, latency_val, p['name'].lower())
providers_sorted = sorted(new_providers, key=sort_key)
header = "| # | 名称 | 官网 | 支持模型 | Base URL | API延迟 | 官网延迟 |"
align = "|:---:|:---|:---|:---|:---|:---:|:---:|"
new_lines = [header, align]
for i, p in enumerate(providers_sorted, 1):
line = (
f"| {i} | {p['name']} | [{p['domain']}]({p['homepage']}) | "
f"{p['models']} | {p['api_url']} | {p['api_latency']} | {p['latency']} |"
)
new_lines.append(line)
new_table = "\n".join(new_lines)
new_content = content[:table_start] + new_table + content[table_end:]
with open(README_PATH, "w", encoding="utf-8") as f:
f.write(new_content)
print(f"\nUpdated {len(new_providers)} entries. Sorted by API latency first, then homepage latency.")