Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -122,7 +122,7 @@ async def check_url_safe(url: str, client: httpx.AsyncClient) -> bool:
|
|
| 122 |
print("⚠️ WARNING: SAFE_BROWSING_API key missing. Bypassing live link validation safety verification.")
|
| 123 |
return True
|
| 124 |
|
| 125 |
-
api_url = f"
|
| 126 |
payload = {
|
| 127 |
"client": {"clientId": "tarz-ai-search-agent", "clientVersion": "1.1.0"},
|
| 128 |
"threatInfo": {
|
|
@@ -205,7 +205,7 @@ def clean_google_link(url: str) -> str:
|
|
| 205 |
"""Extracts raw outbound seller targets from Google tracking wrappers safely"""
|
| 206 |
try:
|
| 207 |
if not url.startswith(("http://", "https://")):
|
| 208 |
-
if url.startswith("/"): url = "
|
| 209 |
else: url = "https://" + url
|
| 210 |
|
| 211 |
parsed = urlparse(url)
|
|
@@ -264,7 +264,6 @@ def call_free_llm(prompt, max_tokens=600):
|
|
| 264 |
)
|
| 265 |
content = output.choices[0].message.content.strip()
|
| 266 |
|
| 267 |
-
# Fixed: Broken up with multiplication to prevent cutting off container builds
|
| 268 |
triple_backtick = "`" * 3
|
| 269 |
content = content.replace(f"{triple_backtick}json", "").replace(triple_backtick, "").strip()
|
| 270 |
return content
|
|
@@ -313,7 +312,7 @@ async def fetch_serpapi_shopping(product_name: str, desired_size: str, client: h
|
|
| 313 |
"api_key": SERPAPI_KEY
|
| 314 |
}
|
| 315 |
try:
|
| 316 |
-
response = await client.get("
|
| 317 |
if response.status_code == 200:
|
| 318 |
data = response.json()
|
| 319 |
formatted_deals = []
|
|
@@ -358,7 +357,7 @@ async def get_tavily_payload(query: str, desired_size: str, client: httpx.AsyncC
|
|
| 358 |
"max_results": 5
|
| 359 |
}
|
| 360 |
try:
|
| 361 |
-
response = await client.post("
|
| 362 |
if response.status_code == 200:
|
| 363 |
search_data = response.json()
|
| 364 |
tavily_images = search_data.get("images", [])
|
|
@@ -386,7 +385,7 @@ async def fetch_ddg_backup_shopping(product_name: str, desired_size: str, client
|
|
| 386 |
"""Performs structured context harvesting directly from DuckDuckGo organic text clusters with zero tokens spent"""
|
| 387 |
scraped_pages = []
|
| 388 |
search_q = f"buy {product_name} {desired_size} retail price".strip()
|
| 389 |
-
url = f"
|
| 390 |
headers = {"User-Agent": random.choice(USER_AGENTS)}
|
| 391 |
try:
|
| 392 |
print(f"🦆 [DuckDuckGo Fallback] Routing zero-cost backup query pipeline for '{search_q}'")
|
|
@@ -416,14 +415,16 @@ async def fetch_ddg_backup_shopping(product_name: str, desired_size: str, client
|
|
| 416 |
"image": None
|
| 417 |
})
|
| 418 |
except Exception as e:
|
| 419 |
-
print(f"
|
| 420 |
return scraped_pages
|
| 421 |
|
| 422 |
# --- MASTER PRICE SWEEPER SEQUENCE (CASCADING MULTI-ENGINE ROUTER) ---
|
| 423 |
async def scan_prices(product_name, desired_size=""):
|
| 424 |
-
"""Orchestrates structured arrays across all endpoints concurrently
|
| 425 |
limits = httpx.Limits(max_keepalive_connections=5, max_connections=10)
|
| 426 |
-
|
|
|
|
|
|
|
| 427 |
|
| 428 |
deals = await fetch_serpapi_shopping(product_name, desired_size, client)
|
| 429 |
|
|
@@ -513,9 +514,9 @@ def generate_ui(results_html="", error_msg=""):
|
|
| 513 |
<title>Tarz Price Scout</title>
|
| 514 |
<meta charset="UTF-8">
|
| 515 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
| 516 |
-
<link rel="preconnect" href="
|
| 517 |
-
<link rel="preconnect" href="
|
| 518 |
-
<link href="
|
| 519 |
<style>
|
| 520 |
:root {{
|
| 521 |
--bg-main: #0a0c14; --card-bg: #131622; --input-bg: #1b1f32; --border-color: rgba(255, 255, 255, 0.08);
|
|
@@ -628,7 +629,7 @@ def generate_ui(results_html="", error_msg=""):
|
|
| 628 |
<div class="input-flex">
|
| 629 |
<div class="input-group">
|
| 630 |
<label>Enter Product Details OR Paste Product Link</label>
|
| 631 |
-
<input type="text" name="product_name" placeholder="e.g., Creed Green Irish Tweed OR
|
| 632 |
</div>
|
| 633 |
<div class="input-group size-group">
|
| 634 |
<label>Size / Variant</label>
|
|
@@ -678,13 +679,14 @@ async def handle_search(product_name: str = Form(...), product_size: str = Form(
|
|
| 678 |
|
| 679 |
deals = []
|
| 680 |
original_site_deal = None
|
| 681 |
-
fallback_img = "
|
| 682 |
|
| 683 |
if target_item.startswith(("http://", "https://")):
|
| 684 |
input_url = target_item
|
| 685 |
print(f"🔗 [Input URL] Pasted hyperlink reference intercepted: {input_url}")
|
| 686 |
|
| 687 |
-
|
|
|
|
| 688 |
if not await check_url_safe(input_url, safety_client):
|
| 689 |
return generate_ui(error_msg="The provided link target matches active security alert threat signatures and was isolated.")
|
| 690 |
|
|
|
|
| 122 |
print("⚠️ WARNING: SAFE_BROWSING_API key missing. Bypassing live link validation safety verification.")
|
| 123 |
return True
|
| 124 |
|
| 125 |
+
api_url = f"https://safebrowsing.googleapis.com/v4/threatMatches:find?key={SAFE_BROWSING_API}"
|
| 126 |
payload = {
|
| 127 |
"client": {"clientId": "tarz-ai-search-agent", "clientVersion": "1.1.0"},
|
| 128 |
"threatInfo": {
|
|
|
|
| 205 |
"""Extracts raw outbound seller targets from Google tracking wrappers safely"""
|
| 206 |
try:
|
| 207 |
if not url.startswith(("http://", "https://")):
|
| 208 |
+
if url.startswith("/"): url = "https://www.google.com" + url
|
| 209 |
else: url = "https://" + url
|
| 210 |
|
| 211 |
parsed = urlparse(url)
|
|
|
|
| 264 |
)
|
| 265 |
content = output.choices[0].message.content.strip()
|
| 266 |
|
|
|
|
| 267 |
triple_backtick = "`" * 3
|
| 268 |
content = content.replace(f"{triple_backtick}json", "").replace(triple_backtick, "").strip()
|
| 269 |
return content
|
|
|
|
| 312 |
"api_key": SERPAPI_KEY
|
| 313 |
}
|
| 314 |
try:
|
| 315 |
+
response = await client.get("https://serpapi.com/search", params=params, timeout=12.0)
|
| 316 |
if response.status_code == 200:
|
| 317 |
data = response.json()
|
| 318 |
formatted_deals = []
|
|
|
|
| 357 |
"max_results": 5
|
| 358 |
}
|
| 359 |
try:
|
| 360 |
+
response = await client.post("https://api.tavily.com/search", json=payload, timeout=10.0)
|
| 361 |
if response.status_code == 200:
|
| 362 |
search_data = response.json()
|
| 363 |
tavily_images = search_data.get("images", [])
|
|
|
|
| 385 |
"""Performs structured context harvesting directly from DuckDuckGo organic text clusters with zero tokens spent"""
|
| 386 |
scraped_pages = []
|
| 387 |
search_q = f"buy {product_name} {desired_size} retail price".strip()
|
| 388 |
+
url = f"https://html.duckduckgo.com/html/?q={requests.utils.quote(search_q)}"
|
| 389 |
headers = {"User-Agent": random.choice(USER_AGENTS)}
|
| 390 |
try:
|
| 391 |
print(f"🦆 [DuckDuckGo Fallback] Routing zero-cost backup query pipeline for '{search_q}'")
|
|
|
|
| 415 |
"image": None
|
| 416 |
})
|
| 417 |
except Exception as e:
|
| 418 |
+
print(f"❌ [DuckDuckGo Engine Failure] Processing bypassed: {e}")
|
| 419 |
return scraped_pages
|
| 420 |
|
| 421 |
# --- MASTER PRICE SWEEPER SEQUENCE (CASCADING MULTI-ENGINE ROUTER) ---
|
| 422 |
async def scan_prices(product_name, desired_size=""):
|
| 423 |
+
"""Orchestrates structured arrays across all endpoints concurrently while bypassing broken environment proxies"""
|
| 424 |
limits = httpx.Limits(max_keepalive_connections=5, max_connections=10)
|
| 425 |
+
|
| 426 |
+
# Critical Fix: trust_env=False strips out un-prefixed platform infrastructure proxy errors completely
|
| 427 |
+
async with httpx.AsyncClient(limits=limits, http2=True, trust_env=False) as client:
|
| 428 |
|
| 429 |
deals = await fetch_serpapi_shopping(product_name, desired_size, client)
|
| 430 |
|
|
|
|
| 514 |
<title>Tarz Price Scout</title>
|
| 515 |
<meta charset="UTF-8">
|
| 516 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
| 517 |
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 518 |
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 519 |
+
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
|
| 520 |
<style>
|
| 521 |
:root {{
|
| 522 |
--bg-main: #0a0c14; --card-bg: #131622; --input-bg: #1b1f32; --border-color: rgba(255, 255, 255, 0.08);
|
|
|
|
| 629 |
<div class="input-flex">
|
| 630 |
<div class="input-group">
|
| 631 |
<label>Enter Product Details OR Paste Product Link</label>
|
| 632 |
+
<input type="text" name="product_name" placeholder="e.g., Creed Green Irish Tweed OR https://www.store.com/item" autocomplete="off" required>
|
| 633 |
</div>
|
| 634 |
<div class="input-group size-group">
|
| 635 |
<label>Size / Variant</label>
|
|
|
|
| 679 |
|
| 680 |
deals = []
|
| 681 |
original_site_deal = None
|
| 682 |
+
fallback_img = "https://placehold.co/100x100/1a1c28/9fa4b4?text=IMG"
|
| 683 |
|
| 684 |
if target_item.startswith(("http://", "https://")):
|
| 685 |
input_url = target_item
|
| 686 |
print(f"🔗 [Input URL] Pasted hyperlink reference intercepted: {input_url}")
|
| 687 |
|
| 688 |
+
# Fixed: Added trust_env=False here to clear potential platform proxy drops
|
| 689 |
+
async with httpx.AsyncClient(trust_env=False) as safety_client:
|
| 690 |
if not await check_url_safe(input_url, safety_client):
|
| 691 |
return generate_ui(error_msg="The provided link target matches active security alert threat signatures and was isolated.")
|
| 692 |
|