Hana Celeste commited on
Update app/aternos_api.py
Browse files- app/aternos_api.py +49 -57
app/aternos_api.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from playwright.async_api import async_playwright
|
| 2 |
-
import asyncio, time
|
| 3 |
|
| 4 |
PROXY = {
|
| 5 |
"server": "http://proxy.scrapingant.com:8080",
|
|
@@ -8,68 +8,60 @@ PROXY = {
|
|
| 8 |
}
|
| 9 |
|
| 10 |
class AternosAPI:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
proxy=PROXY,
|
| 17 |
-
args=[
|
| 18 |
-
"--no-sandbox",
|
| 19 |
-
"--disable-dev-shm-usage",
|
| 20 |
-
"--disable-gpu",
|
| 21 |
-
"--single-process",
|
| 22 |
-
"--disable-blink-features=AutomationControlled",
|
| 23 |
-
]
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
context = await browser.new_context(
|
| 27 |
-
user_agent=(
|
| 28 |
-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 29 |
-
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 30 |
-
"Chrome/121.0.0.0 Safari/537.36"
|
| 31 |
-
),
|
| 32 |
-
locale="en-US",
|
| 33 |
-
timezone_id="America/New_York",
|
| 34 |
-
viewport={"width": 1366, "height": 768},
|
| 35 |
-
)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
cleared = False
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
cleared = True
|
| 55 |
-
break
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"reason": None if cleared else "cloudflare_challenge"
|
| 65 |
}
|
| 66 |
|
| 67 |
-
|
| 68 |
-
return result
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
"trace": traceback.format_exc()
|
| 75 |
-
}
|
|
|
|
| 1 |
from playwright.async_api import async_playwright
|
| 2 |
+
import asyncio, time
|
| 3 |
|
| 4 |
PROXY = {
|
| 5 |
"server": "http://proxy.scrapingant.com:8080",
|
|
|
|
| 8 |
}
|
| 9 |
|
| 10 |
class AternosAPI:
|
| 11 |
+
def __init__(self):
|
| 12 |
+
self.pw = None
|
| 13 |
+
self.browser = None
|
| 14 |
+
self.context = None
|
| 15 |
+
self.page = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
async def start(self):
|
| 18 |
+
self.pw = await async_playwright().start()
|
| 19 |
+
self.browser = await self.pw.chromium.launch(
|
| 20 |
+
headless=True,
|
| 21 |
+
proxy=PROXY,
|
| 22 |
+
args=[
|
| 23 |
+
"--no-sandbox",
|
| 24 |
+
"--disable-dev-shm-usage",
|
| 25 |
+
"--disable-gpu",
|
| 26 |
+
"--disable-blink-features=AutomationControlled",
|
| 27 |
+
]
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
self.context = await self.browser.new_context(
|
| 31 |
+
user_agent=(
|
| 32 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 33 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 34 |
+
"Chrome/121.0.0.0 Safari/537.36"
|
| 35 |
+
),
|
| 36 |
+
locale="en-US",
|
| 37 |
+
timezone_id="America/New_York",
|
| 38 |
+
viewport={"width": 1366, "height": 768},
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
self.page = await self.context.new_page()
|
|
|
|
| 42 |
|
| 43 |
+
async def check_access(self):
|
| 44 |
+
await self.page.goto(
|
| 45 |
+
"https://aternos.org/server/",
|
| 46 |
+
wait_until="networkidle",
|
| 47 |
+
timeout=60000
|
| 48 |
+
)
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
start = time.time()
|
| 51 |
+
while time.time() - start < 40:
|
| 52 |
+
title = await self.page.title()
|
| 53 |
+
html = await self.page.content()
|
| 54 |
|
| 55 |
+
if "just a moment" not in title.lower() \
|
| 56 |
+
and "cloudflare" not in html.lower():
|
| 57 |
+
return {
|
| 58 |
+
"ok": True,
|
| 59 |
+
"final_url": self.page.url
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
+
await asyncio.sleep(2)
|
|
|
|
| 63 |
|
| 64 |
+
return {
|
| 65 |
+
"ok": False,
|
| 66 |
+
"reason": "cloudflare_challenge"
|
| 67 |
+
}
|
|
|
|
|
|