-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
97 lines (90 loc) · 2.86 KB
/
database.py
File metadata and controls
97 lines (90 loc) · 2.86 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Database module for the project
Author: Jiacheng Zhao (John)
Date: 11/12/22
"""
import sqlite3
class Database:
def __init__(self, db_name: str) -> None:
"""
Constructor for the class,
used to connect to the database.
Due to time constraints,
this would only implement the sqlite way.
"""
self.db_name = db_name
self.connection = sqlite3.connect(db_name)
self.cursor = self.connection.cursor()
def is_database_empty(self) -> bool:
"""
Check if the database do not have any data
:return: True if the database is empty, False otherwise
"""
return len(self.cursor.execute("SELECT COUNT(*) FROM sqlite_master").fetchall()) == 0
def close(self) -> None:
"""
Close the connection to the database
:return: None
"""
self.connection.close()
def add_feed_list(self, feed_data) -> None:
"""
Add the feed list to the database
:param feed_data: list of the feed
:return: None
"""
# Check if the database is empty
if self.is_database_empty():
Database.init_db(self.db_name)
else:
# Add the data
SQL = """
INSERT INTO feed_list (feed_link, title, description, subscribers, last_update, refresh_rate, dump)
VALUES (?, ?, ?, ?, ?, ?, ?)
"""
self.cursor.execute(SQL)
@staticmethod
def init_db(db_name: str) -> None:
"""
Initialize the database
:param db_name: Name of the database
:return: None
"""
connection = sqlite3.connect(db_name)
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE "feed_list" (
"feed_link" TEXT NOT NULL UNIQUE,
"title" TEXT NOT NULL,
"description" TEXT,
"subscribers" JSON NOT NULL,
"last_update" TIMESTAMP NOT NULL,
"refresh_rate" INTEGER NOT NULL,
"dump" JSON NOT NULL,
PRIMARY KEY("feed_link")
);
""")
cursor.execute("""
CREATE TABLE "feed_data" (
"feed_link" TEXT NOT NULL,
"title" TEXT NOT NULL,
"description" TEXT,
"link" TEXT,
"last_update" TIMESTAMP NOT NULL,
"dump" JSON NOT NULL,
FOREIGN KEY("feed_link") REFERENCES "feed_list"("feed_link")
);
""")
cursor.execute("""
CREATE TABLE "user_list" (
"user_id" TEXT NOT NULL UNIQUE,
"use_email" BOOL NOT NULL,
"email" TEXT,
"use_sms" BOOL NOT NULL,
"phone_number" TEXT,
"subscribed_feeds" JSON NOT NULL,
PRIMARY KEY("user_id")
);
""")
connection.commit()
connection.close()