ghstedpixel commited on
Commit
f2d6769
·
verified ·
1 Parent(s): b762726

Update schema.py

Browse files
Files changed (1) hide show
  1. schema.py +36 -12
schema.py CHANGED
@@ -110,19 +110,43 @@ def extract_universal_sizing(text: str) -> str:
110
  return ""
111
 
112
  def clean_google_link(url: str) -> str:
113
- """Extracts raw outbound seller targets from Google tracking wrappers safely"""
 
 
 
 
 
 
 
 
 
114
  try:
115
- if not url.startswith(("http://", "https://")):
116
- if url.startswith("/"): url = "https://www.google.com" + url
117
- else: url = "https://" + url
118
- parsed = urlparse(url)
119
- if "google.com" in parsed.netloc:
120
- qs = parse_qs(parsed.query)
121
- for key in ['url', 'adurl', 'link']:
122
- if key in qs and qs[key]:
123
- extracted = unquote(qs[key][0])
124
- if extracted.startswith(("http://", "https://")): return extracted
125
- except: pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  return url
127
 
128
  def parse_price_locally(dense_text, product_name):
 
110
  return ""
111
 
112
  def clean_google_link(url: str) -> str:
113
+ """
114
+ Strips Google Shopping tracking wrappers and redirects,
115
+ returning the direct outbound merchant website URL instantly.
116
+ """
117
+ if not url:
118
+ return ""
119
+
120
+ if "google.com" not in url:
121
+ return url
122
+
123
  try:
124
+ # Step 1: Extract parameters from query string configurations
125
+ decoded_link = unquote(url)
126
+ parsed_url = urlparse(decoded_link)
127
+ qs = parse_qs(parsed_url.query)
128
+
129
+ # Intercept standard routing redirects directly
130
+ for redirect_param in ['adurl', 'url', 'q', 'link', 'shopping_redirect']:
131
+ if redirect_param in qs:
132
+ target_url = qs[redirect_param][0]
133
+ if target_url.startswith(("http://", "https://")):
134
+ return target_url
135
+
136
+ # Step 2: Heuristic text fallback layer
137
+ found_urls = re.findall(r'(https?://[^\s&]+)', decoded_link)
138
+ for sub_url in found_urls:
139
+ if "google.com" not in sub_url and "googleadservices.com" not in sub_url:
140
+ if "?" in sub_url:
141
+ base_part, query_part = sub_url.split("?", 1)
142
+ # Exclude typical tracking tokens
143
+ query_part = re.sub(re.compile(r'&?(gclid|gclsrc|utm_)[^&]*'), '', query_part)
144
+ sub_url = f"{base_part}?{query_part}".rstrip('?')
145
+ return sub_url
146
+
147
+ except Exception as e:
148
+ print(f"⚠️ [Link Extractor Error] Could not unwrap target metadata: {e}")
149
+
150
  return url
151
 
152
  def parse_price_locally(dense_text, product_name):