Hana Celeste commited on
Update app/aternos_api.py
Browse files- app/aternos_api.py +55 -49
app/aternos_api.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
from playwright.async_api import async_playwright
|
| 2 |
-
import asyncio
|
| 3 |
|
| 4 |
PROXIES = [
|
| 5 |
"http://szusjfnw:mftzj9sljp5t@31.59.20.176:6754",
|
|
@@ -15,59 +14,66 @@ PROXIES = [
|
|
| 15 |
]
|
| 16 |
|
| 17 |
class AternosAPI:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
async def check_with_proxy(self, proxy: str):
|
| 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 |
-
blocked
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return {
|
| 56 |
-
"proxy": proxy,
|
| 57 |
-
"ok": not blocked,
|
| 58 |
-
"status": resp.status if resp else None,
|
| 59 |
-
"title": title,
|
| 60 |
-
"final_url": url,
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
except Exception as e:
|
| 64 |
-
return {
|
| 65 |
-
"proxy": proxy,
|
| 66 |
-
"ok": False,
|
| 67 |
-
"error": str(e),
|
| 68 |
-
}
|
| 69 |
-
finally:
|
| 70 |
-
await browser.close()
|
| 71 |
|
| 72 |
async def find_working_proxy(self, proxies):
|
| 73 |
for proxy in proxies:
|
|
|
|
| 1 |
from playwright.async_api import async_playwright
|
|
|
|
| 2 |
|
| 3 |
PROXIES = [
|
| 4 |
"http://szusjfnw:mftzj9sljp5t@31.59.20.176:6754",
|
|
|
|
| 14 |
]
|
| 15 |
|
| 16 |
class AternosAPI:
|
| 17 |
+
def __init__(self):
|
| 18 |
+
self.playwright = None
|
| 19 |
+
|
| 20 |
+
async def start(self):
|
| 21 |
+
# preload playwright (đỡ delay request đầu)
|
| 22 |
+
self.playwright = await async_playwright().start()
|
| 23 |
+
|
| 24 |
+
async def stop(self):
|
| 25 |
+
if self.playwright:
|
| 26 |
+
await self.playwright.stop()
|
| 27 |
+
self.playwright = None
|
| 28 |
+
|
| 29 |
async def check_with_proxy(self, proxy: str):
|
| 30 |
+
if not self.playwright:
|
| 31 |
+
raise RuntimeError("AternosAPI not started")
|
| 32 |
+
|
| 33 |
+
browser = await self.playwright.chromium.launch(
|
| 34 |
+
headless=True,
|
| 35 |
+
proxy={"server": proxy},
|
| 36 |
+
args=[
|
| 37 |
+
"--no-sandbox",
|
| 38 |
+
"--disable-dev-shm-usage",
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
context = await browser.new_context(
|
| 43 |
+
user_agent=(
|
| 44 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 45 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 46 |
+
"Chrome/120.0.0.0 Safari/537.36"
|
| 47 |
)
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
page = await context.new_page()
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
resp = await page.goto(
|
| 54 |
+
"https://aternos.org/server/",
|
| 55 |
+
wait_until="domcontentloaded",
|
| 56 |
+
timeout=20000
|
| 57 |
)
|
| 58 |
|
| 59 |
+
title = await page.title()
|
| 60 |
+
html = await page.content()
|
| 61 |
+
|
| 62 |
+
blocked = (
|
| 63 |
+
"just a moment" in title.lower()
|
| 64 |
+
or "cloudflare" in html.lower()
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
return {
|
| 68 |
+
"ok": not blocked,
|
| 69 |
+
"status": resp.status if resp else None,
|
| 70 |
+
"title": title,
|
| 71 |
+
"final_url": page.url,
|
| 72 |
+
"blocked": blocked,
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
finally:
|
| 76 |
+
await browser.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
async def find_working_proxy(self, proxies):
|
| 79 |
for proxy in proxies:
|