Muttered3 commited on
Commit
d6ec5af
·
verified ·
1 Parent(s): 69f02a2

Update scraper.py

Browse files
Files changed (1) hide show
  1. scraper.py +49 -53
scraper.py CHANGED
@@ -1,62 +1,58 @@
1
  import asyncio
2
  import random
3
- from curl_cffi.requests import AsyncSession
4
 
5
- # We use free CORS proxies to hide your Server IP from Cloudflare
6
- PROXIES = [
7
- "", # 1st Attempt: Direct Connection
8
- "https://api.allorigins.win/raw?url=", # 2nd Attempt: Proxy Router 1
9
- "https://corsproxy.io/?", # 3rd Attempt: Proxy Router 2
10
  ]
11
 
12
  async def check_fragment(word: str) -> str:
13
- target_url = f"https://fragment.com/username/{word}"
14
 
15
- headers = {
16
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
17
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
18
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Spoof a real Google Chrome browser fingerprint
21
- async with AsyncSession(impersonate="chrome120") as session:
22
- for proxy in PROXIES:
23
- url = f"{proxy}{target_url}"
 
 
 
 
24
 
25
- try:
26
- resp = await session.get(url, headers=headers, timeout=15)
27
- html = resp.text
28
-
29
- # 1. Did we hit the Cloudflare Wall?
30
- if resp.status_code in [403, 429] or "Just a moment..." in html or "cf-browser-verification" in html:
31
- proxy_name = "Proxy" if proxy else "Direct HF IP"
32
- print(f"⚠️ [CLOUDFLARE BLOCK] Blocked on {proxy_name} for @{word}. Retrying...")
33
- await asyncio.sleep(random.uniform(2, 4))
34
- continue # Try the next proxy in the list
35
-
36
- # 2. Success! Parse the HTML accurately
37
- if resp.status_code == 200:
38
-
39
- if "tm-status-avail" in html:
40
- print(f"✅ [SUCCESS] @{word} is AVAILABLE!")
41
- return "AVAILABLE"
42
-
43
- if "tm-status-taken" in html:
44
- if "On auction" in html or "tm-section-subscribe" in html: return "ON_AUCTION"
45
- if "For sale" in html: return "FOR_SALE"
46
- if ">Sold for" in html or ">Sold at" in html or 'class="tm-status-text">Sold' in html: return "SOLD"
47
- return "TAKEN"
48
-
49
- if "tm-status-unavail" in html:
50
- return "UNAVAILABLE"
51
-
52
- # If Fragment loads but the layout changed
53
- print(f"❓ [UNKNOWN HTML] Couldn't parse @{word}. Fragment may have changed their website.")
54
- return "ERROR"
55
-
56
- except Exception as e:
57
- print(f"❌ [NETWORK ERROR] Failed to load @{word}: {str(e)}")
58
- continue
59
-
60
- # If all proxies fail, DO NOT label as Unavail. Label as ERROR to protect the database.
61
- print(f"💀 [FATAL] All proxies blocked for @{word}.")
62
- return "ERROR"
 
1
  import asyncio
2
  import random
3
+ import aiohttp
4
 
5
+ USER_AGENTS = [
6
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
7
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Safari/605.1.15",
8
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/123.0 Safari/537.36",
 
9
  ]
10
 
11
  async def check_fragment(word: str) -> str:
12
+ url = f"https://fragment.com/username/{word}"
13
 
14
+ async with aiohttp.ClientSession() as session:
15
+ # Your original 6 retries with 3 attempts
16
+ for _ in range(6):
17
+ for attempt in range(3):
18
+ headers = {"User-Agent": random.choice(USER_AGENTS)}
19
+ try:
20
+ async with session.get(url, headers=headers, allow_redirects=True, timeout=15) as resp:
21
+ # Your original exponential backoff for rate limits
22
+ if resp.status == 429:
23
+ wait = (2 ** attempt * 3) + random.uniform(1, 3)
24
+ await asyncio.sleep(wait)
25
+ continue
26
+
27
+ if resp.status == 200:
28
+ html = await resp.text()
29
+ await asyncio.sleep(random.uniform(0.15, 0.6))
30
+
31
+ # Original fast parsing
32
+ if "tm-status-avail" in html:
33
+ return "AVAILABLE"
34
+
35
+ if "tm-status-taken" in html:
36
+ if "On auction" in html or "tm-section-subscribe" in html:
37
+ return "ON_AUCTION"
38
+ if "For sale" in html:
39
+ return "FOR_SALE"
40
+ if ">Sold for" in html or ">Sold at" in html or 'class="tm-status-text">Sold' in html:
41
+ return "SOLD"
42
+ return "TAKEN"
43
+
44
+ if "tm-status-unavail" in html:
45
+ return "UNAVAILABLE"
46
 
47
+ # Fallbacks
48
+ if "Available" in html: return "AVAILABLE"
49
+ if "Taken" in html: return "TAKEN"
50
+
51
+ return "UNAVAILABLE"
52
+
53
+ except Exception:
54
+ pass
55
 
56
+ await asyncio.sleep(random.uniform(0.8, 2.0))
57
+
58
+ return "ERROR"