Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from asyncio import Queue, create_task, gather, sleep
|
2 |
from contextlib import asynccontextmanager
|
3 |
from json import dumps, loads
|
|
|
4 |
from pathlib import Path
|
5 |
from typing import Literal
|
6 |
|
@@ -10,12 +11,24 @@ from fastapi.responses import JSONResponse, PlainTextResponse
|
|
10 |
from proxybroker import Broker
|
11 |
from uvicorn import run as uvicorn_run
|
12 |
|
|
|
13 |
scheduler = AsyncIOScheduler()
|
|
|
14 |
try:
|
15 |
workdir = Path(__file__).parent
|
16 |
except:
|
17 |
workdir = Path.cwd().parent
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
is_first_run = True
|
20 |
|
21 |
http_collected_json = workdir / 'http_proxies.json'
|
@@ -95,19 +108,23 @@ async def stop_broker_after_timeout(broker: Broker, timeout_minutes: int):
|
|
95 |
|
96 |
|
97 |
async def find_proxies_by_type(proxy_type: Literal['HTTP', 'HTTPS', 'SOCKS5'], output_json_file: Path, timeout_minutes: int = 50):
|
|
|
98 |
output_json_file.write_text(dumps({'countries': None, 'proxies': []}, indent=4))
|
99 |
proxies_queue = Queue()
|
100 |
-
broker = Broker(proxies_queue, timeout=
|
101 |
stop_task = create_task(stop_broker_after_timeout(broker, timeout_minutes))
|
102 |
await broker.find(types=[proxy_type], countries=countries_list, limit=0)
|
103 |
await stop_task
|
104 |
proxies_list = await collect_proxies(proxies_queue)
|
105 |
-
|
|
|
|
|
106 |
|
107 |
|
108 |
async def find_proxies():
|
109 |
global is_first_run
|
110 |
timeout_minutes = 10 if is_first_run else 50
|
|
|
111 |
results = await gather(
|
112 |
find_proxies_by_type('HTTP', http_collected_json, timeout_minutes),
|
113 |
find_proxies_by_type('HTTPS', https_collected_json, timeout_minutes),
|
@@ -115,6 +132,7 @@ async def find_proxies():
|
|
115 |
)
|
116 |
await sort_proxies_and_merge(list(results), collected_json)
|
117 |
is_first_run = False
|
|
|
118 |
|
119 |
|
120 |
scheduler.add_job(find_proxies, 'interval', max_instances=1, minutes=60)
|
@@ -173,6 +191,14 @@ async def get_socks5_proxies():
|
|
173 |
return not_redy_yet()
|
174 |
|
175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
@app.get('/')
|
177 |
async def read_root():
|
178 |
return PlainTextResponse('ну пролапс, ну и что', status_code=200)
|
|
|
1 |
from asyncio import Queue, create_task, gather, sleep
|
2 |
from contextlib import asynccontextmanager
|
3 |
from json import dumps, loads
|
4 |
+
from logging import INFO, basicConfig, getLogger
|
5 |
from pathlib import Path
|
6 |
from typing import Literal
|
7 |
|
|
|
11 |
from proxybroker import Broker
|
12 |
from uvicorn import run as uvicorn_run
|
13 |
|
14 |
+
|
15 |
scheduler = AsyncIOScheduler()
|
16 |
+
|
17 |
try:
|
18 |
workdir = Path(__file__).parent
|
19 |
except:
|
20 |
workdir = Path.cwd().parent
|
21 |
|
22 |
+
logfile = workdir / 'log.log'
|
23 |
+
|
24 |
+
basicConfig(
|
25 |
+
level=INFO,
|
26 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
27 |
+
filename=str(logfile),
|
28 |
+
filemode='a'
|
29 |
+
)
|
30 |
+
logger = getLogger('proxy_collector')
|
31 |
+
|
32 |
is_first_run = True
|
33 |
|
34 |
http_collected_json = workdir / 'http_proxies.json'
|
|
|
108 |
|
109 |
|
110 |
async def find_proxies_by_type(proxy_type: Literal['HTTP', 'HTTPS', 'SOCKS5'], output_json_file: Path, timeout_minutes: int = 50):
|
111 |
+
logger.info(f'начат сбор прокси {proxy_type}')
|
112 |
output_json_file.write_text(dumps({'countries': None, 'proxies': []}, indent=4))
|
113 |
proxies_queue = Queue()
|
114 |
+
broker = Broker(proxies_queue, timeout=8, max_conn=200, max_tries=3, verify_ssl=False)
|
115 |
stop_task = create_task(stop_broker_after_timeout(broker, timeout_minutes))
|
116 |
await broker.find(types=[proxy_type], countries=countries_list, limit=0)
|
117 |
await stop_task
|
118 |
proxies_list = await collect_proxies(proxies_queue)
|
119 |
+
saved_proxy = create_json_from_proxies(proxies_list, output_json_file)
|
120 |
+
logger.info(f'завершён сбор прокси {proxy_type}')
|
121 |
+
return saved_proxy
|
122 |
|
123 |
|
124 |
async def find_proxies():
|
125 |
global is_first_run
|
126 |
timeout_minutes = 10 if is_first_run else 50
|
127 |
+
logger.info(f'запущены задачи по сбору всех типов прокси')
|
128 |
results = await gather(
|
129 |
find_proxies_by_type('HTTP', http_collected_json, timeout_minutes),
|
130 |
find_proxies_by_type('HTTPS', https_collected_json, timeout_minutes),
|
|
|
132 |
)
|
133 |
await sort_proxies_and_merge(list(results), collected_json)
|
134 |
is_first_run = False
|
135 |
+
logger.info(f'задачи по сбору прокси завершены')
|
136 |
|
137 |
|
138 |
scheduler.add_job(find_proxies, 'interval', max_instances=1, minutes=60)
|
|
|
191 |
return not_redy_yet()
|
192 |
|
193 |
|
194 |
+
@app.get('/log/')
|
195 |
+
async def get_logs():
|
196 |
+
if logfile.exists():
|
197 |
+
return PlainTextResponse(logfile.read_text(encoding='utf-8'), status_code=200)
|
198 |
+
else:
|
199 |
+
return PlainTextResponse('лог пуст', status_code=200)
|
200 |
+
|
201 |
+
|
202 |
@app.get('/')
|
203 |
async def read_root():
|
204 |
return PlainTextResponse('ну пролапс, ну и что', status_code=200)
|