Spaces:
Sleeping
Sleeping
Update shopping.py
Browse files- shopping.py +8 -7
shopping.py
CHANGED
|
@@ -48,22 +48,23 @@ async def fetch_serpapi_shopping(product_name: str, desired_size: str, client: h
|
|
| 48 |
|
| 49 |
price_val = item.get("extracted_price")
|
| 50 |
|
| 51 |
-
# --- START DIRECT
|
| 52 |
-
# 1. Grab direct merchant links first if exposed by SerpApi
|
| 53 |
raw_link = item.get("merchant_link") or item.get("link") or item.get("product_link") or ""
|
| 54 |
|
| 55 |
-
#
|
| 56 |
if "google.com" in raw_link:
|
| 57 |
try:
|
| 58 |
parsed_query = urllib.parse.parse_qs(urllib.parse.urlparse(raw_link).query)
|
| 59 |
-
if "
|
|
|
|
|
|
|
| 60 |
raw_link = parsed_query["url"][0]
|
| 61 |
-
elif "adurl" in parsed_query:
|
| 62 |
raw_link = parsed_query["adurl"][0]
|
| 63 |
except:
|
| 64 |
pass
|
| 65 |
|
| 66 |
-
#
|
| 67 |
if "google.com/search" in raw_link or "google.com/shopping" in raw_link:
|
| 68 |
item_source = item.get("source", "").strip().lower()
|
| 69 |
if item_source and "google" not in item_source:
|
|
@@ -74,7 +75,7 @@ async def fetch_serpapi_shopping(product_name: str, desired_size: str, client: h
|
|
| 74 |
raw_link = f"https://www.{sanitized_src}.com"
|
| 75 |
|
| 76 |
link = clean_google_link(raw_link)
|
| 77 |
-
# --- END DIRECT
|
| 78 |
|
| 79 |
detected_size = desired_size if desired_size else (extract_universal_sizing(title) or "Standard")
|
| 80 |
rating = item.get("rating")
|
|
|
|
| 48 |
|
| 49 |
price_val = item.get("extracted_price")
|
| 50 |
|
| 51 |
+
# --- START DIRECT PRODUCT PAGE EXTRACTION MACHINE ---
|
|
|
|
| 52 |
raw_link = item.get("merchant_link") or item.get("link") or item.get("product_link") or ""
|
| 53 |
|
| 54 |
+
# Unpack tracking params inside shopping results to capture product-specific deep directories
|
| 55 |
if "google.com" in raw_link:
|
| 56 |
try:
|
| 57 |
parsed_query = urllib.parse.parse_qs(urllib.parse.urlparse(raw_link).query)
|
| 58 |
+
if "q" in parsed_query and parsed_query["q"][0].startswith("http"):
|
| 59 |
+
raw_link = parsed_query["q"][0]
|
| 60 |
+
elif "url" in parsed_query and parsed_query["url"][0].startswith("http"):
|
| 61 |
raw_link = parsed_query["url"][0]
|
| 62 |
+
elif "adurl" in parsed_query and parsed_query["adurl"][0].startswith("http"):
|
| 63 |
raw_link = parsed_query["adurl"][0]
|
| 64 |
except:
|
| 65 |
pass
|
| 66 |
|
| 67 |
+
# Check for absolute safety fallbacks if no redirect was resolved
|
| 68 |
if "google.com/search" in raw_link or "google.com/shopping" in raw_link:
|
| 69 |
item_source = item.get("source", "").strip().lower()
|
| 70 |
if item_source and "google" not in item_source:
|
|
|
|
| 75 |
raw_link = f"https://www.{sanitized_src}.com"
|
| 76 |
|
| 77 |
link = clean_google_link(raw_link)
|
| 78 |
+
# --- END DIRECT PRODUCT PAGE EXTRACTION MACHINE ---
|
| 79 |
|
| 80 |
detected_size = desired_size if desired_size else (extract_universal_sizing(title) or "Standard")
|
| 81 |
rating = item.get("rating")
|