-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_db.py
More file actions
77 lines (65 loc) · 1.78 KB
/
setup_db.py
File metadata and controls
77 lines (65 loc) · 1.78 KB
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
69
70
71
72
73
74
75
76
77
import sqlite3
import csv
def setup():
# Create & connect to database
db = sqlite3.connect("emails.db")
# Create tables for email parser to fill
db.execute("""
CREATE TABLE IF NOT EXISTS "articles" (
"id" INTEGER,
"date" TEXT,
"title" TEXT UNIQUE,
"publication" TEXT,
"tier" INTEGER,
"category" TEXT,
PRIMARY KEY("id" AUTOINCREMENT))
""")
db.execute("""
CREATE TABLE IF NOT EXISTS "links" (
"article_id" INTEGER,
"link0" TEXT,
"link1" TEXT,
"link2" TEXT,
"link3" TEXT,
"link4" TEXT,
PRIMARY KEY("article_id"))
""")
db.execute("""
CREATE TABLE IF NOT EXISTS "medialist" (
"url" TEXT UNIQUE,
"tier" INTEGER,
"type" TEXT)
""")
db.execute("""
CREATE TABLE IF NOT EXISTS "platforms" (
"article_id" INTEGER,
"platform0" TEXT,
"platform1" TEXT,
"platform2" TEXT,
"platform3" TEXT,
"platform4" TEXT,
PRIMARY KEY("article_id"))
""")
# Write files
results = db.execute("""SELECT * FROM medialist """)
if not [r for r in results]:
print("Error: 'medialist' table is empty!")
print("Attempting to write medialist.csv to database")
# Open new CSV file for writing
try:
with open("medialist.csv", "r") as medialist:
reader = csv.DictReader(medialist)
for row in reader:
print(row)
db.execute("INSERT INTO medialist (url, tier, type) VALUES(?, ?, ?)",
(row["url"], row["tier"], row["type"]))
except FileNotFoundError:
print("medialist.csv not found")
exit(1)
db.commit()
# Reset databases after each run
db.execute("DELETE FROM articles")
db.execute("DELETE FROM platforms")
db.execute("DELETE FROM links")
return db
setup()