Muttered3 commited on
Commit
873798b
·
verified ·
1 Parent(s): bf04591

Update scraper.py

Browse files
Files changed (1) hide show
  1. scraper.py +23 -20
scraper.py CHANGED
@@ -1,6 +1,7 @@
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",
@@ -12,13 +13,12 @@ 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)
@@ -26,27 +26,30 @@ async def check_fragment(word: str) -> str:
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
 
 
1
  import asyncio
2
  import random
3
  import aiohttp
4
+ import re
5
 
6
  USER_AGENTS = [
7
  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
 
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
  async with session.get(url, headers=headers, allow_redirects=True, timeout=15) as resp:
21
+
22
  if resp.status == 429:
23
  wait = (2 ** attempt * 3) + random.uniform(1, 3)
24
  await asyncio.sleep(wait)
 
26
 
27
  if resp.status == 200:
28
  html = await resp.text()
 
29
 
30
+ # 1. Handle the "Unavailable" redirect you found
31
+ if "/?query=" in str(resp.url):
32
+ return "UNAVAILABLE"
33
 
34
+ # 2. Extract the exact status from Fragment's HTML header
35
+ match = re.search(r'<span class="tm-section-header-status[^>]*>(.*?)</span>', html, re.IGNORECASE)
 
 
 
 
 
 
36
 
37
+ if match:
38
+ status_text = match.group(1).strip().lower()
39
+
40
+ if status_text == "sold": return "SOLD"
41
+ if status_text == "taken": return "TAKEN"
42
+ if status_text == "on auction": return "ON_AUCTION"
43
+ if status_text == "available": return "AVAILABLE"
44
+
45
+ # 3. NEW FEATURE: If it's a completely unknown status, write to other.txt
46
+ clean_status = status_text.upper()
47
+ with open("other.txt", "a", encoding="utf-8") as f:
48
+ f.write(f"{word} - {clean_status}\n")
49
+ return clean_status
50
+
51
+ # Fallback if the header is completely missing
52
+ if "tm-status-unavail" in html: return "UNAVAILABLE"
53
 
54
  return "UNAVAILABLE"
55