# run.py import argparse import logging # Import necessary functions/modules from your project # The 'import models' is crucial for init_db to know about the tables import models from database import init_db, SessionLocal from fuel_scraper import main as run_scraper_main # Assuming your scraper's main is 'main' # Configure basic logging for the run.py script itself if needed # Your other modules (fuel_scraper, database) will have their own logging # or you might centralize logging configuration further. # For simplicity, we'll let fuel_scraper handle its detailed logging. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) def initialize_database(): """Initializes the database by creating tables based on models.""" logger.info("Attempting to initialize database...") try: init_db() # This function is imported from database.py # It relies on models being imported so Base.metadata is populated logger.info("Database initialization process completed.") except Exception as e: logger.error(f"Error during database initialization: {e}", exc_info=True) def scrape_data(): """Runs the fuel price scraper.""" logger.info("Starting the fuel price scraper...") try: run_scraper_main() # This is the main function from fuel_scraper.py logger.info("Fuel price scraper finished.") except Exception as e: logger.error(f"Error during scraping process: {e}", exc_info=True) def main(): parser = argparse.ArgumentParser(description="Fuel Price Scraper Control Script") parser.add_argument( "action", choices=["initdb", "scrape"], help="The action to perform: 'initdb' to initialize the database, 'scrape' to run the scraper." ) args = parser.parse_args() if args.action == "initdb": initialize_database() elif args.action == "scrape": scrape_data() else: logger.error(f"Unknown action: {args.action}") parser.print_help() if __name__ == "__main__": main()