From c146dbb6749040089cd17be49df571e3e2934ad9 Mon Sep 17 00:00:00 2001 From: Marc Nixon Date: Fri, 1 Sep 2023 10:39:33 -0700 Subject: [PATCH] add datetime_handler --- snowscraper/controller.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/snowscraper/controller.py b/snowscraper/controller.py index a77a10a..386a028 100644 --- a/snowscraper/controller.py +++ b/snowscraper/controller.py @@ -6,12 +6,10 @@ from pathlib import Path SCRAPERS = {} - def register_scraper(cls): SCRAPERS[cls.__name__] = cls return cls - def run_all(args: argparse.Namespace): results = {} for scraper_cls in SCRAPERS.values(): @@ -19,12 +17,16 @@ def run_all(args: argparse.Namespace): results.update(scraper.scrape()) print(results) - # Save the results to a JSON file - with open('results.json', 'w') as json_file: - json.dump(results, json_file, indent=4) +def datetime_handler(obj): + if isinstance(obj, datetime.datetime): + return obj.isoformat() + raise TypeError("Unknown type") +# Save the results to a JSON file +with open('results.json', 'w') as json_file: + json.dump(results, json_file, indent=4, default=datetime_handler) def import_scrapers(): directory = Path(__file__).resolve().parent / "scrapers" for module_loader, name, ispkg in pkgutil.iter_modules([directory]): - importlib.import_module(f".scrapers.{name}", __package__) \ No newline at end of file + importlib.import_module(f".scrapers.{name}", __package__)