-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
121 lines (103 loc) · 4.27 KB
/
Copy pathdatabase.py
File metadata and controls
121 lines (103 loc) · 4.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
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
import sqlite3
connection = sqlite3.connect("knotifier.db", check_same_thread=False)
cursor = connection.cursor()
class knotifier:
def initiate() -> None:
"""Creates all tables if they do not exist"""
cursor.execute(
"""CREATE TABLE IF NOT EXISTS monitored (
uid INTEGER,
seriesId INTEGER,
lastChapter INTEGER,
friendlyName TEXT
)"""
)
cursor.execute(
"""CREATE TABLE IF NOT EXISTS emails (
uid INTEGER,
email TEXT
)"""
)
cursor.execute(
"""CREATE TABLE IF NOT EXISTS telegram_channels (
uid INTEGER,
channel_id int
)"""
)
connection.commit()
class db:
def track(uid, seriesId, lastChapter, friendlyName):
"""Inserts a series into the database"""
cursor.execute(
"""INSERT INTO monitored VALUES (?, ?, ?, ?)""",
(uid, seriesId, lastChapter, friendlyName),
)
connection.commit()
return True
def get_email(uid):
"""Retrieves the email associated with a UID"""
cursor.execute(
"SELECT email FROM emails WHERE uid=?", (uid,)
)
result = cursor.fetchone()
if result is not None:
return result[0] # Extract the email from the result tuple
else:
return None # Return None if no email is found
def save_email(uid, email):
"""Saves the email and UID into the database"""
# Check if the UID already exists in the table
cursor.execute("SELECT uid FROM emails WHERE uid=?", (uid,))
result = cursor.fetchone()
if result is not None:
# Update the email if the UID already exists
cursor.execute("UPDATE emails SET email=? WHERE uid=?", (email, uid))
else:
# Insert a new row if the UID doesn't exist
cursor.execute(
"INSERT INTO emails (uid, email) VALUES (?, ?)", (uid, email)
)
# Commit the changes and close the connection
connection.commit()
return True
def get_tracked_series(uid):
"""Retrieves series IDs and their friendly names tracked by a user from the database"""
cursor.execute(
"SELECT seriesId, friendlyName FROM monitored WHERE uid=?", (uid,)
)
results = cursor.fetchall()
if results:
tracked_series = [(result[0], result[1]) for result in results]
return tracked_series
else:
return []
def save_telegram_chat_id(uid, channel_id):
"""Saves the Telegram channel ID with the Discord user ID"""
# Check if the UID already exists in the table
cursor.execute(
"SELECT uid FROM telegram_channels WHERE uid=?", (uid,)
)
result = cursor.fetchone()
if result is not None:
# Update the channel ID if the UID already exists
cursor.execute(
"UPDATE telegram_channels SET channel_id=? WHERE uid=?", (channel_id, uid)
)
else:
# Insert a new row if the UID doesn't exist
cursor.execute(
"INSERT INTO telegram_channels (uid, channel_id) VALUES (?, ?)", (uid, channel_id)
)
# Commit the changes and close the connection
connection.commit()
return True
def get_telegram_chat_id(uid):
"""Retrieves the Telegram channel ID associated with a Discord user ID"""
cursor.execute(
"SELECT channel_id FROM telegram_channels WHERE uid=?", (uid,)
)
result = cursor.fetchone()
if result is not None:
return result[0] # Extract the channel ID from the result tuple
else:
return None # Return None if no channel ID is found