Cleanups for scrapers
This commit is contained in:
@@ -28,6 +28,7 @@ from config import (
|
||||
from src.browser import get_browser, shutdown_browser
|
||||
from src.product_tracker import ProductTracker
|
||||
from src.discord_notifier import send_stock_alert, send_startup_notification, send_error_notification, send_price_change_alert
|
||||
from src.scraper_state import scraper_state
|
||||
# Note: Pokemon Center uses Chrome Extension (chrome-extension/), not a Python scraper
|
||||
from scrapers import (
|
||||
TargetScraper,
|
||||
@@ -139,6 +140,7 @@ def check_target():
|
||||
# Log successful check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("target", total_products, total_new, total_restocks, duration_ms, success=True)
|
||||
scraper_state.record_check("target", success=True)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking Target: {e}")
|
||||
@@ -146,6 +148,7 @@ def check_target():
|
||||
# Log failed check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("target", total_products, total_new, total_restocks, duration_ms, success=False, error_message=str(e))
|
||||
scraper_state.record_check("target", success=False, error=str(e))
|
||||
|
||||
|
||||
def check_gamestop():
|
||||
@@ -228,6 +231,7 @@ def check_gamestop():
|
||||
# Log successful check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("gamestop", total_products, total_new, total_restocks, duration_ms, success=True)
|
||||
scraper_state.record_check("gamestop", success=True)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking GameStop: {e}")
|
||||
@@ -235,6 +239,7 @@ def check_gamestop():
|
||||
# Log failed check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("gamestop", total_products, total_new, total_restocks, duration_ms, success=False, error_message=str(e))
|
||||
scraper_state.record_check("gamestop", success=False, error=str(e))
|
||||
|
||||
|
||||
def check_bestbuy():
|
||||
@@ -317,6 +322,7 @@ def check_bestbuy():
|
||||
# Log successful check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("bestbuy", total_products, total_new, total_restocks, duration_ms, success=True)
|
||||
scraper_state.record_check("bestbuy", success=True)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking Best Buy: {e}")
|
||||
@@ -324,6 +330,7 @@ def check_bestbuy():
|
||||
# Log failed check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("bestbuy", total_products, total_new, total_restocks, duration_ms, success=False, error_message=str(e))
|
||||
scraper_state.record_check("bestbuy", success=False, error=str(e))
|
||||
|
||||
|
||||
def check_walmart():
|
||||
@@ -406,6 +413,7 @@ def check_walmart():
|
||||
# Log successful check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("walmart", total_products, total_new, total_restocks, duration_ms, success=True)
|
||||
scraper_state.record_check("walmart", success=True)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking Walmart: {e}")
|
||||
@@ -413,6 +421,7 @@ def check_walmart():
|
||||
# Log failed check to database
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
tracker.log_check("walmart", total_products, total_new, total_restocks, duration_ms, success=False, error_message=str(e))
|
||||
scraper_state.record_check("walmart", success=False, error=str(e))
|
||||
|
||||
|
||||
def run_checks():
|
||||
@@ -440,6 +449,11 @@ def graceful_shutdown(signum, frame):
|
||||
"""Handle shutdown gracefully"""
|
||||
logger.info("Shutting down...")
|
||||
shutdown_browser()
|
||||
scraper_state.state["monitor_running"] = False
|
||||
scraper_state.state["monitor_pid"] = None
|
||||
for site in scraper_state.state["scrapers"]:
|
||||
scraper_state.state["scrapers"][site]["running"] = False
|
||||
scraper_state._save_state()
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
@@ -469,6 +483,15 @@ def main():
|
||||
print(" playwright install chromium")
|
||||
return
|
||||
|
||||
# Mark monitor as running in state file
|
||||
import os
|
||||
scraper_state.state["monitor_running"] = True
|
||||
scraper_state.state["monitor_pid"] = os.getpid()
|
||||
for site in SITES_ENABLED:
|
||||
if site in scraper_state.state["scrapers"]:
|
||||
scraper_state.state["scrapers"][site]["running"] = True
|
||||
scraper_state._save_state()
|
||||
|
||||
# Send startup notification
|
||||
send_startup_notification()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user