Muttered3 commited on
Commit
a38c4aa
·
verified ·
1 Parent(s): e6fabf5

Update scraper.py

Browse files
Files changed (1) hide show
  1. scraper.py +24 -23
scraper.py CHANGED
@@ -17,51 +17,52 @@ async def check_fragment(word: str) -> str:
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
 
22
- # Handle standard rate limits
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
 
31
- # 1. The True Unavailable Check (Redirects to search page)
32
- if "/?query=" in str(resp.url):
33
- return "UNAVAILABLE"
34
-
35
- # 2. Did Fragment hit us with a silent Captcha?
36
- if "Just a moment..." in html or "Cloudflare" in html:
37
- await asyncio.sleep(random.uniform(3, 6))
38
- continue
39
-
40
- # 3. Extract exact status (re.DOTALL handles hidden newlines in HTML)
41
- match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', html, re.IGNORECASE | re.DOTALL)
42
 
43
  if match:
44
  status_text = match.group(1).strip().lower()
45
 
46
- if "sold" in status_text: return "SOLD"
47
- if "taken" in status_text: return "TAKEN"
48
- if "auction" in status_text: return "ON_AUCTION"
49
- if "available" in status_text: return "AVAILABLE"
50
 
51
- # Write unknown exact statuses to other.txt
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. CRITICAL FIX: The Blind Fallback
58
- # If we get here, the regex failed. DO NOT return UNAVAILABLE.
59
- # Return ERROR so it ignores the word and doesn't permanently ruin your text files.
60
- return "ERROR"
61
 
62
  except Exception:
63
  pass
64
 
65
  await asyncio.sleep(random.uniform(0.8, 2.0))
66
 
67
- return "ERROR"
 
17
  for attempt in range(3):
18
  headers = {"User-Agent": random.choice(USER_AGENTS)}
19
  try:
20
+ # CRITICAL FIX: allow_redirects=False stops Fragment from bouncing us to the homepage!
21
+ async with session.get(url, headers=headers, allow_redirects=False, 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
+ # FAST-FAIL REDIRECT CHECK:
29
+ # If Fragment issues a 302 redirect to the search page, the word is UNAVAILABLE.
30
+ # This skips downloading the HTML entirely, making the bot blazing fast.
31
+ if resp.status in [301, 302, 303, 307]:
32
+ location = resp.headers.get("Location", "")
33
+ if "/?query=" in location:
34
+ return "UNAVAILABLE"
35
+ continue # If it's a weird unknown redirect, retry
36
+
37
+ # If the page actually loads (Status 200)
38
  if resp.status == 200:
39
  html = await resp.text()
40
 
41
+ # Extract the exact status from Fragment's HTML header
42
+ match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', html, re.IGNORECASE)
 
 
 
 
 
 
 
 
 
43
 
44
  if match:
45
  status_text = match.group(1).strip().lower()
46
 
47
+ if status_text == "sold": return "SOLD"
48
+ if status_text == "taken": return "TAKEN"
49
+ if status_text == "on auction": return "ON_AUCTION"
50
+ if status_text == "available": return "AVAILABLE"
51
 
52
+ # NEW FEATURE: If it's an unknown status, write to other.txt
53
  clean_status = status_text.upper()
54
  with open("other.txt", "a", encoding="utf-8") as f:
55
  f.write(f"{word} - {clean_status}\n")
56
  return clean_status
57
 
58
+ # Fallback if the header is completely missing
59
+ if "tm-status-unavail" 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"