Update app.py
Browse files
app.py
CHANGED
@@ -9,54 +9,44 @@ from aiohttp import web, ClientTimeout, TCPConnector
|
|
9 |
from urllib.parse import parse_qs
|
10 |
from cachetools import TTLCache
|
11 |
|
12 |
-
# 创建一个TTL缓存,最多存储1000个项目,每个项目的有效期为
|
13 |
-
cache = TTLCache(maxsize=1000, ttl=
|
14 |
|
15 |
-
async def fetch_url(url, session, max_retries=3
|
|
|
|
|
|
|
16 |
for attempt in range(max_retries):
|
17 |
try:
|
18 |
-
async with session.get(url, timeout=ClientTimeout(total=
|
19 |
response.raise_for_status()
|
20 |
return await response.text()
|
21 |
-
except asyncio.TimeoutError:
|
22 |
-
print(f"Attempt {attempt + 1} timed out after {timeout} seconds", flush=True)
|
23 |
except aiohttp.ClientError as e:
|
24 |
print(f"Attempt {attempt + 1} failed: {str(e)}", flush=True)
|
25 |
-
|
26 |
-
|
27 |
-
await asyncio.sleep(
|
28 |
-
|
29 |
-
raise Exception(f"Failed to fetch URL after {max_retries} attempts")
|
30 |
|
31 |
async def extract_and_transform_proxies(input_text):
|
32 |
-
print("Complete original input data:")
|
33 |
-
print(input_text)
|
34 |
-
print("------------------------")
|
35 |
-
|
36 |
try:
|
37 |
-
# 尝试直接解析整个输入作为YAML
|
38 |
data = yaml.safe_load(input_text)
|
39 |
if isinstance(data, dict) and 'proxies' in data:
|
40 |
proxies_list = data['proxies']
|
|
|
|
|
41 |
else:
|
42 |
-
|
43 |
-
proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE | re.DOTALL)
|
44 |
if proxies_match:
|
45 |
proxies_text = proxies_match.group(1)
|
46 |
proxies_list = yaml.safe_load(proxies_text)
|
47 |
else:
|
48 |
return "未找到有效的代理配置"
|
49 |
-
except yaml.YAMLError
|
50 |
-
return
|
51 |
|
52 |
if not proxies_list:
|
53 |
return "未找到有效的代理配置"
|
54 |
|
55 |
-
print(f"Found {len(proxies_list)} possible proxy configurations")
|
56 |
-
print("Sample of parsed proxies list:")
|
57 |
-
print(proxies_list[:5]) # 只打印前5个代理配置
|
58 |
-
print("------------------------")
|
59 |
-
|
60 |
transformed_proxies = []
|
61 |
|
62 |
for proxy in proxies_list:
|
@@ -141,22 +131,19 @@ async def handle_request(request):
|
|
141 |
try:
|
142 |
print(f"Fetching URL: {url}", flush=True)
|
143 |
async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:
|
144 |
-
input_text = await fetch_url(url, session
|
145 |
print(f"URL content length: {len(input_text)}", flush=True)
|
146 |
result = await extract_and_transform_proxies(input_text)
|
147 |
print(f"Transformed result length: {len(result)}", flush=True)
|
148 |
-
print("First 1000 characters of transformed result:")
|
149 |
-
print(result[:1000])
|
150 |
-
print("------------------------")
|
151 |
|
152 |
# 将结果存入缓存
|
153 |
cache[url] = result
|
154 |
|
155 |
return web.Response(text=result, content_type='text/plain')
|
156 |
except Exception as e:
|
157 |
-
|
158 |
-
|
159 |
-
return web.Response(text=
|
160 |
else:
|
161 |
usage_guide = """
|
162 |
<html>
|
|
|
9 |
from urllib.parse import parse_qs
|
10 |
from cachetools import TTLCache
|
11 |
|
12 |
+
# 创建一个TTL缓存,最多存储1000个项目,每个项目的有效期为30分钟
|
13 |
+
cache = TTLCache(maxsize=1000, ttl=1800) # 1800秒 = 30分钟
|
14 |
|
15 |
+
async def fetch_url(url, session, max_retries=3):
|
16 |
+
headers = {
|
17 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
|
18 |
+
}
|
19 |
for attempt in range(max_retries):
|
20 |
try:
|
21 |
+
async with session.get(url, headers=headers, timeout=ClientTimeout(total=120)) as response:
|
22 |
response.raise_for_status()
|
23 |
return await response.text()
|
|
|
|
|
24 |
except aiohttp.ClientError as e:
|
25 |
print(f"Attempt {attempt + 1} failed: {str(e)}", flush=True)
|
26 |
+
if attempt == max_retries - 1:
|
27 |
+
raise
|
28 |
+
await asyncio.sleep(1) # 在重试之前等待1秒
|
|
|
|
|
29 |
|
30 |
async def extract_and_transform_proxies(input_text):
|
|
|
|
|
|
|
|
|
31 |
try:
|
|
|
32 |
data = yaml.safe_load(input_text)
|
33 |
if isinstance(data, dict) and 'proxies' in data:
|
34 |
proxies_list = data['proxies']
|
35 |
+
elif isinstance(data, list):
|
36 |
+
proxies_list = data
|
37 |
else:
|
38 |
+
proxies_match = re.search(r'proxies:\s*\n((?:[-\s]*{.*\n?)*)', input_text, re.MULTILINE)
|
|
|
39 |
if proxies_match:
|
40 |
proxies_text = proxies_match.group(1)
|
41 |
proxies_list = yaml.safe_load(proxies_text)
|
42 |
else:
|
43 |
return "未找到有效的代理配置"
|
44 |
+
except yaml.YAMLError:
|
45 |
+
return "YAML解析错误"
|
46 |
|
47 |
if not proxies_list:
|
48 |
return "未找到有效的代理配置"
|
49 |
|
|
|
|
|
|
|
|
|
|
|
50 |
transformed_proxies = []
|
51 |
|
52 |
for proxy in proxies_list:
|
|
|
131 |
try:
|
132 |
print(f"Fetching URL: {url}", flush=True)
|
133 |
async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:
|
134 |
+
input_text = await fetch_url(url, session)
|
135 |
print(f"URL content length: {len(input_text)}", flush=True)
|
136 |
result = await extract_and_transform_proxies(input_text)
|
137 |
print(f"Transformed result length: {len(result)}", flush=True)
|
|
|
|
|
|
|
138 |
|
139 |
# 将结果存入缓存
|
140 |
cache[url] = result
|
141 |
|
142 |
return web.Response(text=result, content_type='text/plain')
|
143 |
except Exception as e:
|
144 |
+
print(f"Error processing request: {str(e)}", flush=True)
|
145 |
+
traceback.print_exc()
|
146 |
+
return web.Response(text=f"Error: {str(e)}", status=500)
|
147 |
else:
|
148 |
usage_guide = """
|
149 |
<html>
|