-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
123 lines (112 loc) · 3.84 KB
/
db.py
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
import sqlite3
class Database:
def __init__(self, db_name="rss_subs.db"):
self.conn = sqlite3.connect(db_name)
self.init_db()
def init_db(self):
c = self.conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
url TEXT,
channel_id TEXT,
interval INTEGER DEFAULT 10
)
""")
c.execute("""
CREATE TABLE IF NOT EXISTS sent_messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
url TEXT,
channel_id TEXT,
message_id TEXT
)
""")
self.conn.commit()
def subscribe(self, user_id, url, channel_id, interval):
c = self.conn.cursor()
try:
c.execute(
"SELECT * FROM subscriptions WHERE user_id = ? AND url = ? AND channel_id = ?",
(user_id, url, channel_id),
)
if c.fetchone():
return False
else:
c.execute(
"INSERT INTO subscriptions (user_id, url, channel_id, interval) VALUES (?, ?, ?, ?)",
(user_id, url, channel_id, interval),
)
self.conn.commit()
return True
finally:
c.close()
def unsubscribe(self, user_id, url, channel_id):
c = self.conn.cursor()
try:
c.execute(
"DELETE FROM subscriptions WHERE user_id = ? AND url = ? AND channel_id = ?",
(user_id, url, channel_id),
)
self.conn.commit()
finally:
c.close()
def update_interval(self, user_id, url, channel_id, interval):
c = self.conn.cursor()
try:
c.execute(
"UPDATE subscriptions SET interval = ? WHERE user_id = ? AND url = ? AND channel_id = ?",
(interval, user_id, url, channel_id),
)
self.conn.commit()
finally:
c.close()
def get_subscriptions(self, user_id):
c = self.conn.cursor()
try:
c.execute(
"SELECT url, channel_id, interval FROM subscriptions WHERE user_id = ?",
(user_id,),
)
rows = c.fetchall()
return rows
finally:
c.close()
def get_all_subscriptions(self):
c = self.conn.cursor()
c.execute("SELECT user_id, url, channel_id, interval FROM subscriptions")
subscriptions = [
{
"user_id": row[0],
"rss_link": row[1],
"channel_id": row[2],
"interval": row[3],
}
for row in c.fetchall()
]
c.close()
return subscriptions
def save_sent_message(self, user_id, url, channel_id, message_id):
c = self.conn.cursor()
try:
c.execute(
"INSERT INTO sent_messages (user_id, url, channel_id, message_id) VALUES (?, ?, ?, ?)",
(user_id, url, channel_id, message_id),
)
self.conn.commit()
finally:
c.close()
def is_message_sent(self, user_id, url, channel_id, message_id):
c = self.conn.cursor()
try:
c.execute(
"SELECT id FROM sent_messages WHERE user_id = ? AND url = ? AND channel_id = ? AND message_id = ?",
(user_id, url, channel_id, message_id),
)
row = c.fetchone()
return row is not None
finally:
c.close()
def close(self):
self.conn.close()