Muttered3 commited on
Commit
93d81a1
·
verified ·
1 Parent(s): 41f0350

Update scraper.py

Browse files
Files changed (1) hide show
  1. scraper.py +38 -49
scraper.py CHANGED
@@ -13,56 +13,45 @@ async def check_fragment(word: str) -> str:
13
  url = f"https://fragment.com/username/{word}"
14
 
15
  async with aiohttp.ClientSession() as session:
16
- for _ in range(6):
17
- for attempt in range(3):
18
  headers = {"User-Agent": random.choice(USER_AGENTS)}
19
- try:
20
- # We allow redirects so we can catch Fragment's final destination URL
21
- async with session.get(url, headers=headers, allow_redirects=True, timeout=15) as resp:
 
 
 
22
 
23
- if resp.status == 429:
24
- wait = (2 ** attempt * 3) + random.uniform(1, 3)
25
- await asyncio.sleep(wait)
26
- continue
27
-
28
- if resp.status == 200:
29
- html = await resp.text()
30
- final_url = str(resp.url)
31
-
32
- # 1. THE SOFT-REDIRECT DETECTOR (Based on your uploaded HTML)
33
- # If Fragment quietly drops us on the homepage, it means the word is Unavailable.
34
- if final_url == "https://fragment.com/" or "/?query=" in final_url:
35
- return "UNAVAILABLE"
36
-
37
- if "<title>Fragment</title>" in html or 'href="https://fragment.com/"' in html:
38
- return "UNAVAILABLE"
39
-
40
- # 2. Extract the exact status from Fragment's HTML header
41
- match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', html, re.IGNORECASE)
42
-
43
- if match:
44
- status_text = match.group(1).strip().lower()
45
-
46
- if status_text == "sold": return "SOLD"
47
- if status_text == "taken": return "TAKEN"
48
- if status_text == "on auction": return "ON_AUCTION"
49
- if status_text == "available": return "AVAILABLE"
50
-
51
- # 3. Unknown status logging
52
- clean_status = status_text.upper()
53
- with open("other.txt", "a", encoding="utf-8") as f:
54
- f.write(f"{word} - {clean_status}\n")
55
- return clean_status
56
-
57
- # 4. Final Fail-safes
58
- if "tm-status-unavail" in html: return "UNAVAILABLE"
59
- if "This username is unavailable" in html: return "UNAVAILABLE"
60
-
61
- return "UNAVAILABLE"
62
-
63
- except Exception:
64
- pass
65
-
66
- await asyncio.sleep(random.uniform(0.8, 2.0))
67
 
68
  return "ERROR"
 
13
  url = f"https://fragment.com/username/{word}"
14
 
15
  async with aiohttp.ClientSession() as session:
16
+ for _ in range(4):
17
+ try:
18
  headers = {"User-Agent": random.choice(USER_AGENTS)}
19
+ # allow_redirects=True so we can catch the canonical link
20
+ async with session.get(url, headers=headers, allow_redirects=True, timeout=12) as resp:
21
+
22
+ if resp.status in [429, 403]:
23
+ await asyncio.sleep(random.uniform(1.5, 3.5))
24
+ continue
25
 
26
+ html = await resp.text()
27
+
28
+ # 1. THE CANONICAL RULE (100% accurate Soft-404 detection)
29
+ # If Fragment drops us on the homepage, the canonical link changes.
30
+ canonical = re.search(r'<link rel="canonical" href="([^"]+)"', html)
31
+ if canonical and "/username/" not in canonical.group(1):
32
+ return "UNAVAILABLE"
33
+
34
+ # 2. EXACT STATUS EXTRACTION (From your uploaded HTML tags)
35
+ status_match = re.search(r'class="tm-section-header-status[^>]*>([^<]+)</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
+
44
+ # 3. LOG UNKNOWN STATUSES
45
+ clean_status = s.upper()
46
+ with open("other.txt", "a", encoding="utf-8") as f:
47
+ f.write(f"{word} - {clean_status}\n")
48
+ return clean_status
49
+
50
+ # 4. Final Fallback
51
+ if "tm-status-unavail" in html: return "UNAVAILABLE"
52
+
53
+ except Exception:
54
+ await asyncio.sleep(1)
55
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  return "ERROR"