-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
351 lines (291 loc) · 9.39 KB
/
database.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import sqlite3
from datetime import datetime
from datetime import timedelta
class User:
def __init__(self, username, discord_id) -> None:
self.username = username
self.discord_id = discord_id
class Anime:
def __init__(self, title, image_url, anilist_id) -> None:
self.title = title
self.image_url = image_url
self.anilist_id = anilist_id
class Character:
def __init__(self, name, image_url, anilist_id) -> None:
self.name = name
self.image_url = image_url
self.anilist_id = anilist_id
class AnimeEntry:
def __init__(self, title, image_url, anilist_id, date, rank) -> None:
self.title = title
self.image_url = image_url
self.anilist_id = anilist_id
self.date = date
self.rank = rank
class CharacterEntry:
def __init__(self, name, image_url, anilist_id, date, rank) -> None:
self.name = name
self.image_url = image_url
self.anilist_id = anilist_id
self.date = date
self.rank = rank
def db_create_tables():
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('''
CREATE TABLE User (
UserId INTEGER PRIMARY KEY,
Username TEXT NOT NULL,
DiscordId INTEGER NOT NULL
)
''')
c.execute('''
CREATE TABLE Anime (
AnimeId INTEGER PRIMARY KEY,
Title TEXT NOT NULL,
ImageUrl TEXT NOT NULL,
AnilistId INTEGER NOT NULL
)
''')
c.execute('''
CREATE TABLE Character (
CharacterId INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
ImageUrl TEXT NOT NULL,
AnilistId INTEGER NOT NULL
)
''')
c.execute('''
CREATE TABLE AnimeEntry (
EntryId INTEGER PRIMARY KEY,
Date TEXT NOT NULL,
Rank INTEGER NOT NULL,
UserId INTEGER NOT NULL,
AnimeId INTEGER NOT NULL,
FOREIGN KEY(UserId) REFERENCES User(UserId),
FOREIGN KEY(AnimeId) REFERENCES Anime(AnimeId)
)
''')
c.execute('''
CREATE TABLE CharacterEntry (
EntryId INTEGER PRIMARY KEY,
Date TEXT NOT NULL,
Rank INTEGER NOT NULL,
UserId INTEGER NOT NULL,
CharacterId INTEGER NOT NULL,
FOREIGN KEY(UserId) REFERENCES User(UserId),
FOREIGN KEY(CharacterId) REFERENCES Character(CharacterId)
)
''')
conn.commit()
conn.close()
return 0
def db_add_user(username, discord_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('INSERT INTO User VALUES (NULL,?,?)', (username, discord_id))
conn.commit()
conn.close()
return 0
def db_add_anime(title, image_url, anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('INSERT INTO Anime VALUES (NULL,?,?,?)', (title, image_url, anilist_id))
conn.commit()
conn.close()
return 0
def db_add_character(name, image_url, anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('INSERT INTO Character VALUES (NULL,?,?,?)', (name, image_url, anilist_id))
conn.commit()
conn.close()
return 0
def db_add_anime_entry(date, rank, discord_id, anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'SELECT AnimeId FROM Anime WHERE AnilistId = "{anilist_id}"')
anime_id = c.fetchone()[0]
c.execute ('PRAGMA foreign_keys = ON')
c.execute('INSERT INTO AnimeEntry VALUES (NULL,?,?,?,?)', (date, rank, user_id, anime_id))
conn.commit()
conn.close()
return 0
def db_add_character_entry(date, rank, discord_id, anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'SELECT CharacterId FROM Character WHERE AnilistId = "{anilist_id}"')
character_id = c.fetchone()[0]
c.execute ('PRAGMA foreign_keys = ON')
c.execute('INSERT INTO CharacterEntry VALUES (NULL,?,?,?,?)', (date, rank, user_id, character_id))
conn.commit()
conn.close()
return 0
# Can fetch user by only supplying either one of the two parameter
def db_get_user(username = False, discord_id = False):
conn = sqlite3.connect('data.db')
c = conn.cursor()
if username and discord_id:
c.execute(f'SELECT Username, DiscordId FROM User WHERE Username = "{username}" AND DiscordId = "{discord_id}"')
else:
if username:
c.execute(f'SELECT Username, DiscordId FROM User WHERE Username = "{username}"')
if discord_id:
c.execute(f'SELECT Username, DiscordId FROM User WHERE DiscordId = "{discord_id}"')
user = c.fetchone()
conn.commit()
conn.close()
if user == None:
return False
else:
return User(user[0], user[1])
def db_get_anime(anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT Title, ImageUrl, AnilistId FROM Anime WHERE AnilistId = "{anilist_id}"')
anime = c.fetchone()
conn.commit()
conn.close()
if anime == None:
return False
else:
return Anime(anime[0], anime[1], anime[2])
def db_get_character(anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT Name, ImageUrl, AnilistId FROM Character WHERE AnilistId = "{anilist_id}"')
character = c.fetchone()
conn.commit()
conn.close()
if character == None:
return False
else:
return Character(character[0], character[1], character[2])
def db_is_user_match_anime(discord_id, anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'SELECT AnimeId FROM Anime WHERE AnilistId = "{anilist_id}"')
anime_id = c.fetchone()[0]
c.execute(f'SELECT * FROM AnimeEntry WHERE UserId = {user_id} And AnimeId = {anime_id}')
anime = c.fetchone()
conn.commit()
conn.close()
if anime == None:
return False
else:
return True
def db_is_user_match_character(discord_id, anilist_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'SELECT CharacterId FROM Character WHERE AnilistId = "{anilist_id}"')
anime_id = c.fetchone()[0]
c.execute(f'SELECT * FROM CharacterEntry WHERE UserId = {user_id} And CharacterId = {anime_id}')
anime = c.fetchone()
conn.commit()
conn.close()
if anime == None:
return False
else:
return True
def db_get_anime_list_by_user(discord_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'''
SELECT
Anime.Title,
Anime.ImageUrl,
Anime.AnilistId,
AnimeEntry.Date,
AnimeEntry.Rank
FROM
AnimeEntry
JOIN Anime
ON AnimeEntry.AnimeId = Anime.AnimeId
WHERE
AnimeEntry.UserId = {user_id}
ORDER BY
AnimeEntry.Rank ASC
''')
entries = c.fetchall()
conn.commit()
conn.close()
if entries == []:
return False
else:
return list(map(lambda entry: AnimeEntry(entry[0], entry[1], entry[2], entry[3], entry[4]), entries))
def db_get_character_list_by_user(discord_id):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'''
SELECT
Character.Name,
Character.ImageUrl,
Character.AnilistId,
CharacterEntry.Date,
CharacterEntry.Rank
FROM
CharacterEntry
JOIN Character
ON CharacterEntry.CharacterId = Character.CharacterId
WHERE
CharacterEntry.UserId = {user_id}
ORDER BY
CharacterEntry.Rank ASC
''')
entries = c.fetchall()
conn.commit()
conn.close()
if entries == []:
return False
else:
return list(map(lambda entry: CharacterEntry(entry[0], entry[1], entry[2], entry[3], entry[4]), entries))
def db_preprocess_anime_rank(discord_id, input_rank):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'SELECT COUNT(EntryId) From AnimeEntry WHERE UserId = {user_id}')
max_rank = c.fetchone()[0]
# Reverse to resolve double operation
for current_rank in range(max_rank, input_rank-1, -1):
c.execute(f'UPDATE AnimeEntry SET Rank = {current_rank + 1} WHERE Rank = {current_rank}')
conn.commit()
conn.close()
return 0
def db_preprocess_character_rank(discord_id, input_rank):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(f'SELECT UserId FROM User WHERE DiscordId = "{discord_id}"')
user_id = c.fetchone()[0]
c.execute(f'SELECT COUNT(EntryId) From CharacterEntry WHERE UserId = {user_id}')
max_rank = c.fetchone()[0]
# Reverse to resolve double operation
for current_rank in range(max_rank, input_rank-1, -1):
c.execute(f'UPDATE CharacterEntry SET Rank = {current_rank + 1} WHERE Rank = {current_rank}')
conn.commit()
conn.close()
return 0
# For test only, will delete
def db_reset():
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('DROP TABLE User')
c.execute('DROP TABLE Anime')
c.execute('DROP TABLE Character')
c.execute('DROP TABLE AnimeEntry')
c.execute('DROP TABLE CharacterEntry')
conn.commit()
conn.close()
return 0