-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
64 lines (54 loc) · 2.27 KB
/
database.py
File metadata and controls
64 lines (54 loc) · 2.27 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
import include
DATE = include.dt.now().strftime("%Y-%m-%d %H:%M:%S")
def insert_data(connection):
# Get zipped list of tuples from scrape
zipper = include.sc.scrape()
# Get dictionary of sources and their id key
with open("JSONs/sources.JSON") as f:
current_sources = include.json.load(f)
# Create cursor and initiates tables if they dont exist
cursor = connection.cursor()
try:
cursor.execute(
"CREATE TABLE news (id INTEGER PRIMARY KEY AUTOINCREMENT, site TEXT NOT NULL, source_id INT NOT NULL, datetime TEXT NOT NULL);")
print("Table Created: news")
except include.sql.Error as err:
print(err)
try:
cursor.execute(
"CREATE TABLE sources (id INT NOT NULL, source TEXT NOT NULL, FOREIGN KEY(id) REFERENCES news (source_id));")
print("Table Created: sources")
except include.sql.Error as err:
print(err)
new_sources = 0
for item in current_sources:
final_string = (current_sources[item], item)
cursor.execute("SELECT source FROM sources WHERE source = ? OR id = ?;",
(item, current_sources[item]))
check = cursor.fetchall()
if len(check) == 0:
new_sources = new_sources + 1
try:
cursor.execute(
"INSERT INTO sources (id, source) VALUES (?, ?);", final_string)
except include.sql.Error as err:
print(err)
cursor.execute("SELECT source, id FROM sources;")
get_all_sources = dict(cursor.fetchall())
new_headlines = 0
for item in zipper:
if item[1] in get_all_sources.keys():
final_string = (item[0], get_all_sources[item[1]], DATE)
cursor.execute("SELECT site FROM news WHERE site = ?;", (item[0],))
check = cursor.fetchall()
if len(check) == 0:
new_headlines = new_headlines + 1
try:
cursor.execute(
"INSERT INTO news (site, source_id, datetime) VALUES (?, ?, ?);", final_string)
except include.sql.Error as err:
print(err)
connection.commit()
print(f"{new_sources} sources added")
print(f"{new_headlines} headlines added")
print("Complete")