-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.py
More file actions
220 lines (186 loc) · 5.88 KB
/
Copy pathdatabase.py
File metadata and controls
220 lines (186 loc) · 5.88 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import sqlite3
from pathlib import Path
import settings
class Database:
def __init__(self, database_path: Path | None = None):
if database_path is None:
database_path = settings.DATABASE_PATH
self.database_path = Path(database_path)
self.database_path.parent.mkdir(parents=True, exist_ok=True)
self.connection = sqlite3.connect(self.database_path)
self.connection.row_factory = sqlite3.Row
self.create_tables()
def create_tables(self) -> None:
self.connection.execute(
"""
CREATE TABLE IF NOT EXISTS articles
(
id
INTEGER
PRIMARY
KEY
AUTOINCREMENT,
university
TEXT
NOT
NULL,
title
TEXT
NOT
NULL,
summary
TEXT,
link
TEXT
NOT
NULL
UNIQUE,
published
TEXT,
fetched_at
TEXT
NOT
NULL,
image_url
TEXT
);
"""
)
self.connection.commit()
try:
self.connection.execute("ALTER TABLE articles ADD COLUMN image_url TEXT;")
self.connection.commit()
except sqlite3.OperationalError:
pass
try:
self.connection.execute("ALTER TABLE articles ADD COLUMN source TEXT;")
self.connection.commit()
except sqlite3.OperationalError:
pass
def save_article(self, article: dict) -> None:
query = """
INSERT \
OR IGNORE INTO articles (
university,
source,
title,
summary,
link,
published,
fetched_at,
image_url
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?); \
"""
self.connection.execute(
query,
(
article.get("university", "Unknown university"),
article.get("source", article.get("university", "Unknown source")),
article.get("title", "Untitled"),
article.get("summary", ""),
article.get("link", ""),
article.get("published", "Unknown date"),
article.get("fetched_at", ""),
article.get("image_url", ""),
),
)
self.connection.commit()
def save_articles(self, articles: list[dict]) -> None:
for article in articles:
self.save_article(article)
def get_articles(
self,
university=None,
source=None,
search_text=None,
limit: int = 500,
) -> list[dict]:
query = """
SELECT
id,
university,
source,
title,
summary,
link,
published,
fetched_at,
image_url
FROM articles
WHERE 1 = 1 \
"""
params = []
if university and university != "All":
query += " AND university = ?"
params.append(university)
if source and source != "All":
query += " AND source = ?"
params.append(source)
if university and university != "All":
query += " AND university = ?"
params.append(university)
if search_text:
query += """
AND (
title LIKE ?
OR summary LIKE ?
OR university LIKE ?
)
"""
search_pattern = f"%{search_text}%"
params.extend([search_pattern, search_pattern, search_pattern])
query += """
ORDER BY id DESC
LIMIT ?
"""
params.append(limit)
cursor = self.connection.execute(query, params)
rows = cursor.fetchall()
return [dict(row) for row in rows]
def delete_all_articles(self) -> None:
self.connection.execute("DELETE FROM articles;")
self.connection.commit()
def close(self) -> None:
self.connection.close()
def enforce_article_limit(self, max_articles: int) -> None:
query = """
DELETE \
FROM articles
WHERE id NOT IN (SELECT id \
FROM articles \
ORDER BY id DESC
LIMIT ?
); \
"""
self.connection.execute(query, (max_articles,))
self.connection.commit()
def load_cached_articles(self):
try:
self.articles = self.database.get_articles()
except Exception as error:
print(f"Could not load cached articles: {error}")
self.articles = []
self.selected_university = "All"
self.search_text = ""
self.render_articles()
def get_universities(self) -> list[str]:
query = """
SELECT DISTINCT university
FROM articles
ORDER BY university ASC;
"""
cursor = self.connection.execute(query)
rows = cursor.fetchall()
return [row["university"] for row in rows]
def get_sources_for_university(self, university: str) -> list[str]:
cursor = self.connection.execute(
"""
SELECT DISTINCT source
FROM articles
WHERE university = ?
ORDER BY source ASC;
""",
(university,),
)
return [row["source"] for row in cursor.fetchall() if row["source"]]