Spaces:
Paused
Paused
Update scraper.py
Browse files- scraper.py +19 -13
scraper.py
CHANGED
|
@@ -19,6 +19,7 @@ async def check_fragment(word: str) -> str:
|
|
| 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)
|
|
@@ -27,35 +28,40 @@ async def check_fragment(word: str) -> str:
|
|
| 27 |
if resp.status == 200:
|
| 28 |
html = await resp.text()
|
| 29 |
|
| 30 |
-
# 1.
|
| 31 |
if "/?query=" in str(resp.url):
|
| 32 |
return "UNAVAILABLE"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 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
|
| 41 |
-
if
|
| 42 |
-
if
|
| 43 |
-
if
|
| 44 |
|
| 45 |
-
#
|
| 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 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
return "
|
| 55 |
|
| 56 |
except Exception:
|
| 57 |
pass
|
| 58 |
|
| 59 |
await asyncio.sleep(random.uniform(0.8, 2.0))
|
| 60 |
|
| 61 |
-
return "ERROR"
|
|
|
|
| 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)
|
|
|
|
| 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"
|