Spaces:
Paused
Paused
Update scraper.py
Browse files- scraper.py +34 -9
scraper.py
CHANGED
|
@@ -11,10 +11,11 @@ log = get_logger()
|
|
| 11 |
USER_AGENTS = [
|
| 12 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
|
| 13 |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_4_1) AppleWebKit/605.1.15 Version/17.4.1 Safari/605.1.15",
|
| 14 |
-
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/123.0.0.0 Safari/537.36"
|
|
|
|
| 15 |
]
|
| 16 |
|
| 17 |
-
# Thread-safe global
|
| 18 |
_global_api_hash = None
|
| 19 |
_hash_fetched_at = 0.0
|
| 20 |
_hash_lock = asyncio.Lock()
|
|
@@ -35,7 +36,10 @@ async def _get_valid_api_hash(proxy_url: str = None) -> str:
|
|
| 35 |
|
| 36 |
headers = {
|
| 37 |
"User-Agent": random.choice(USER_AGENTS),
|
| 38 |
-
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
|
| 41 |
try:
|
|
@@ -44,21 +48,37 @@ async def _get_valid_api_hash(proxy_url: str = None) -> str:
|
|
| 44 |
async with session.get("https://fragment.com/", headers=headers, proxy=proxy_url, allow_redirects=True) as resp:
|
| 45 |
html = await resp.text()
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
if match:
|
| 49 |
_global_api_hash = match.group(1)
|
| 50 |
_hash_fetched_at = now
|
| 51 |
-
log.info(f"π
|
| 52 |
return _global_api_hash
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
log.error(f"Global Handshake Sync Failure: {str(e)}")
|
| 55 |
|
| 56 |
return _global_api_hash if _global_api_hash else "FAILED_TOKEN"
|
| 57 |
|
| 58 |
async def check_fragment(word: str, proxy_url: str = None) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
global _rate_limited_until
|
| 60 |
word = word.strip().replace("@", "").lower()
|
| 61 |
|
|
|
|
| 62 |
for attempt in range(1, 5):
|
| 63 |
current_time = time.time()
|
| 64 |
if current_time < _rate_limited_until:
|
|
@@ -91,6 +111,7 @@ async def check_fragment(word: str, proxy_url: str = None) -> str:
|
|
| 91 |
if resp.status in [429, 403]:
|
| 92 |
backoff = 4 + (2 ** attempt) + random.uniform(0.5, 1.5)
|
| 93 |
_rate_limited_until = time.time() + backoff
|
|
|
|
| 94 |
continue
|
| 95 |
|
| 96 |
if resp.status != 200:
|
|
@@ -99,7 +120,7 @@ async def check_fragment(word: str, proxy_url: str = None) -> str:
|
|
| 99 |
response_json = await resp.json()
|
| 100 |
|
| 101 |
if not response_json.get("ok"):
|
| 102 |
-
#
|
| 103 |
global _global_api_hash
|
| 104 |
_global_api_hash = None
|
| 105 |
continue
|
|
@@ -108,10 +129,12 @@ async def check_fragment(word: str, proxy_url: str = None) -> str:
|
|
| 108 |
if not html_fragment.strip():
|
| 109 |
return "AVAILABLE"
|
| 110 |
|
| 111 |
-
#
|
| 112 |
clean_html = re.sub(r'\s+', ' ', html_fragment)
|
| 113 |
|
| 114 |
-
#
|
|
|
|
|
|
|
| 115 |
search_regex = re.compile(rf'>@{word}<.*?class="[^"]*status[^"]*"[^>]*>\s*([^<]+?)\s*<', re.IGNORECASE)
|
| 116 |
match = search_regex.search(clean_html)
|
| 117 |
|
|
@@ -124,7 +147,9 @@ async def check_fragment(word: str, proxy_url: str = None) -> str:
|
|
| 124 |
if "sale" in s or "purchase" in s: return "FOR_SALE"
|
| 125 |
return s.upper()
|
| 126 |
|
| 127 |
-
#
|
|
|
|
|
|
|
| 128 |
fallback_text = clean_html.lower()
|
| 129 |
if "on auction" in fallback_text: return "ON_AUCTION"
|
| 130 |
if "sold" in fallback_text: return "SOLD"
|
|
|
|
| 11 |
USER_AGENTS = [
|
| 12 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
|
| 13 |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 14_4_1) AppleWebKit/605.1.15 Version/17.4.1 Safari/605.1.15",
|
| 14 |
+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/123.0.0.0 Safari/537.36",
|
| 15 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0"
|
| 16 |
]
|
| 17 |
|
| 18 |
+
# Thread-safe global synchronization boundaries for token balancing
|
| 19 |
_global_api_hash = None
|
| 20 |
_hash_fetched_at = 0.0
|
| 21 |
_hash_lock = asyncio.Lock()
|
|
|
|
| 36 |
|
| 37 |
headers = {
|
| 38 |
"User-Agent": random.choice(USER_AGENTS),
|
| 39 |
+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
| 40 |
+
"Accept-Language": "en-US,en;q=0.5",
|
| 41 |
+
"Cache-Control": "no-cache",
|
| 42 |
+
"Pragma": "no-cache"
|
| 43 |
}
|
| 44 |
|
| 45 |
try:
|
|
|
|
| 48 |
async with session.get("https://fragment.com/", headers=headers, proxy=proxy_url, allow_redirects=True) as resp:
|
| 49 |
html = await resp.text()
|
| 50 |
|
| 51 |
+
# Target matching layout configurations capturing nested App.init configurations
|
| 52 |
+
match = (
|
| 53 |
+
re.search(r'"hash"\s*:\s*"([a-f0-9]+)"', html, re.IGNORECASE) or
|
| 54 |
+
re.search(r'hash\s*=\s*["\']([a-f0-9]+)["\']', html, re.IGNORECASE) or
|
| 55 |
+
re.search(r'href="/api\?hash=([a-f0-9]+)"', html, re.IGNORECASE)
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
if match:
|
| 59 |
_global_api_hash = match.group(1)
|
| 60 |
_hash_fetched_at = now
|
| 61 |
+
log.info(f"π Successfully Stabilized Global API Hash Token: {_global_api_hash}")
|
| 62 |
return _global_api_hash
|
| 63 |
+
else:
|
| 64 |
+
log.error("β Token extraction fault. DOM format structure layout changed.")
|
| 65 |
+
with open("token_dom_error.log", "w", encoding="utf-8") as f:
|
| 66 |
+
f.write(html[:2000])
|
| 67 |
+
|
| 68 |
except Exception as e:
|
| 69 |
log.error(f"Global Handshake Sync Failure: {str(e)}")
|
| 70 |
|
| 71 |
return _global_api_hash if _global_api_hash else "FAILED_TOKEN"
|
| 72 |
|
| 73 |
async def check_fragment(word: str, proxy_url: str = None) -> str:
|
| 74 |
+
"""
|
| 75 |
+
Connects directly to Fragment's internal AJAX endpoint using XMLHttpRequest signatures,
|
| 76 |
+
bypassing the front-end layout routing and redirection problems.
|
| 77 |
+
"""
|
| 78 |
global _rate_limited_until
|
| 79 |
word = word.strip().replace("@", "").lower()
|
| 80 |
|
| 81 |
+
# 4 Retries matching your advanced optimization matrix
|
| 82 |
for attempt in range(1, 5):
|
| 83 |
current_time = time.time()
|
| 84 |
if current_time < _rate_limited_until:
|
|
|
|
| 111 |
if resp.status in [429, 403]:
|
| 112 |
backoff = 4 + (2 ** attempt) + random.uniform(0.5, 1.5)
|
| 113 |
_rate_limited_until = time.time() + backoff
|
| 114 |
+
log.warning(f"β οΈ Proxy soft-banned on '{word}'. Backing off {backoff:.2f}s...")
|
| 115 |
continue
|
| 116 |
|
| 117 |
if resp.status != 200:
|
|
|
|
| 120 |
response_json = await resp.json()
|
| 121 |
|
| 122 |
if not response_json.get("ok"):
|
| 123 |
+
# If token data gets invalidated by endpoint engine, force reload sequence
|
| 124 |
global _global_api_hash
|
| 125 |
_global_api_hash = None
|
| 126 |
continue
|
|
|
|
| 129 |
if not html_fragment.strip():
|
| 130 |
return "AVAILABLE"
|
| 131 |
|
| 132 |
+
# Clean whitespace structure to allow seamless single-line tracking
|
| 133 |
clean_html = re.sub(r'\s+', ' ', html_fragment)
|
| 134 |
|
| 135 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 136 |
+
# ENGINE 1: STRUCTURAL TABLE LAYER SCANNING
|
| 137 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 138 |
search_regex = re.compile(rf'>@{word}<.*?class="[^"]*status[^"]*"[^>]*>\s*([^<]+?)\s*<', re.IGNORECASE)
|
| 139 |
match = search_regex.search(clean_html)
|
| 140 |
|
|
|
|
| 147 |
if "sale" in s or "purchase" in s: return "FOR_SALE"
|
| 148 |
return s.upper()
|
| 149 |
|
| 150 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 151 |
+
# ENGINE 2: GLOBAL GLOBAL FALLBACK LAYER MATCHING
|
| 152 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 153 |
fallback_text = clean_html.lower()
|
| 154 |
if "on auction" in fallback_text: return "ON_AUCTION"
|
| 155 |
if "sold" in fallback_text: return "SOLD"
|