Hana Celeste commited on
Commit
0aa2bce
·
verified ·
1 Parent(s): e2b6def

Update app/fetch.py

Browse files
Files changed (1) hide show
  1. app/fetch.py +37 -79
app/fetch.py CHANGED
@@ -1,95 +1,53 @@
1
- import random
2
- import json
3
  from fastapi import HTTPException
4
- from playwright.async_api import async_playwright, TimeoutError
5
 
6
- UAS = [
7
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
8
- "(KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
9
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
10
- "(KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36",
11
- ]
12
 
13
- PROXY = {
14
- "server": "http://proxy.scrapingant.com:8080",
15
- "username": "scrapingant&proxy_type=residential&proxy_country=VN&browser=false",
16
- "password": "fdd15bd8d6f042f893d8bb93a8e47722",
17
- }
18
 
19
 
20
  class Fetcher:
21
- def __init__(self):
22
- self.pw = None
23
- self.browser = None
24
-
25
  async def start(self):
26
- self.pw = await async_playwright().start()
27
- self.browser = await self.pw.chromium.launch(
28
- headless=True,
29
- proxy=PROXY,
30
- args=[
31
- "--no-sandbox",
32
- "--disable-dev-shm-usage",
33
- "--disable-blink-features=AutomationControlled",
34
- ],
35
- )
36
 
37
  async def stop(self):
38
- if self.browser:
39
- await self.browser.close()
40
- if self.pw:
41
- await self.pw.stop()
42
 
43
  async def fetch(self, url: str):
44
- context = await self.browser.new_context(
45
- user_agent=random.choice(UAS),
46
- locale="en-US",
47
- timezone_id="Asia/Ho_Chi_Minh",
48
- extra_http_headers={
49
- "Accept": "application/json, text/plain, */*",
50
- "Accept-Language": "en-US,en;q=0.9",
51
- },
52
- )
53
-
54
- page = await context.new_page()
55
 
56
  try:
57
- # warm CF
58
- await page.goto(
59
- "https://ihentai.dog/",
60
- wait_until="domcontentloaded",
61
- timeout=20000,
62
- )
63
- await page.wait_for_timeout(1500)
64
-
65
- resp = await page.goto(
66
- url,
67
- wait_until="domcontentloaded",
68
- timeout=20000,
69
- )
70
-
71
- if not resp:
72
- raise HTTPException(500, "No response")
73
-
74
- status = resp.status
75
- body = await resp.text()
76
-
77
- if status >= 400:
78
- raise HTTPException(status, "Upstream error")
79
-
80
  try:
81
- data = json.loads(body)
82
  except Exception:
83
- data = body
84
-
85
- return {
86
- "status": status,
87
- "data": data,
88
- }
89
-
90
- except TimeoutError:
91
- raise HTTPException(504, "Timeout")
92
 
93
- finally:
94
- await page.close()
95
- await context.close()
 
1
+ import httpx
 
2
  from fastapi import HTTPException
3
+ from urllib.parse import quote_plus
4
 
 
 
 
 
 
 
5
 
6
+ SCRAPINGANT_KEY = "fdd15bd8d6f042f893d8bb93a8e47722"
7
+ SCRAPINGANT_ENDPOINT = "https://api.scrapingant.com/v2/general"
 
 
 
8
 
9
 
10
  class Fetcher:
 
 
 
 
11
  async def start(self):
12
+ # không cần init gì
13
+ pass
 
 
 
 
 
 
 
 
14
 
15
  async def stop(self):
16
+ pass
 
 
 
17
 
18
  async def fetch(self, url: str):
19
+ params = {
20
+ "url": url,
21
+ "x-api-key": SCRAPINGANT_KEY,
22
+ "proxy_type": "residential",
23
+ "proxy_country": "VN",
24
+ "browser": "false",
25
+ }
 
 
 
 
26
 
27
  try:
28
+ async with httpx.AsyncClient(timeout=20) as client:
29
+ resp = await client.get(
30
+ SCRAPINGANT_ENDPOINT,
31
+ params=params,
32
+ headers={
33
+ "Accept": "application/json, text/plain, */*",
34
+ "User-Agent": "Mozilla/5.0",
35
+ },
36
+ )
37
+
38
+ if resp.status_code != 200:
39
+ raise HTTPException(
40
+ status_code=resp.status_code,
41
+ detail="Upstream error",
42
+ )
43
+
44
+ text = resp.text.strip()
45
+
46
+ # ScrapingAnt trả JSON thẳng nếu API
 
 
 
 
47
  try:
48
+ return resp.json()
49
  except Exception:
50
+ return {"raw": text}
 
 
 
 
 
 
 
 
51
 
52
+ except httpx.RequestError as e:
53
+ raise HTTPException(502, f"Proxy error: {str(e)}")