recipe-graph/src/recipe_graph/insert_sites.py

45 lines
1.0 KiB
Python
Raw Normal View History

2022-10-15 12:32:58 -04:00
from pydoc import apropos
2022-07-18 11:13:53 -04:00
from sqlalchemy.orm import sessionmaker
2022-10-15 12:32:58 -04:00
from recipe_graph import db
2022-07-18 11:13:53 -04:00
import json
import argparse
import logging
2022-10-15 12:32:58 -04:00
def load_file(f_name: str):
with open(f_name) as f:
sites = json.load(f)
return sites
2022-07-18 11:13:53 -04:00
2022-10-15 12:35:37 -04:00
2022-10-15 12:32:58 -04:00
def setup_argparser() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Import recipes into database")
parser.add_argument("file", type=str, help="JSON file with recipe site information")
parser.add_argument("-v", "--verbose", action="store_true")
2022-07-18 11:13:53 -04:00
2022-10-15 12:32:58 -04:00
args = parser.parse_args()
return args
2022-07-18 11:13:53 -04:00
2022-10-15 12:35:37 -04:00
2022-10-15 12:32:58 -04:00
def setup_logging(args: argparse.Namespace):
if args.verbose:
logging.basicConfig(level=logging.INFO)
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
2022-07-18 11:13:53 -04:00
2022-10-15 12:35:37 -04:00
2022-10-15 12:32:58 -04:00
def main():
args = setup_argparser()
setup_logging(args)
S = db.get_session()
sites = load_file(args.file)
with S.begin() as session:
for site in sites:
logging.info(f"Adding {site}")
session.add(db.RecipeSite(**site))
2022-10-15 12:35:37 -04:00
2022-10-15 12:32:58 -04:00
if __name__ == "__main__":
2022-10-15 12:35:37 -04:00
main()