-
Notifications
You must be signed in to change notification settings - Fork 36
/
database.py
399 lines (358 loc) · 15.4 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import pymysql
import uuid
import datetime
import os
import base64
from werkzeug.security import generate_password_hash, check_password_hash
class Database:
def __init__(self): # WORKS
self.db = pymysql.connect(host="localhost", user="root", passwd="*********", db="video")
self.cur = self.db.cursor()
def get_most_viewed(self): # WORKS
"""
- Returns a list of top 10 video IDs in the descending order of view count from the VIDEOS table.
"""
self.cur.execute("SELECT video_ID FROM videos ORDER BY CAST(view_count as decimal) DESC LIMIT 10")
most_viewed_video_IDs = []
for ID in self.cur.fetchall():
most_viewed_video_IDs.append(ID[0])
return most_viewed_video_IDs
def is_valid_user(self, username, password): # WORKS
"""
- Checks if the entered username and password corresponds to a valid user in the USERS and ADMINS table.
"""
done1 = self.cur.execute("SELECT password FROM users WHERE username=\"{}\"".format(username))
done2 = self.cur.execute("SELECT username FROM admins WHERE username=\"{}\"".format(username))
if done1 == 0 and done2 == 0: # If both queries are unsuccessful, username doesn't exist in both tables.
return False
else:
if done1 == 1: # If username exists in USERS table.
self.cur.execute("SELECT password FROM users WHERE username=\"{}\"".format(username))
stored_password = self.cur.fetchone()[0]
return check_password_hash(stored_password, password) # Returns True if the hashes match.
else: # If username exists in ADMINS table.
self.cur.execute("SELECT password FROM admins WHERE username=\"{}\"".format(username))
stored_password = self.cur.fetchone()[0]
return check_password_hash(stored_password, password) # Returns True if the hashes match.
def is_valid_username(self, username): # WORKS
"""
- Checks if the username is already present in the USERS table.
"""
done1 = self.cur.execute("SELECT username FROM users WHERE username=\"{}\"".format(username))
done2 = self.cur.execute("SELECT username FROM admins WHERE username=\"{}\"".format(username))
if done1 == 0 and done2 == 0: # If both queries are unsuccessful, username doesn't exist in both tables.
return False
else:
return True
def is_admin(self, username): #WORKS
"""
- Checks if a user is an admin in the ADMINS table..
"""
done = self.cur.execute("SELECT username FROM admins WHERE username=\"{}\"".format(username))
if done == 0: # If query is unsuccessful, username is not an administrator.
return False
else:
return True
def add_user(self, username, password): #WORKS
"""
- Add the new user credentials to the USERS table.
"""
password_hash = generate_password_hash(password) # Generates a SHA256 hash.
try:
self.cur.execute("INSERT INTO users VALUES(\"{}\", \"{}\")".format(username, password_hash))
self.db.commit()
except:
self.db.rollback()
def update_password(self, username, password): #WORKS
"""
- Updates the password of the user in the USERS table.
"""
password_hash = generate_password_hash(password)
try:
self.cur.execute("UPDATE users SET password = \"{}\" WHERE username = \"{}\"".format(password_hash, username))
self.db.commit()
except:
self.db.rollback()
def delete_user(self, username): #WORKS
"""
- Deletes user credentials from the USERS table.
"""
try:
self.cur.execute("SELECT video_ID FROM videos WHERE uploader = \"{}\"".format(username))
videos_to_delete = []
for row in self.cur.fetchall():
videos_to_delete.append(row[0]) # Get video IDs of all videos uploaded by the user.
for ID in videos_to_delete:
os.remove('static/videos/' + str(ID) + '.mp4') # Deletes the video from the static/videos directory.
os.remove('static/images/' + str(ID) + '.jpg') # Deletes the image from the static/images directory.
self.cur.execute("DELETE FROM users WHERE username = \"{}\"".format(username))
self.db.commit()
except:
self.db.rollback()
def upload_video(self, video_ID, username, title): #WORKS
"""
- Updates VIDEOS table with video ID, uploader username and video title in the VIDEOS table.
"""
try:
view_count = 0
self.cur.execute("INSERT INTO videos VALUES(\"{}\", \"{}\", \"{}\", {}, NULL)".format(video_ID, title, username, view_count))
self.db.commit()
except:
self.db.rollback()
def get_video_title(self, video_ID): #WORKS
"""
- Returns the title of the video with the corresponding video ID from the VIDEOS table.
"""
try:
self.cur.execute("SELECT video_title FROM videos WHERE video_ID = \"{}\"".format(video_ID))
title = self.cur.fetchone()[0]
return title
except:
return "Error getting title"
def update_view_count(self, video_ID): #WORKS
"""
- Updates the view count for the corresponding video ID in the VIDEOS table.
"""
try:
self.cur.execute("UPDATE videos SET view_count = view_count + 1 WHERE video_ID = \"{}\"".format(video_ID)) # Adds 1 to the existing value.
self.db.commit()
except:
self.db.rollback()
def update_watched(self, username, video_ID): #WORKS
"""
- Adds the username and video ID to the WATCHED table.
"""
try:
done = self.cur.execute("SELECT * FROM watched WHERE username = \"{}\" AND video_ID = \"{}\"".format(username, video_ID))
if done == 1: # If the query was successful, one row exists
self.cur.execute("UPDATE watched SET count = count + 1 WHERE username = \"{}\" AND video_ID = \"{}\"".format(username, video_ID))
try:
self.cur.execute("CALL add_to_fav(\"{}\", \"{}\")".format(video_ID, username))
except:
pass
if done == 0: # If the query was unsuccessful, row does not exist.
count = 1
self.cur.execute("INSERT INTO watched VALUES(\"{}\", \"{}\", {})".format(video_ID, username, count))
self.db.commit()
except:
self.db.rollback()
def get_video_uploader(self, video_ID): #WORKS
"""
- Returns the username of the user that uploaded the video with the corresponding video ID.
"""
try:
done = self.cur.execute("SELECT uploader FROM videos WHERE video_ID = \"{}\"".format(video_ID))
uploader = self.cur.fetchone()[0]
return uploader
except:
return "Error getting username"
def get_upload_date(self, video_ID):
"""
- Returns the date when the video was uploaded from VIDEOS table.
"""
self.cur.execute("SELECT upload_date FROM videos WHERE video_ID = \"{}\"".format(video_ID))
return self.cur.fetchone()[0]
def get_random_ID(self): # WORKS
"""
- Returns a random video ID from the VIDEOS table.
"""
self.cur.execute("SELECT video_ID FROM videos ORDER BY RAND() LIMIT 1") # Selects video_ID from 1 random row.
return self.cur.fetchone()[0]
def get_watched(self, username): # WORKS
"""
- Returns a list of video IDs watched by the user from the WATCHED table.
"""
self.cur.execute("SELECT video_ID FROM watched WHERE username = \"{}\"".format(username))
watched_video_IDs = []
for ID in self.cur.fetchall():
watched_video_IDs.append(ID[0])
return watched_video_IDs
def get_views(self, video_ID): # WORKS
"""
- Returns the view count of the video with the corresponding video_ID.
"""
self.cur.execute("SELECT view_count FROM videos WHERE video_ID = \"{}\"".format(video_ID))
return self.cur.fetchone()[0]
def delete_video(self, video_ID): # WORKS
"""
- Deletes the video from the database.
"""
try:
self.cur.execute("DELETE FROM videos WHERE video_ID = \"{}\"".format(video_ID))
self.db.commit()
os.remove('static/videos/' + str(video_ID) + '.mp4')
os.remove('static/images/' + str(video_ID) + '.jpg')
except:
self.db.rollback()
def is_available(self, video_ID): # WORKS
"""
- Checks if the video ID is present in the database.
"""
done = self.cur.execute("SELECT video_ID FROM videos WHERE video_ID = \"{}\"".format(video_ID))
if done == 1:
return True
else:
return False
def get_uploaded(self, username): # WORKS
"""
- Returns a list of all videos uploaded by the user with the corresponding username.
"""
self.cur.execute("SELECT video_ID FROM videos WHERE uploader = \"{}\"".format(username))
uploaded_video_IDs = []
for ID in self.cur.fetchall():
uploaded_video_IDs.append(ID[0])
return uploaded_video_IDs
def is_user_present(self, username): # WORKS
"""
- Returns True if the username is present in the USERS table.
"""
done = self.cur.execute("SELECT username FROM users WHERE username = \"{}\"".format(username))
if done == 1:
return True
else:
return False
def get_five_random_IDs(self):
"""
- Returns a maximum of 5 random video IDS from the VIDEOS table.
"""
self.cur.execute("SELECT video_ID FROM videos ORDER BY RAND() LIMIT 5")
IDs = []
for ID in self.cur.fetchall():
IDs.append(ID[0])
return IDs
def flag_ID(self, username, video_ID):
"""
- Adds the video ID to the FLAGS table.
"""
done = self.cur.execute("SELECT video_ID from flags WHERE video_ID = \"{}\"".format(video_ID))
if done == 0: # Not yet flagged by any user.
try:
self.cur.execute("INSERT INTO flags VALUES(\"{}\", \"{}\")".format(video_ID, username))
self.db.commit()
except:
self.db.rollback()
def add_admin(self, username, password):
"""
- Adds the new administrator to the ADMINS table.
"""
password_hash = generate_password_hash(password) # Generates a SHA256 hash.
try:
self.cur.execute("INSERT INTO admins VALUES(\"{}\", \"{}\")".format(username, password_hash))
self.db.commit()
except:
self.db.rollback()
def get_flagger(self, video_ID):
"""
- Returns username of flagger of the video ID.
"""
self.cur.execute("SELECT username FROM flags WHERE video_ID = \"{}\"".format(video_ID))
return self.cur.fetchone()[0]
def get_flagged(self):
"""
- Returns a list of flagged videos from FLAGS table.
"""
self.cur.execute("SELECT video_ID FROM flags")
flagged_IDs = []
for ID in self.cur.fetchall():
flagged_IDs.append(ID[0])
return flagged_IDs
def user_list(self):
"""
- Returns a list of users in the USERS table.
"""
self.cur.execute("SELECT username FROM users")
users = []
for username in self.cur.fetchall():
users.append(username[0])
return users
def get_video_num(self, username):
"""
- Returns the number of videos uploaded by the username.
"""
done = self.cur.execute("SELECT video_ID FROM videos where uploader = \"{}\"".format(username))
return done
def get_flagged_num(self, username):
"""
- Returns number of videos of user flagged by other users.
"""
done = self.cur.execute("SELECT flags.video_ID FROM videos,flags WHERE videos.video_ID = flags.video_ID AND videos.uploader = \"{}\"".format(username))
return done
def get_user_count(self):
"""
- Returns number of users in the USERS table.
"""
done = self.cur.execute("SELECT username FROM users")
return done
def get_video_count(self):
"""
- Returns number of videos in the VIDEOS table.
"""
done = self.cur.execute("SELECT video_ID FROM videos")
return done
def get_total_view_count(self):
"""
- Returns number of views on all videos in the VIDEOS table.
"""
done = self.cur.execute("SELECT CAST(SUM(view_count) AS DECIMAL(10, 0)) FROM videos")
count = self.cur.fetchone()[0]
return count
def get_flag_count(self):
"""
- Returns number of flagged videos in the VIDEOS table.
"""
done = self.cur.execute("SELECT video_ID FROM flags")
return done
def get_user_video_count(self, username):
"""
- Returns number of videos uploaded by the user from VIDEOS table.
"""
done = self.cur.execute("SELECT video_ID FROM videos WHERE uploader = \"{}\"".format(username))
return done
def get_user_view_count(self, username):
"""
- Returns number of views on all videos uploaded by the user from VIDEOS table.
"""
self.cur.execute("SELECT CAST(SUM(view_count) AS DECIMAL(10, 0)) FROM videos WHERE uploader = \"{}\"".format(username))
return self.cur.fetchone()[0]
def get_best_video_ID(self, username):
"""
- Returns the video ID of the video uploaded by the user with most views.
"""
self.cur.execute("SELECT video_ID FROM videos WHERE uploader = \"{}\" ORDER BY view_count DESC".format(username))
return self.cur.fetchone()[0]
def get_fav_video_ID(self, username):
"""
- Returns the video ID of the user's favourite video.
"""
self.cur.execute("SELECT video_ID FROM watched WHERE username = \"{}\" ORDER BY CAST(count as decimal) DESC".format(username))
return self.cur.fetchone()[0]
def get_favourites(self, username):
"""
- Returns list of videos favourited by the user from FAVOURITES table.
"""
self.cur.execute("SELECT video_ID FROM favourites WHERE username = \"{}\"".format(username))
favourites = []
for ID in self.cur.fetchall():
favourites.append(ID[0])
return favourites
def delete_flag(self, video_ID):
"""
- Deletes the video from FLAGS table.
"""
try:
self.cur.execute("DELETE FROM flags WHERE video_ID = \"{}\"".format(video_ID))
self.db.commit()
except:
self.db.rollback()
def video_dict(self):
"""
- Returns a dictionary of video IDs, title and a list of video titles.
- For Fuzzy Search.
"""
self.cur.execute("SELECT video_ID, video_title FROM videos")
videos = {}
video_titles = []
for video in self.cur.fetchall():
video_titles.append(video[1])
videos.update({video[0] : video[1]})
return videos, video_titles