Browser session recovery, price tracking, and dashboard improvements
- Add browser session validation and auto-restart for BestBuy/GameStop scrapers - Add price change detection and notifications - Remove PokemonCenter scraper (now uses Chrome Extension) - Dashboard UI improvements Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
+128
-36
@@ -27,15 +27,55 @@ class BestBuyScraper(BaseScraper):
|
||||
|
||||
def __init__(self):
|
||||
self._stealth_browser = None
|
||||
self._restart_attempts = 0
|
||||
self._max_restart_attempts = 3
|
||||
|
||||
def _get_stealth_browser(self):
|
||||
"""Get or create stealth browser for Best Buy"""
|
||||
"""Get or create stealth browser for Best Buy, with auto-restart on session errors"""
|
||||
from tools.stealth_browser import StealthBrowser
|
||||
|
||||
# Check if we need to create a new browser
|
||||
if self._stealth_browser is None:
|
||||
from tools.stealth_browser import StealthBrowser
|
||||
logger.info("Creating new Best Buy stealth browser...")
|
||||
self._stealth_browser = StealthBrowser(headless=False, session_name="bestbuy")
|
||||
self._stealth_browser.start()
|
||||
self._restart_attempts = 0
|
||||
return self._stealth_browser
|
||||
|
||||
# Check if existing session is still valid
|
||||
if not self._stealth_browser.is_session_valid():
|
||||
if self._restart_attempts >= self._max_restart_attempts:
|
||||
logger.error(f"Best Buy browser failed after {self._max_restart_attempts} restart attempts")
|
||||
# Reset counter and try one more time after clearing
|
||||
self._restart_attempts = 0
|
||||
self._stealth_browser = None
|
||||
return self._get_stealth_browser()
|
||||
|
||||
logger.warning("Best Buy browser session invalid, restarting...")
|
||||
self._restart_attempts += 1
|
||||
self._stealth_browser.restart()
|
||||
|
||||
return self._stealth_browser
|
||||
|
||||
def _handle_session_error(self, error: Exception) -> bool:
|
||||
"""
|
||||
Check if error is a session error and handle it.
|
||||
Returns True if browser was restarted and operation should be retried.
|
||||
"""
|
||||
error_msg = str(error).lower()
|
||||
session_errors = ["invalid session id", "session deleted", "no such session", "browser has closed"]
|
||||
|
||||
if any(err in error_msg for err in session_errors):
|
||||
logger.warning(f"Session error detected: {error}")
|
||||
if self._stealth_browser and self._restart_attempts < self._max_restart_attempts:
|
||||
self._restart_attempts += 1
|
||||
try:
|
||||
self._stealth_browser.restart()
|
||||
return True # Retry operation
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to restart browser: {e}")
|
||||
return False
|
||||
|
||||
def scrape_category_page(self, url: str, max_pages: int = 1) -> List[Product]:
|
||||
"""
|
||||
Scrape a Best Buy search/category page for all products
|
||||
@@ -65,39 +105,62 @@ class BestBuyScraper(BaseScraper):
|
||||
for page_num in range(1, max_pages + 1):
|
||||
products = []
|
||||
page_url = url if page_num == 1 else f"{url}&cp={page_num}"
|
||||
retry_count = 0
|
||||
max_retries = 2
|
||||
html = None
|
||||
|
||||
while retry_count <= max_retries:
|
||||
try:
|
||||
logger.info(f"Navigating to: {page_url}")
|
||||
browser.driver.get(page_url)
|
||||
time.sleep(8) # Wait longer for initial page load
|
||||
|
||||
# Scroll for lazy loading - wait longer between scrolls
|
||||
browser.driver.execute_script("window.scrollTo(0, 500)")
|
||||
time.sleep(2)
|
||||
browser.driver.execute_script("window.scrollTo(0, 1500)")
|
||||
time.sleep(2)
|
||||
browser.driver.execute_script("window.scrollTo(0, 3000)")
|
||||
time.sleep(2)
|
||||
browser.driver.execute_script("window.scrollTo(0, 0)") # Scroll back to top
|
||||
time.sleep(3)
|
||||
|
||||
# Wait for products to load (check for content)
|
||||
for _ in range(10):
|
||||
html = browser.driver.page_source
|
||||
if "sku-item" in html or "sku-title" in html or "priceView" in html:
|
||||
logger.info("Product content detected")
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
# Get HTML after waiting
|
||||
html = browser.driver.page_source
|
||||
|
||||
# Save debug screenshot
|
||||
try:
|
||||
browser.driver.save_screenshot("debug_bestbuy.png")
|
||||
logger.info("Saved debug screenshot to debug_bestbuy.png")
|
||||
except:
|
||||
pass
|
||||
|
||||
break # Success, exit retry loop
|
||||
|
||||
except Exception as e:
|
||||
if self._handle_session_error(e) and retry_count < max_retries:
|
||||
retry_count += 1
|
||||
logger.info(f"Retrying after browser restart (attempt {retry_count}/{max_retries})")
|
||||
browser = self._get_stealth_browser() # Get restarted browser
|
||||
continue
|
||||
else:
|
||||
logger.error(f"Error navigating to {page_url}: {e}")
|
||||
return all_products
|
||||
else:
|
||||
# while loop completed without break (all retries exhausted)
|
||||
if html is None:
|
||||
logger.error(f"Failed to load page after {max_retries} retries")
|
||||
return all_products
|
||||
|
||||
try:
|
||||
logger.info(f"Navigating to: {page_url}")
|
||||
browser.driver.get(page_url)
|
||||
time.sleep(8) # Wait longer for initial page load
|
||||
|
||||
# Scroll for lazy loading - wait longer between scrolls
|
||||
browser.driver.execute_script("window.scrollTo(0, 500)")
|
||||
time.sleep(2)
|
||||
browser.driver.execute_script("window.scrollTo(0, 1500)")
|
||||
time.sleep(2)
|
||||
browser.driver.execute_script("window.scrollTo(0, 3000)")
|
||||
time.sleep(2)
|
||||
browser.driver.execute_script("window.scrollTo(0, 0)") # Scroll back to top
|
||||
time.sleep(3)
|
||||
|
||||
# Wait for products to load (check for content)
|
||||
for _ in range(10):
|
||||
html = browser.driver.page_source
|
||||
if "sku-item" in html or "sku-title" in html or "priceView" in html:
|
||||
logger.info("Product content detected")
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
# Get HTML after waiting
|
||||
html = browser.driver.page_source
|
||||
|
||||
# Save debug screenshot
|
||||
try:
|
||||
browser.driver.save_screenshot("debug_bestbuy.png")
|
||||
logger.info("Saved debug screenshot to debug_bestbuy.png")
|
||||
except:
|
||||
pass
|
||||
|
||||
soup = BeautifulSoup(html, "html.parser")
|
||||
|
||||
@@ -272,11 +335,17 @@ class BestBuyScraper(BaseScraper):
|
||||
price_match = re.search(r"\$[\d,]+\.?\d*", price_text)
|
||||
price = price_match.group() if price_match else None
|
||||
|
||||
# Check stock status from element text
|
||||
item_text = item.get_text().lower()
|
||||
in_stock = True
|
||||
if any(phrase in item_text for phrase in ["sold out", "out of stock", "unavailable", "coming soon"]):
|
||||
in_stock = False
|
||||
|
||||
products.append(Product(
|
||||
name=name,
|
||||
url=f"{self.base_url}/site/{sku_id}.p",
|
||||
price=price,
|
||||
in_stock=True,
|
||||
in_stock=in_stock,
|
||||
image_url=None,
|
||||
site=self.site_name,
|
||||
product_id=sku_id,
|
||||
@@ -359,12 +428,28 @@ class BestBuyScraper(BaseScraper):
|
||||
if image_matches:
|
||||
image_url = image_matches[0]
|
||||
|
||||
# Try to determine stock status from Apollo data context
|
||||
in_stock = True # Default to True if no stock info found
|
||||
if url_pos > 0:
|
||||
# Look for availability info near this product's URL
|
||||
context_start = max(0, url_pos - 3000)
|
||||
context_end = min(len(script_content), url_pos + 500)
|
||||
product_context = script_content[context_start:context_end]
|
||||
|
||||
# Check for explicit out of stock indicators
|
||||
if '"isAvailable":false' in product_context or '"available":false' in product_context:
|
||||
in_stock = False
|
||||
elif '"soldOut":true' in product_context or '"outOfStock":true' in product_context:
|
||||
in_stock = False
|
||||
elif 'sold out' in product_context.lower() or 'out of stock' in product_context.lower():
|
||||
in_stock = False
|
||||
|
||||
if name and len(name) > 5:
|
||||
products.append(Product(
|
||||
name=name,
|
||||
url=url,
|
||||
price=price,
|
||||
in_stock=True, # Assume in stock if listed
|
||||
in_stock=in_stock,
|
||||
image_url=image_url,
|
||||
site=self.site_name,
|
||||
product_id=sku_id,
|
||||
@@ -512,7 +597,14 @@ class BestBuyScraper(BaseScraper):
|
||||
if "availability" in data:
|
||||
availability = data["availability"]
|
||||
if isinstance(availability, dict):
|
||||
in_stock = availability.get("isAvailable", True) or availability.get("available", True)
|
||||
# Check isAvailable first, then available - don't use 'or' which short-circuits incorrectly
|
||||
is_available = availability.get("isAvailable")
|
||||
available = availability.get("available")
|
||||
if is_available is not None:
|
||||
in_stock = bool(is_available)
|
||||
elif available is not None:
|
||||
in_stock = bool(available)
|
||||
# else keep default True
|
||||
elif isinstance(availability, bool):
|
||||
in_stock = availability
|
||||
elif isinstance(availability, str):
|
||||
|
||||
Reference in New Issue
Block a user