-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
103 lines (93 loc) · 2.91 KB
/
Copy pathdatabase.py
File metadata and controls
103 lines (93 loc) · 2.91 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
import sqlite3, os
DB_PATH = os.path.join(os.path.dirname(__file__), "entries.db")
week2idx = {
"Monday": 0,
"Tuesday": 1,
"Wednesday": 2,
"Thursday": 3,
"Friday": 4,
"Saturday": 5,
"Sunday": 6,
}
idx2week = {idx: day for day, idx in week2idx.items()}
def init_db():
with sqlite3.connect(DB_PATH) as conn:
conn.execute("""
CREATE TABLE IF NOT EXISTS entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL,
category TEXT NOT NULL,
author_id TEXT NOT NULL,
link TEXT,
seen INTEGER DEFAULT 0
)
""")
def add_entry(content, category, author_id, link=None):
with sqlite3.connect(DB_PATH) as conn:
conn.execute(
"INSERT INTO entries (content, category, author_id, link) VALUES (?, ?, ?, ?)",
(content, category, author_id, link)
)
def get_random():
with sqlite3.connect(DB_PATH) as conn:
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute("SELECT COUNT(*) FROM entries WHERE seen = 0")
(unseen_count,) = cur.fetchone()
if unseen_count == 0:
cur.execute("UPDATE entries SET seen = 0")
conn.commit()
cur.execute("""
SELECT * FROM entries
WHERE seen = 0
ORDER BY RANDOM()
LIMIT 1
""")
row = cur.fetchone()
if not row:
return None
cur.execute(
"UPDATE entries SET seen = 1 WHERE id = ?",
(row["id"],)
)
conn.commit()
return dict(row)
def get_by_link(content, return_row=False):
if not content:
return None
with sqlite3.connect(DB_PATH) as conn:
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute(
"SELECT * FROM entries WHERE link = ? COLLATE NOCASE LIMIT 1",
(content,)
)
row = cur.fetchone()
if return_row:
return dict(row) if row else None
else:
return row is not None
def get_by_entry(content, return_row=False):
with sqlite3.connect(DB_PATH) as conn:
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute(
"SELECT * FROM entries WHERE content = ? COLLATE NOCASE LIMIT 1",
(content,)
)
row = cur.fetchone()
if return_row:
return dict(row) if row else None
else:
return row is not None
def delete_entry(content):
with sqlite3.connect(DB_PATH) as conn:
cur = conn.cursor()
cur.execute("DELETE FROM entries WHERE content = ? COLLATE NOCASE",
(content,))
conn.commit()
def delete_db():
with sqlite3.connect(DB_PATH) as conn:
cur = conn.cursor()
cur.execute("DELETE FROM entries")
conn.commit()