-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
68 lines (47 loc) · 1.13 KB
/
init_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
DB Setup
"""
import datetime
import json
import os
import sqlite3
DB_NAME = "quotes.db"
SEEDS_DIR = "seeds"
create_sql: str = """
create table if not exists quotes (
id integer primary key,
text text unique,
source text,
topic text,
created timestamp
)
"""
insert_sql: str = """
insert into quotes (text, source, topic, created) values (?, ?, ?, ?)
"""
def init_db():
conn = sqlite3.connect(DB_NAME)
cur = conn.cursor()
cur.execute(create_sql)
conn.commit()
print("Created DB")
return conn
def load_seeds(conn):
js = []
now = datetime.datetime.now()
for file_name in os.listdir(SEEDS_DIR):
if not file_name.endswith(".json"):
continue
with open(os.path.join(SEEDS_DIR, file_name)) as f:
js += json.loads(f.read())
topic, _ = file_name.split(".")
cur = conn.cursor()
for quote in js:
print(topic, quote)
cur.execute(insert_sql, (quote["text"], quote["source"], topic, now))
conn.commit()
if __name__ == "__main__":
conn = init_db()
print("Loading ...")
load_seeds(conn)
print("Done")