Muttered3 commited on
Commit
4ae6604
·
verified ·
1 Parent(s): 356e2ca

Update scraper.py

Browse files
Files changed (1) hide show
  1. scraper.py +40 -32
scraper.py CHANGED
@@ -1,54 +1,62 @@
1
  import asyncio
2
  import random
3
  import re
 
4
  from curl_cffi.requests import AsyncSession
5
 
6
- async def check_fragment(word: str) -> str:
7
  url = f"https://fragment.com/username/{word}"
8
 
9
- # Impersonate Google Chrome so Fragment stops shadow-banning/redirecting you
10
- async with AsyncSession(impersonate="chrome120") as session:
11
- for _ in range(4):
 
 
12
  try:
13
- # We allow redirects so we can catch Fragment's final destination URL
14
- resp = await session.get(url, timeout=12, allow_redirects=True)
15
-
16
  if resp.status_code in [429, 403]:
17
  await asyncio.sleep(random.uniform(1.5, 3.5))
18
  continue
19
-
20
  html = resp.text
21
-
22
- # 1. THE CANONICAL RULE (100% accurate Soft-404 detector based on your HTML)
23
- # If Fragment tries to shadow-ban us to the homepage, this catches it instantly.
24
- canonical = re.search(r'<link rel="canonical" href="([^"]+)"', html)
 
 
 
 
25
  if canonical and "/username/" not in canonical.group(1):
26
  return "UNAVAILABLE"
27
-
28
- # 2. EXACT STATUS EXTRACTION (From your uploaded HTML tags)
29
- status_match = re.search(r'class="tm-section-header-status[^>]*>([^<]+)</span>', html, re.IGNORECASE)
30
-
31
  if status_match:
32
  s = status_match.group(1).strip().lower()
33
  if "sold" in s: return "SOLD"
34
  if "taken" in s: return "TAKEN"
35
  if "auction" in s: return "ON_AUCTION"
36
  if "available" in s: return "AVAILABLE"
37
-
38
- # 3. LOG UNKNOWN STATUSES TO other.txt
39
- clean_status = s.upper()
40
- with open("other.txt", "a", encoding="utf-8") as f:
41
- f.write(f"{word} - {clean_status}\n")
42
- return clean_status
43
-
44
- # 4. Final Fallbacks
45
- if "tm-status-unavail" in html: return "UNAVAILABLE"
46
- if "Just a moment..." in html or "cloudflare" in html.lower():
47
- await asyncio.sleep(random.uniform(2, 4))
48
- continue
49
-
 
 
 
50
  except Exception:
51
  await asyncio.sleep(1)
52
- pass
53
-
54
- return "ERROR"
 
1
  import asyncio
2
  import random
3
  import re
4
+ import aiofiles
5
  from curl_cffi.requests import AsyncSession
6
 
7
+ async def check_fragment(word: str, proxy_url: str = None) -> str:
8
  url = f"https://fragment.com/username/{word}"
9
 
10
+ # Format proxy for curl_cffi (Applies to both HTTP and HTTPS traffic)
11
+ proxies = {"http": proxy_url, "https": proxy_url} if proxy_url else None
12
+
13
+ async with AsyncSession(impersonate="chrome120", proxies=proxies) as session:
14
+ for attempt in range(4):
15
  try:
16
+ resp = await session.get(url, timeout=15, allow_redirects=True)
17
+
 
18
  if resp.status_code in [429, 403]:
19
  await asyncio.sleep(random.uniform(1.5, 3.5))
20
  continue
21
+
22
  html = resp.text
23
+
24
+ # Cloudflare challenge - rotate proxy or wait
25
+ if "Just a moment..." in html or "cloudflare" in html.lower():
26
+ await asyncio.sleep(random.uniform(2, 4))
27
+ continue
28
+
29
+ # 1. Canonical Rule: If redirected to homepage, username is soft-banned/Unavailable
30
+ canonical = re.search(r'<link rel="canonical" href="([^"]+)"', html, re.IGNORECASE)
31
  if canonical and "/username/" not in canonical.group(1):
32
  return "UNAVAILABLE"
33
+
34
+ # 2. Extract specific status text
35
+ status_match = re.search(r'class="tm-section-header-status[^"]*"[^>]*>\s*([^<]+?)\s*</span>', html, re.IGNORECASE)
36
+
37
  if status_match:
38
  s = status_match.group(1).strip().lower()
39
  if "sold" in s: return "SOLD"
40
  if "taken" in s: return "TAKEN"
41
  if "auction" in s: return "ON_AUCTION"
42
  if "available" in s: return "AVAILABLE"
43
+ if "sale" in s or "purchase" in s: return "FOR_SALE"
44
+
45
+ # Async I/O for unknown statuses
46
+ clean = s.upper()
47
+ async with aiofiles.open("other.txt", "a", encoding="utf-8") as f:
48
+ await f.write(f"{word} - {clean} (Proxy: {proxy_url})\n")
49
+ return clean
50
+
51
+ # 3. Fallbacks
52
+ if "tm-status-taken" in html: return "TAKEN"
53
+ if "tm-status-unavail" in html:
54
+ # Prevent false Unavailable markings for Sold pages
55
+ if ">Sold<" in html or ">sold<" in html.lower():
56
+ return "SOLD"
57
+ return "UNAVAILABLE"
58
+
59
  except Exception:
60
  await asyncio.sleep(1)
61
+
62
+ return "ERROR"