Spaces:
Sleeping
Sleeping
Update popular.py
Browse files- popular.py +52 -24
popular.py
CHANGED
|
@@ -17,51 +17,81 @@ def normalize_deal_title(raw_title: str) -> str:
|
|
| 17 |
strips away forum wrappers, merchant brackets, shipping buzzwords, and loose pricing strings
|
| 18 |
to isolate the clean, professional product catalog name.
|
| 19 |
"""
|
| 20 |
-
# 1. Convert raw HTML entities like ' and " into clean readable characters
|
| 21 |
t = html.unescape(raw_title.strip())
|
| 22 |
|
| 23 |
-
#
|
| 24 |
t = re.sub(r'\[[^\]]+\]', '', t)
|
| 25 |
t = re.sub(r'\([^)]+\b(amazon|walmart|bestbuy|target|ebay|nike|adidas|ssense|free\s*ship)\b[^)]*\)', '', t, flags=re.IGNORECASE)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
t = re.sub(r'\([^\)]*\$[^\)]*\)', '', t)
|
| 29 |
t = re.sub(r'\([^\)]*\d+%\s*off[^\)]*\)', '', t)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
t = re.sub(r'\b(?:was|reg|regular|list)?\s*\$\d+(?:\.\d{2})?\b', '', t, flags=re.IGNORECASE)
|
| 33 |
t = re.sub(r'\b\d+%\s*off\b', '', t, flags=re.IGNORECASE)
|
| 34 |
t = re.sub(r'\+?\s*(?:free\s*shipping|free\s*ship|fs|w/\s*code)\b', '', t, flags=re.IGNORECASE)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
t = re.sub(r'[\s\-:|–]+$', '', t)
|
| 38 |
t = re.sub(r'^[\s\-:|–]+', '', t)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
t = ' '.join(t.split()).strip()
|
| 42 |
|
| 43 |
return t if len(t) > 8 else raw_title
|
| 44 |
|
| 45 |
def determine_product_type(title_str: str) -> str:
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
"""
|
| 50 |
t = title_str.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if any(k in t for k in ["shoe", "sneaker", "boot", "samba", "pulse", "jordan", "adidas", "nike", "puma", "crocs", "footwear", "slides", "low-top", "cole haan"]):
|
| 52 |
return "Footwear"
|
|
|
|
| 53 |
if any(k in t for k in ["shirt", "hoodie", "jeans", "fleece", "pant", "jacket", "coat", "shorts", "tee", "sweatpants", "suit", "apparel", "clothing", "socks", "windrunner", "polo"]):
|
| 54 |
return "Clothes"
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
return "Fragrances"
|
|
|
|
| 57 |
if any(k in t for k in ["tv", "oled", "switch", "headphones", "buds", "audio", "console", "laptop", "pc", "desktop", "asus", "sony", "apple", "airpods", "deck", "nintendo", "drive", "ssd", "xbox", "ps5", "monitor", "tech", "charger", "anker"]):
|
| 58 |
return "Electronics"
|
|
|
|
| 59 |
return "General"
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def get_trending_deals():
|
| 62 |
"""
|
| 63 |
-
Queries premium
|
| 64 |
-
|
| 65 |
"""
|
| 66 |
global _DEALS_CACHE, _LAST_FETCH_TIME
|
| 67 |
current_time = time.time()
|
|
@@ -69,13 +99,13 @@ def get_trending_deals():
|
|
| 69 |
if _DEALS_CACHE and (current_time - _LAST_FETCH_TIME < CACHE_EXPIRATION_SECONDS):
|
| 70 |
return _DEALS_CACHE
|
| 71 |
|
| 72 |
-
TARGET_FEED_URL = "https://www.techbargains.com/rss.xml"
|
| 73 |
live_stream_deals = []
|
| 74 |
|
| 75 |
try:
|
| 76 |
with httpx.Client(timeout=8.0, trust_env=False) as client:
|
| 77 |
headers = {
|
| 78 |
-
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, Gecko) Chrome/125.0.0.0 Safari/537.36"
|
| 79 |
}
|
| 80 |
response = client.get(TARGET_FEED_URL, headers=headers)
|
| 81 |
|
|
@@ -102,7 +132,6 @@ def get_trending_deals():
|
|
| 102 |
except:
|
| 103 |
source_domain = "retailer.link"
|
| 104 |
|
| 105 |
-
# Compute numeric current absolute item price out of text blocks
|
| 106 |
price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_title)
|
| 107 |
if not price_match and raw_desc:
|
| 108 |
price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_desc)
|
|
@@ -111,16 +140,13 @@ def get_trending_deals():
|
|
| 111 |
if parsed_price == 0.0:
|
| 112 |
continue
|
| 113 |
|
| 114 |
-
# Clean up titles and resolve text encoding bugs instantly
|
| 115 |
clean_title = normalize_deal_title(raw_title)
|
| 116 |
|
| 117 |
-
# Initialize target variables for the extraction sequence
|
| 118 |
original_price = 0.0
|
| 119 |
badge_text = ""
|
| 120 |
extracted_percentage = None
|
| 121 |
|
| 122 |
-
# -
|
| 123 |
-
# Tier 1: Look for an explicit percentage off indicator in title or body
|
| 124 |
pct_match = re.search(r'(\d+)\s*%\s*off', raw_title, re.IGNORECASE)
|
| 125 |
if not pct_match and raw_desc:
|
| 126 |
pct_match = re.search(r'(\d+)\s*%\s*off', raw_desc, re.IGNORECASE)
|
|
@@ -131,7 +157,6 @@ def get_trending_deals():
|
|
| 131 |
original_price = round(parsed_price / (1 - extracted_percentage / 100.0), 2)
|
| 132 |
badge_text = f"{extracted_percentage}% OFF"
|
| 133 |
|
| 134 |
-
# Tier 2: Look for an explicit regular reference price comparison variable
|
| 135 |
if not extracted_percentage:
|
| 136 |
orig_match = re.search(r'(?:reg|regular|was|list|orig|original)\b\.?\s*:?\s*\$\s*(\d+(?:\.\d{2})?)', raw_title, re.IGNORECASE)
|
| 137 |
if not orig_match and raw_desc:
|
|
@@ -143,7 +168,6 @@ def get_trending_deals():
|
|
| 143 |
extracted_percentage = int(round((1 - parsed_price / original_price) * 100))
|
| 144 |
badge_text = f"{extracted_percentage}% OFF"
|
| 145 |
|
| 146 |
-
# Tier 3: Look for dollar-off baseline value savings tracking indicators
|
| 147 |
if not extracted_percentage:
|
| 148 |
save_match = re.search(r'(?:save|\bsave\s*up\s*to\s*)\s*\$\s*(\d+(?:\.\d{2})?)', raw_title, re.IGNORECASE)
|
| 149 |
if not save_match and raw_desc:
|
|
@@ -155,13 +179,10 @@ def get_trending_deals():
|
|
| 155 |
extracted_percentage = int(round((save_amt / original_price) * 100))
|
| 156 |
badge_text = f"{extracted_percentage}% OFF"
|
| 157 |
|
| 158 |
-
# Tier 4: Procedural Variance Engine (Prevents static fallbacks like 26%)
|
| 159 |
if not extracted_percentage:
|
| 160 |
-
# Mathematically generates dynamic variation between 15% and 42% based on title characteristics
|
| 161 |
dynamic_percentage = 15 + (len(clean_title) % 28)
|
| 162 |
original_price = round(parsed_price / (1 - dynamic_percentage / 100.0), 2)
|
| 163 |
badge_text = f"{dynamic_percentage}% OFF"
|
| 164 |
-
# --- END MULTI-TIER DISCOUNT EXTRACTION MATRIX ---
|
| 165 |
|
| 166 |
image_url = "https://placehold.co/100x100/ffffff/000000?text=RETAIL"
|
| 167 |
if raw_desc:
|
|
@@ -171,15 +192,22 @@ def get_trending_deals():
|
|
| 171 |
if image_url.startswith("//"):
|
| 172 |
image_url = "https:" + image_url
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
live_stream_deals.append({
|
| 175 |
"title": clean_title,
|
| 176 |
"price": parsed_price,
|
| 177 |
"original_price": original_price,
|
| 178 |
"source_domain": source_domain,
|
| 179 |
-
"size":
|
| 180 |
"image": image_url,
|
| 181 |
"url": raw_url,
|
| 182 |
"product_type": determine_product_type(clean_title),
|
|
|
|
| 183 |
"badge": badge_text,
|
| 184 |
"rating": 4.8,
|
| 185 |
"reviews": 85 + (idx * 4),
|
|
|
|
| 17 |
strips away forum wrappers, merchant brackets, shipping buzzwords, and loose pricing strings
|
| 18 |
to isolate the clean, professional product catalog name.
|
| 19 |
"""
|
|
|
|
| 20 |
t = html.unescape(raw_title.strip())
|
| 21 |
|
| 22 |
+
# Strip out bracketed store headers (e.g. [Amazon], [Best Buy], [Dell])
|
| 23 |
t = re.sub(r'\[[^\]]+\]', '', t)
|
| 24 |
t = re.sub(r'\([^)]+\b(amazon|walmart|bestbuy|target|ebay|nike|adidas|ssense|free\s*ship)\b[^)]*\)', '', t, flags=re.IGNORECASE)
|
| 25 |
|
| 26 |
+
# Extract loose currency fragments or discount percentages inside parentheticals
|
| 27 |
t = re.sub(r'\([^\)]*\$[^\)]*\)', '', t)
|
| 28 |
t = re.sub(r'\([^\)]*\d+%\s*off[^\)]*\)', '', t)
|
| 29 |
|
| 30 |
+
# Clean up loose suffix promotional strings
|
| 31 |
t = re.sub(r'\b(?:was|reg|regular|list)?\s*\$\d+(?:\.\d{2})?\b', '', t, flags=re.IGNORECASE)
|
| 32 |
t = re.sub(r'\b\d+%\s*off\b', '', t, flags=re.IGNORECASE)
|
| 33 |
t = re.sub(r'\+?\s*(?:free\s*shipping|free\s*ship|fs|w/\s*code)\b', '', t, flags=re.IGNORECASE)
|
| 34 |
|
| 35 |
+
# Strip trailing punctuation, loose dashes, or hanging colons
|
| 36 |
t = re.sub(r'[\s\-:|–]+$', '', t)
|
| 37 |
t = re.sub(r'^[\s\-:|–]+', '', t)
|
| 38 |
|
| 39 |
+
# Collapse duplicate whitespaces cleanly
|
| 40 |
t = ' '.join(t.split()).strip()
|
| 41 |
|
| 42 |
return t if len(t) > 8 else raw_title
|
| 43 |
|
| 44 |
def determine_product_type(title_str: str) -> str:
|
| 45 |
"""
|
| 46 |
+
Strict-Inclusion Taxonomy Engine. Uses highly selective keyword parameters
|
| 47 |
+
and exclusion lists to prevent general household products from polluting
|
| 48 |
+
specialized luxury or apparel view blocks.
|
| 49 |
"""
|
| 50 |
t = title_str.lower()
|
| 51 |
+
|
| 52 |
+
# Aggressive Exclusion Matrix to drop household utilities or automotive products
|
| 53 |
+
BLACKLIST = [
|
| 54 |
+
"humidifier", "diffuser", "hose", "car", "wash", "refill", "refills",
|
| 55 |
+
"cleaning", "plug-in", "candle", "wax", "griot", "garage", "dish", "soap",
|
| 56 |
+
"cleaner", "purifier", "essential oil", "filter", "detergent", "deodorizer"
|
| 57 |
+
]
|
| 58 |
+
if any(b in t for b in BLACKLIST):
|
| 59 |
+
return "General"
|
| 60 |
+
|
| 61 |
if any(k in t for k in ["shoe", "sneaker", "boot", "samba", "pulse", "jordan", "adidas", "nike", "puma", "crocs", "footwear", "slides", "low-top", "cole haan"]):
|
| 62 |
return "Footwear"
|
| 63 |
+
|
| 64 |
if any(k in t for k in ["shirt", "hoodie", "jeans", "fleece", "pant", "jacket", "coat", "shorts", "tee", "sweatpants", "suit", "apparel", "clothing", "socks", "windrunner", "polo"]):
|
| 65 |
return "Clothes"
|
| 66 |
+
|
| 67 |
+
# Strict Premium Scent Profile Check Rules
|
| 68 |
+
if any(k in t for k in ["perfume", "cologne", "edp", "edt", "fragrance", "eau de", "parfum", "toilette", "lattafa", "armaf", "creed", "ysl", "sauvage", "aventus", "fakhar", "colognes", "perfumes"]):
|
| 69 |
return "Fragrances"
|
| 70 |
+
|
| 71 |
if any(k in t for k in ["tv", "oled", "switch", "headphones", "buds", "audio", "console", "laptop", "pc", "desktop", "asus", "sony", "apple", "airpods", "deck", "nintendo", "drive", "ssd", "xbox", "ps5", "monitor", "tech", "charger", "anker"]):
|
| 72 |
return "Electronics"
|
| 73 |
+
|
| 74 |
return "General"
|
| 75 |
|
| 76 |
+
def determine_gender(title_str: str) -> str:
|
| 77 |
+
"""
|
| 78 |
+
Identifies explicit gender indexing context out of raw retail titles
|
| 79 |
+
to drive multi-layered backend filter streams.
|
| 80 |
+
```
|
| 81 |
+
"""
|
| 82 |
+
t = title_str.lower()
|
| 83 |
+
if any(k in t for k in ["men's", "mens", "man", "pour homme", "homme", "for men"]):
|
| 84 |
+
return "mens"
|
| 85 |
+
if any(k in t for k in ["women's", "womens", "woman", "pour femme", "femme", "for women", "her"]):
|
| 86 |
+
return "womens"
|
| 87 |
+
if any(k in t for k in ["kid's", "kids", "boy", "girl", "toddler", "child"]):
|
| 88 |
+
return "kids"
|
| 89 |
+
return "unisex"
|
| 90 |
+
|
| 91 |
def get_trending_deals():
|
| 92 |
"""
|
| 93 |
+
Queries premium commercial deal feeds, processes item metadata using
|
| 94 |
+
namespace-agnostic parsing, and computes multi-tiered discount indicators.
|
| 95 |
"""
|
| 96 |
global _DEALS_CACHE, _LAST_FETCH_TIME
|
| 97 |
current_time = time.time()
|
|
|
|
| 99 |
if _DEALS_CACHE and (current_time - _LAST_FETCH_TIME < CACHE_EXPIRATION_SECONDS):
|
| 100 |
return _DEALS_CACHE
|
| 101 |
|
| 102 |
+
TARGET_FEED_URL = "[https://www.techbargains.com/rss.xml](https://www.techbargains.com/rss.xml)"
|
| 103 |
live_stream_deals = []
|
| 104 |
|
| 105 |
try:
|
| 106 |
with httpx.Client(timeout=8.0, trust_env=False) as client:
|
| 107 |
headers = {
|
| 108 |
+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
|
| 109 |
}
|
| 110 |
response = client.get(TARGET_FEED_URL, headers=headers)
|
| 111 |
|
|
|
|
| 132 |
except:
|
| 133 |
source_domain = "retailer.link"
|
| 134 |
|
|
|
|
| 135 |
price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_title)
|
| 136 |
if not price_match and raw_desc:
|
| 137 |
price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_desc)
|
|
|
|
| 140 |
if parsed_price == 0.0:
|
| 141 |
continue
|
| 142 |
|
|
|
|
| 143 |
clean_title = normalize_deal_title(raw_title)
|
| 144 |
|
|
|
|
| 145 |
original_price = 0.0
|
| 146 |
badge_text = ""
|
| 147 |
extracted_percentage = None
|
| 148 |
|
| 149 |
+
# Multi-Tier Discount Extraction Machine
|
|
|
|
| 150 |
pct_match = re.search(r'(\d+)\s*%\s*off', raw_title, re.IGNORECASE)
|
| 151 |
if not pct_match and raw_desc:
|
| 152 |
pct_match = re.search(r'(\d+)\s*%\s*off', raw_desc, re.IGNORECASE)
|
|
|
|
| 157 |
original_price = round(parsed_price / (1 - extracted_percentage / 100.0), 2)
|
| 158 |
badge_text = f"{extracted_percentage}% OFF"
|
| 159 |
|
|
|
|
| 160 |
if not extracted_percentage:
|
| 161 |
orig_match = re.search(r'(?:reg|regular|was|list|orig|original)\b\.?\s*:?\s*\$\s*(\d+(?:\.\d{2})?)', raw_title, re.IGNORECASE)
|
| 162 |
if not orig_match and raw_desc:
|
|
|
|
| 168 |
extracted_percentage = int(round((1 - parsed_price / original_price) * 100))
|
| 169 |
badge_text = f"{extracted_percentage}% OFF"
|
| 170 |
|
|
|
|
| 171 |
if not extracted_percentage:
|
| 172 |
save_match = re.search(r'(?:save|\bsave\s*up\s*to\s*)\s*\$\s*(\d+(?:\.\d{2})?)', raw_title, re.IGNORECASE)
|
| 173 |
if not save_match and raw_desc:
|
|
|
|
| 179 |
extracted_percentage = int(round((save_amt / original_price) * 100))
|
| 180 |
badge_text = f"{extracted_percentage}% OFF"
|
| 181 |
|
|
|
|
| 182 |
if not extracted_percentage:
|
|
|
|
| 183 |
dynamic_percentage = 15 + (len(clean_title) % 28)
|
| 184 |
original_price = round(parsed_price / (1 - dynamic_percentage / 100.0), 2)
|
| 185 |
badge_text = f"{dynamic_percentage}% OFF"
|
|
|
|
| 186 |
|
| 187 |
image_url = "https://placehold.co/100x100/ffffff/000000?text=RETAIL"
|
| 188 |
if raw_desc:
|
|
|
|
| 192 |
if image_url.startswith("//"):
|
| 193 |
image_url = "https:" + image_url
|
| 194 |
|
| 195 |
+
# Extract product variant values cleanly
|
| 196 |
+
size_val = "Live Item"
|
| 197 |
+
size_match = re.search(r'(\d+(?:\.\d+)?\s*(?:oz|ml|pack|pk|ct|fl\.\s*oz|lbs|inch|”|"))', clean_title, re.IGNORECASE)
|
| 198 |
+
if size_match:
|
| 199 |
+
size_val = size_match.group(1)
|
| 200 |
+
|
| 201 |
live_stream_deals.append({
|
| 202 |
"title": clean_title,
|
| 203 |
"price": parsed_price,
|
| 204 |
"original_price": original_price,
|
| 205 |
"source_domain": source_domain,
|
| 206 |
+
"size": size_val,
|
| 207 |
"image": image_url,
|
| 208 |
"url": raw_url,
|
| 209 |
"product_type": determine_product_type(clean_title),
|
| 210 |
+
"gender": determine_gender(clean_title),
|
| 211 |
"badge": badge_text,
|
| 212 |
"rating": 4.8,
|
| 213 |
"reviews": 85 + (idx * 4),
|