76 lines
1.8 KiB
Python
Executable File
76 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import csv
|
|
import mariadb
|
|
import signal
|
|
import sys
|
|
from os import environ
|
|
|
|
# fields in pg_catalog.csv:
|
|
# Text# Type Issued Title Language Authors Subjects LoCC Bookshelves
|
|
|
|
bookdata={}
|
|
|
|
# close out on Ctrl-C
|
|
|
|
def signal_handler(sig, frame):
|
|
conn.close()
|
|
sys.exit(0)
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
# read SCV
|
|
|
|
with open(environ.get("LIBRARY_PATH")+"/pg_catalog.csv") as f:
|
|
rdr=csv.DictReader(f)
|
|
for row in rdr:
|
|
bookdata[int(row["Text#"])]=row
|
|
|
|
# connect to database
|
|
|
|
try:
|
|
conn=mariadb.connect(user=environ.get("DB_USER"), password=environ.get("DB_PASS"), host=environ.get("DB_HOST"), database=environ.get("DB_DATA"))
|
|
except mariadb.Error as e:
|
|
print(f"database connection error: {e}")
|
|
sys.exit(1)
|
|
cur=conn.cursor()
|
|
|
|
# cycle through the rows
|
|
|
|
for row in bookdata:
|
|
print(row)
|
|
|
|
try: # skip if we already have it
|
|
cur.execute("insert into books select ?, ?, ?", (row, bookdata[row]["Title"], bookdata[row]["LoCC"]))
|
|
|
|
for subj in bookdata[row]["Subjects"].split("; "):
|
|
try:
|
|
cur.execute("insert into subjects select ?, ?", (row, subj))
|
|
except mariadb.Error as e:
|
|
print(f"database error: {e}")
|
|
|
|
for author in bookdata[row]["Authors"].split("; "):
|
|
try:
|
|
cur.execute("insert into authors select ?, ?", (row, author))
|
|
except mariadb.Error as e:
|
|
print(f"database error: {e}")
|
|
|
|
for lang in bookdata[row]["Language"].split("; "):
|
|
try:
|
|
cur.execute("insert into languages select ?, ?", (row, lang))
|
|
except mariadb.Error as e:
|
|
print(f"database error: {e}")
|
|
|
|
for shelf in bookdata[row]["Bookshelves"].split("; "):
|
|
try:
|
|
cur.execute("insert into shelves select ?, ?", (row, shelf))
|
|
except mariadb.Error as e:
|
|
print(f"database error: {e}")
|
|
|
|
conn.commit()
|
|
|
|
except mariadb.Error as e:
|
|
pass
|
|
|
|
conn.close()
|
|
|