Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from asyncio import Queue, create_task
|
| 2 |
+
from contextlib import asynccontextmanager
|
| 3 |
+
from json import dumps, load, loads
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from proxybroker import Broker
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from fastapi.responses import PlainTextResponse, JSONResponse
|
| 8 |
+
from uvicorn import run as uvicorn_run
|
| 9 |
+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
| 10 |
+
|
| 11 |
+
scheduler = AsyncIOScheduler()
|
| 12 |
+
try:
|
| 13 |
+
workdir = Path(__file__).parent
|
| 14 |
+
except:
|
| 15 |
+
workdir = Path.cwd().parent
|
| 16 |
+
|
| 17 |
+
collected_json = workdir / 'proxies.json'
|
| 18 |
+
countries_list = ['US', 'CA', 'FR', 'FI', 'HR', 'ME', 'CH', 'SE', 'EE', 'DE', 'GB', 'IT', 'NL', 'PL', 'CZ', 'RS', 'RO', 'MD', 'AT', 'BE', 'BG', 'HU', 'DK', 'IS', 'KZ', 'LV', 'LT', 'LU', 'NO', 'PT', 'SK', 'SI']
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def create_json_from_proxies(proxy_lines, filename):
|
| 22 |
+
countries = set()
|
| 23 |
+
proxies = []
|
| 24 |
+
|
| 25 |
+
for line in proxy_lines:
|
| 26 |
+
parts = line.split()
|
| 27 |
+
country = parts[1]
|
| 28 |
+
ping = float(parts[2].strip('s'))
|
| 29 |
+
protocol = parts[3].strip('[]')
|
| 30 |
+
host = parts[4].rstrip('>')
|
| 31 |
+
|
| 32 |
+
if "HTTP:" in protocol:
|
| 33 |
+
protocol = "HTTP"
|
| 34 |
+
host = parts[5].rstrip(']>')
|
| 35 |
+
|
| 36 |
+
countries.add(country)
|
| 37 |
+
proxies.append({"country": country, "ping": ping, "protocol": protocol, "host": host})
|
| 38 |
+
|
| 39 |
+
data = {
|
| 40 |
+
'countries': sorted(list(countries)),
|
| 41 |
+
'proxies': proxies
|
| 42 |
+
}
|
| 43 |
+
filename.write_text(dumps(data, indent=4))
|
| 44 |
+
return filename
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
async def collect_proxies(proxies_queue):
|
| 48 |
+
proxies_list = []
|
| 49 |
+
while True:
|
| 50 |
+
proxy = await proxies_queue.get()
|
| 51 |
+
if proxy is None:
|
| 52 |
+
break
|
| 53 |
+
proxies_list.append(f'{proxy}')
|
| 54 |
+
print(f"\rtotal proxies: {len(proxies_list)}", end='')
|
| 55 |
+
return proxies_list
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
async def find_proxies():
|
| 59 |
+
collected_json.write_text(dumps({'countries': None, 'proxies': []}, indent=4))
|
| 60 |
+
proxies_queue = Queue()
|
| 61 |
+
broker = Broker(proxies_queue, timeout=5, max_conn=200, max_tries=3, verify_ssl=True)
|
| 62 |
+
await broker.find(types=['HTTP', 'HTTPS', 'SOCKS5'], countries=countries_list, limit=150)
|
| 63 |
+
proxies_list = await collect_proxies(proxies_queue)
|
| 64 |
+
return create_json_from_proxies(proxies_list, collected_json)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
scheduler.add_job(find_proxies, 'interval', max_instances=1, minutes=60)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
@asynccontextmanager
|
| 71 |
+
async def app_lifespan(app: FastAPI):
|
| 72 |
+
scheduler.start()
|
| 73 |
+
task = create_task(find_proxies())
|
| 74 |
+
yield
|
| 75 |
+
await task
|
| 76 |
+
scheduler.shutdown()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
app = FastAPI(lifespan=app_lifespan)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@app.post('*')
|
| 83 |
+
async def read_root():
|
| 84 |
+
return HTTPException(405)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@app.get('/proxies/')
|
| 88 |
+
async def get_proxies():
|
| 89 |
+
if collected_json.exists():
|
| 90 |
+
return loads(collected_json.read_text())
|
| 91 |
+
else:
|
| 92 |
+
return JSONResponse({"error": "Proxy list is not ready yet."}, status_code=204)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
@app.get('/')
|
| 96 |
+
async def read_root():
|
| 97 |
+
return PlainTextResponse('ну пролапс, ну и что', status_code=200)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
uvicorn_run(app, host='127.0.0.1', port=7860, timeout_keep_alive=90)
|