Hana Celeste commited on
Commit
3f1fc1b
·
verified ·
1 Parent(s): 8d1a2e9

Update app/aternos_api.py

Browse files
Files changed (1) hide show
  1. app/aternos_api.py +34 -38
app/aternos_api.py CHANGED
@@ -1,6 +1,5 @@
1
- from playwright.async_api import async_playwright, TimeoutError as PWTimeout
2
- import asyncio
3
- import time
4
 
5
  PROXY = {
6
  "server": "http://proxy.scrapingant.com:8080",
@@ -10,38 +9,39 @@ PROXY = {
10
 
11
  class AternosAPI:
12
  async def check_access(self):
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-blink-features=AutomationControlled",
21
- ]
22
- )
 
 
 
23
 
24
- context = await browser.new_context(
25
- user_agent=(
26
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
27
- "AppleWebKit/537.36 (KHTML, like Gecko) "
28
- "Chrome/121.0.0.0 Safari/537.36"
29
- ),
30
- locale="en-US",
31
- timezone_id="America/New_York",
32
- viewport={"width": 1366, "height": 768},
33
- )
34
 
35
- page = await context.new_page()
36
 
37
- try:
38
  await page.goto(
39
  "https://aternos.org/server/",
40
  wait_until="networkidle",
41
  timeout=30000
42
  )
43
 
44
- # ===== chờ Cloudflare thả
45
  start = time.time()
46
  cleared = False
47
 
@@ -49,10 +49,8 @@ class AternosAPI:
49
  title = await page.title()
50
  html = await page.content()
51
 
52
- if (
53
- "just a moment" not in title.lower()
54
- and "cloudflare" not in html.lower()
55
- ):
56
  cleared = True
57
  break
58
 
@@ -62,7 +60,6 @@ class AternosAPI:
62
  "ok": cleared,
63
  "final_url": page.url,
64
  "title": await page.title(),
65
- "content_length": len(await page.content()),
66
  "blocked": not cleared,
67
  "reason": None if cleared else "cloudflare_challenge"
68
  }
@@ -70,10 +67,9 @@ class AternosAPI:
70
  await browser.close()
71
  return result
72
 
73
- except PWTimeout:
74
- await browser.close()
75
- return {
76
- "ok": False,
77
- "blocked": True,
78
- "reason": "timeout"
79
- }
 
1
+ from playwright.async_api import async_playwright
2
+ import asyncio, time, traceback
 
3
 
4
  PROXY = {
5
  "server": "http://proxy.scrapingant.com:8080",
 
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
 
 
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
 
 
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
  }
 
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
+ }