meeat/new_meet

51 lines
1.2 KiB
Python
Executable File

#!/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, w, meet) values(%s, %s, %s, %s)",
(date["date"], date.get("display"), date.get("w", 1), meet_id,)
)
for time in meet["times"]:
csr.execute(
"insert into time(time, display, w, meet) values(%s, %s, %s, %s)",
(time["time"], time.get("display"), time.get("w", 1), meet_id,)
)
for place in meet["places"]:
csr.execute(
"insert into place(name, w, meet) values(%s, %s, %s)",
(place["name"], place.get("w", 1), meet_id,)
)
db.commit()