Hana Celeste commited on
Commit
3add0ff
·
verified ·
1 Parent(s): 52a4854

Update app/aternos_api.py

Browse files
Files changed (1) hide show
  1. 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, traceback
3
 
4
  PROXY = {
5
  "server": "http://proxy.scrapingant.com:8080",
@@ -8,68 +8,60 @@ PROXY = {
8
  }
9
 
10
  class AternosAPI:
11
- async def check_access(self):
12
- try:
13
- async with async_playwright() as p:
14
- browser = await p.chromium.launch(
15
- headless=True,
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
- page = await context.new_page()
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- await page.goto(
40
- "https://aternos.org/server/",
41
- wait_until="networkidle",
42
- timeout=30000
43
- )
 
 
 
 
 
44
 
45
- start = time.time()
46
- cleared = False
47
 
48
- while time.time() - start < 30:
49
- title = await page.title()
50
- html = await page.content()
51
-
52
- if "just a moment" not in title.lower() \
53
- and "cloudflare" not in html.lower():
54
- cleared = True
55
- break
56
 
57
- await asyncio.sleep(2)
 
 
 
58
 
59
- result = {
60
- "ok": cleared,
61
- "final_url": page.url,
62
- "title": await page.title(),
63
- "blocked": not cleared,
64
- "reason": None if cleared else "cloudflare_challenge"
65
  }
66
 
67
- await browser.close()
68
- return result
69
 
70
- except Exception as e:
71
- return {
72
- "ok": False,
73
- "error": str(e),
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
+ }