-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_db.py
More file actions
190 lines (177 loc) · 7.96 KB
/
Copy pathsqlite_db.py
File metadata and controls
190 lines (177 loc) · 7.96 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
import sqlite3
from sqlite3 import Error
class SQLITE_DB:
# Function to create the tables
def create_table(self, conn: sqlite3.Connection, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
print("Table created if it didn't already exist")
except Error as e:
print(e)
def get_connection(self):
""" Get the corresponding db connection object
:return sqlite3.Connection: returns connection object
"""
conn = sqlite3.connect("NewsAgg.db")
return conn
# Function to check if guild has been set up in DB
def setup_check(self, conn: sqlite3.Connection, gid: str, table: str):
""" Check for the guild_id in the DB
:param table: table to check in db
:param conn: sqlite3 connection obj
:param gid: command's ctx guild id
:return bool: True = guild has been set up in this DB, False = not set up
"""
cursor = conn.cursor()
cursor.execute("SELECT guild FROM {} WHERE guild = (?)".format(table), [gid])
# SQL statement will return guild_id as provided by command if the DBs are set up. Otherwise, returns None
if cursor.fetchone() is None:
return False
else:
return True
# Function to set up a guild with a specific channel id for a feed (RSS & Twitter separate dbs / channels)
def setup_guild_channel(self, conn: sqlite3.Connection, guild_id: str, channel_id: str, table: str):
""" add a guild_id:channel_id row in db
:param self: the db helper
:param table: table within which to set up this guild
:param conn: Connection object
:param guild_id: Guild ID str
:param channel_id: Channel ID str
:return:
"""
try:
cursor = conn.cursor()
# if setup has not happened, insert new row
if not self.setup_check(self, conn, guild_id, table):
cursor.execute("INSERT INTO {} (guild, channel) VALUES (?,?)".format(table),
[guild_id, channel_id])
conn.commit()
# else update existing
else:
cursor.execute("UPDATE {} SET channel = (?) WHERE guild = (?)".format(table),
[channel_id, guild_id])
conn.commit()
except sqlite3.Error as error:
print("Failed to setup guild's channel. Error: ", error)
def get_posting_channel(self, conn: sqlite3.Connection, guild_id: str, table: str):
""" fetch posting channel from guild
:param table: table within which to grab channel from
:param conn: db connection obj
:param guild_id: Guild ID str
:return str: Error or feed channel id
"""
cursor = conn.cursor()
# if setup has not happened, abort
if not self.setup_check(self, conn, guild_id, table):
return
else:
cursor.execute("SELECT channel FROM {} WHERE guild = (?)".format(table), [guild_id])
feed_channel_id, = cursor.fetchone()
return feed_channel_id
# Function to check if a specified feed source has been added to a specified guild
def feed_check(self, conn: sqlite3.Connection, feed_key: str, gid: str, table: str):
""" Check for a feed value in the Guild's DB
:param table: table within which to check feed_key for
:param conn: sqlite3 connection obj
:param feed_key: command's feed input to check against
:param gid: Guild ID in which to look for search_key
:return bool:
"""
cursor = conn.cursor()
cursor.execute("SELECT source FROM {} WHERE guild = (?)".format(table), [gid])
guild_feeds = cursor.fetchall()
for feed, in guild_feeds:
if feed == feed_key:
return True
return False
# Function to add a feed source to the guild's DB
def add_feed(self, conn: sqlite3.Connection, feed_source: str, guild_id: str, table: str):
""" add a feed source from slash command to the db
:param table: table within which to add the feed_source to
:param conn: sqlite3 connection object
:param feed_source: feed source submitted by user in command "add"
:param guild_id: Guild ID
:return str: one of the 2 statuses
"""
existing_feed = "Feed already added"
successful = "Feed added successfully"
feed_channel_id = self.get_posting_channel(self, conn, guild_id, table)
cursor = conn.cursor()
if self.feed_check(self, conn, feed_source, guild_id, table):
return existing_feed
else:
cursor.execute("INSERT INTO {} (guild, channel, source) VALUES (?,?,?)".format(table),
[guild_id, feed_channel_id, feed_source])
conn.commit()
return successful
# Function to remove an existing RSS Feed url from guild's DB
def remove_feed(self, conn: sqlite3.Connection, feed_source: str, guild_id: str, table: str):
""" remove an existing feed URL from guild specific DB entries
:param table: table within which to remove feed_source
:param conn: sqlite3 connection object
:param feed_source: feed submitted by user in command
:param guild_id: Guild ID
:return str: one of the 2 statuses
"""
unknown_feed = "Feed does not exist"
successful = "Feed removed successfully"
cursor = conn.cursor()
if not self.feed_check(self, conn, feed_source, guild_id, table):
return unknown_feed
else:
cursor.execute("DELETE FROM {} WHERE source = (?) AND guild = (?)".format(table),
[feed_source, guild_id])
conn.commit()
return successful
def list_feeds(self, conn: sqlite3.Connection, table: str):
""" Fetch all guild:feeds in a list from a single table
:param table: table name from which to pull
:param conn: sqlite3 connection object
:return feed_list: returns a list of all the feeds
"""
cursor = conn.cursor()
cursor.execute("SELECT guild, source FROM {}".format(table))
feed_list = cursor.fetchall()
return feed_list
def list_guild_feeds(self, conn: sqlite3.Connection, table: str, guild: str):
""" Fetch all guild:feeds in a list from a single table
:param table: table name from which to pull
:param conn: sqlite3 connection object
:param guild: guild id to fetch feeds for
:return feed_list: returns a list of all the feeds
"""
cursor = conn.cursor()
cursor.execute("SELECT source FROM {} WHERE guild = {}".format(table, guild))
feed_list = cursor.fetchall()
return feed_list
def add_username_uid(self, conn: sqlite3.Connection, table: str, username: str, uid: str):
""" update the twitter username : uid map as needed
:param table: twitter username db
:param uid: twitter api uid
:param username: twitter account name
:param conn: connection to the main DB
:return:
"""
cursor = conn.cursor()
cursor.execute("INSERT INTO {} VALUES (?,?)".format(table), [username, uid])
conn.commit()
def uid_map_check(self, conn: sqlite3, table: str, uid: str):
"""
:param conn: sqlite2 Connection object
:param table: name of the table to check in
:param uid: uid of the twitter account to check
:return:
"""
cursor = conn.cursor()
cursor.execute("SELECT uid FROM {} WHERE uid = (?)".format(table), [uid])
uids = cursor.fetchall()
for id, in uids:
if id == uid:
return True
return False