ghstedpixel commited on
Commit
d87b3d5
·
verified ·
1 Parent(s): 33a5635

Update popular.py

Browse files
Files changed (1) hide show
  1. popular.py +29 -17
popular.py CHANGED
@@ -1,9 +1,15 @@
1
  # popular.py
2
  import re
 
3
  import httpx
4
  import xml.etree.ElementTree as ET
5
  from urllib.parse import urlparse
6
 
 
 
 
 
 
7
  def determine_product_type(title_str: str) -> str:
8
  """
9
  Scans the live title string to dynamically categorize items
@@ -22,14 +28,20 @@ def determine_product_type(title_str: str) -> str:
22
 
23
  def get_trending_deals():
24
  """
25
- Asynchronously queries real-world retail discount feed vectors via HTTP,
26
- extracts clean pricing structures, and parses live product images on the fly.
27
  """
 
 
 
 
 
 
 
28
  TARGET_FEED_URL = "https://slickdeals.net/newsearch.php?mode=frontpage&searcharea=deals&search_in=first&rss=1"
29
  live_deals = []
30
 
31
  try:
32
- # Standard synchronous client context with a clean 5-second connection boundary
33
  with httpx.Client(timeout=5.0, trust_env=False) as client:
34
  headers = {
35
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
@@ -37,9 +49,8 @@ def get_trending_deals():
37
  response = client.get(TARGET_FEED_URL, headers=headers)
38
 
39
  if response.status_code != 200:
40
- return []
41
-
42
- # Unmarshal operational live XML structure nodes
43
  root = ET.fromstring(response.content)
44
  items = root.findall(".//item")
45
 
@@ -52,7 +63,6 @@ def get_trending_deals():
52
  raw_url = link_node.text if link_node is not None else "https://slickdeals.net"
53
  raw_desc = desc_node.text if desc_node is not None else ""
54
 
55
- # 1. Cleanly parse the outbound retail domain
56
  try:
57
  source_domain = urlparse(raw_url).netloc.replace("www.", "")
58
  if "slickdeals" in source_domain or not source_domain:
@@ -60,23 +70,21 @@ def get_trending_deals():
60
  except:
61
  source_domain = "retailer"
62
 
63
- # 2. Extract the absolute live deal price from the title or descriptive body
64
  price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_title)
65
  if not price_match and raw_desc:
66
  price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_desc)
67
 
68
  parsed_price = float(price_match.group(1)) if price_match else 0.0
69
  if parsed_price == 0.0:
70
- continue # Filter out metadata blocks lacking trackable price figures
71
-
72
- # 3. Live Product Image HTML Extraction Pattern
73
  image_url = "https://placehold.co/100x100/1a1c28/9fa4b4?text=LIVE+DEAL"
74
  if raw_desc:
75
- img_src_match = re.search(r'<img[^>]+src=["']([^"']+)["']', raw_desc, re.IGNORECASE)
76
  if img_src_match:
77
  image_url = img_src_match.group(1)
78
 
79
- # 4. Extract Original Reference Metrics or Calculate Live Markdowns
80
  orig_match = re.search(r'(?:reg|regular|was|list)\.?\s*\$(\d+(?:\.5)?\d*)', raw_title.lower())
81
  if not orig_match and raw_desc:
82
  orig_match = re.search(r'(?:reg|regular|was|list)\.?\s*\$(\d+(?:\.5)?\d*)', raw_desc.lower())
@@ -89,7 +97,6 @@ def get_trending_deals():
89
  original_price = round(parsed_price * 1.30, 2)
90
  badge_text = "PRICE DROP"
91
 
92
- # Direct check for explicit percentage markers within raw text
93
  pct_match = re.search(r'(\d+)\%\s*off', raw_title, re.IGNORECASE)
94
  if pct_match:
95
  badge_text = f"{pct_match.group(1)}% OFF"
@@ -111,8 +118,13 @@ def get_trending_deals():
111
  "is_third_party": True if idx % 3 == 0 else False
112
  })
113
 
114
- return live_deals
 
 
 
 
 
115
 
116
  except Exception as e:
117
- print(f"❌ [Live Popular Engine Failure] Stream broken: {e}")
118
- return []
 
1
  # popular.py
2
  import re
3
+ import time
4
  import httpx
5
  import xml.etree.ElementTree as ET
6
  from urllib.parse import urlparse
7
 
8
+ # Lightweight in-memory state tracking metrics
9
+ _DEALS_CACHE = []
10
+ _LAST_FETCH_TIME = 0.0
11
+ CACHE_EXPIRATION_SECONDS = 600 # Automatically triggers a live network refresh every 10 minutes
12
+
13
  def determine_product_type(title_str: str) -> str:
14
  """
15
  Scans the live title string to dynamically categorize items
 
28
 
29
  def get_trending_deals():
30
  """
31
+ Queries real-world retail discount vectors via HTTP, parses live pricing,
32
+ and handles automatic 10-minute memory throttling natively.
33
  """
34
+ global _DEALS_CACHE, _LAST_FETCH_TIME
35
+ current_time = time.time()
36
+
37
+ # Return hot memory states instantly if the cache window is still open
38
+ if _DEALS_CACHE and (current_time - _LAST_FETCH_TIME < CACHE_EXPIRATION_SECONDS):
39
+ return _DEALS_CACHE
40
+
41
  TARGET_FEED_URL = "https://slickdeals.net/newsearch.php?mode=frontpage&searcharea=deals&search_in=first&rss=1"
42
  live_deals = []
43
 
44
  try:
 
45
  with httpx.Client(timeout=5.0, trust_env=False) as client:
46
  headers = {
47
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
 
49
  response = client.get(TARGET_FEED_URL, headers=headers)
50
 
51
  if response.status_code != 200:
52
+ return _DEALS_CACHE # Return cached state if network drops
53
+
 
54
  root = ET.fromstring(response.content)
55
  items = root.findall(".//item")
56
 
 
63
  raw_url = link_node.text if link_node is not None else "https://slickdeals.net"
64
  raw_desc = desc_node.text if desc_node is not None else ""
65
 
 
66
  try:
67
  source_domain = urlparse(raw_url).netloc.replace("www.", "")
68
  if "slickdeals" in source_domain or not source_domain:
 
70
  except:
71
  source_domain = "retailer"
72
 
 
73
  price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_title)
74
  if not price_match and raw_desc:
75
  price_match = re.search(r'\$(\d+(?:\.\d{2})?)', raw_desc)
76
 
77
  parsed_price = float(price_match.group(1)) if price_match else 0.0
78
  if parsed_price == 0.0:
79
+ continue
80
+
81
+ # Triple double-quotes fix the SyntaxError completely
82
  image_url = "https://placehold.co/100x100/1a1c28/9fa4b4?text=LIVE+DEAL"
83
  if raw_desc:
84
+ img_src_match = re.search(r"""<img[^>]+src=['"]([^'"]+)['"]""", raw_desc, re.IGNORECASE)
85
  if img_src_match:
86
  image_url = img_src_match.group(1)
87
 
 
88
  orig_match = re.search(r'(?:reg|regular|was|list)\.?\s*\$(\d+(?:\.5)?\d*)', raw_title.lower())
89
  if not orig_match and raw_desc:
90
  orig_match = re.search(r'(?:reg|regular|was|list)\.?\s*\$(\d+(?:\.5)?\d*)', raw_desc.lower())
 
97
  original_price = round(parsed_price * 1.30, 2)
98
  badge_text = "PRICE DROP"
99
 
 
100
  pct_match = re.search(r'(\d+)\%\s*off', raw_title, re.IGNORECASE)
101
  if pct_match:
102
  badge_text = f"{pct_match.group(1)}% OFF"
 
118
  "is_third_party": True if idx % 3 == 0 else False
119
  })
120
 
121
+ # Commit new matrix state variables to running application context memory
122
+ if live_deals:
123
+ _DEALS_CACHE = live_deals
124
+ _LAST_FETCH_TIME = current_time
125
+
126
+ return _DEALS_CACHE
127
 
128
  except Exception as e:
129
+ print(f"❌ [Live Popular Engine Failure] Stream handling anomaly: {e}")
130
+ return _DEALS_CACHE