#!/usr/bin/python3 import base64 import argparse import json import os import psycopg import psycopg.rows import config db = psycopg.connect(dbname=config.dbname, user=config.dbuser) csr = db.cursor(row_factory=psycopg.rows.namedtuple_row) ap = argparse.ArgumentParser() ap.add_argument("file") args = ap.parse_args() with open(args.file) as fh: meet = json.load(fh) buf = os.urandom(8) key = base64.urlsafe_b64encode(buf).replace(b'=', b'').decode('us-ascii') csr.execute( "insert into meet(title, key) values(%s, %s) returning id", (meet["title"], key,) ) meet_id = csr.fetchone().id for date in meet["dates"]: csr.execute( "insert into date(date, display, meet) values(%s, %s, %s)", (date["date"], date.get("display"), meet_id,) ) for time in meet["times"]: csr.execute( "insert into time(time, display, meet) values(%s, %s, %s)", (time["time"], time.get("display"), meet_id,) ) for place in meet["places"]: csr.execute( "insert into place(name, meet) values(%s, %s)", (place["name"], meet_id,) ) db.commit() csr.execute("select * from meet where id = %s", (meet_id,)) r = csr.fetchone() print(r.id, r.key, r.title)