-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_interface.py
More file actions
407 lines (367 loc) · 11.6 KB
/
db_interface.py
File metadata and controls
407 lines (367 loc) · 11.6 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
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
400
401
402
403
404
405
406
407
# functions to interface with the db indirectly rather than
# doing so directly within the flask app
import sqlite3
#con = sqlite3.connect('the.db', check_same_thread=False, timeout=10)
#cur = con.cursor()
import s3_utils
def db_wrapper(func):
def inner_func(*args, **kwargs):
con = sqlite3.connect('the.db', check_same_thread=False, timeout=10)
cur = con.cursor()
return_value = func(cur, con, *args, **kwargs)
con.close()
return return_value
return inner_func
def head(table, n=5):
sample(True, table, n)
def tail(table, n=5):
sample(False, table, n)
def sample(head, table, n):
direction = 'ASC' if head else 'DESC'
column = 'id' if table in ['files', 'tags'] else 'file_id'
rows = cur.execute('''
SELECT *
FROM {}
ORDER BY {} {}
LIMIT {}
'''.format(table, column, direction, n)).fetchall()
if head:
print('\n'.join([str(row) for row in rows]))
else:
print('\n'.join([str(row) for row in rows][::-1]))
@db_wrapper
def reset_all(cur, con):
tables_to_drop = [
'files', 'tags_id_files_id', 'tags', 'captchas'
]
for table_to_drop in tables_to_drop:
try:
cur.execute('DROP TABLE {}'.format(table_to_drop))
except Exception as e:
print('Unable to drop table {}, continuing.'.format(table_to_drop))
cur.execute("""CREATE TABLE files(
title text,
author text,
pubdate text,
uploaddate timestamp,
sourcelink text,
coverpath text,
filepath text,
numpages int,
filesize int,
filetype text,
md5 text,
sha1 text,
sha256 text,
id int PRIMARY KEY
)""")
cur.execute("""CREATE TABLE tags_id_files_id(
tag_id int,
file_id int
)""")
cur.execute("""CREATE TABLE tags(
tag_name text,
number_tagged int,
id int PRIMARY KEY
)""")
cur.execute("""CREATE TABLE captchas(
timestamp_hash text,
answer int
)""")
con.commit()
print('Database reset.')
'''
Parameters: None
'''
@db_wrapper
def get_num_files(cur, con):
return cur.execute('SELECT COUNT(*) FROM files').fetchone()[0]
'''
Parameters: None.
Returns the maximum ID found in the files table.
'''
@db_wrapper
def get_max_id(cur, con):
try:
max_id = cur.execute('''
SELECT id
FROM files
ORDER BY id DESC
''').fetchone()[0]
except Exception as e:
print('No files found, creating new max ID starting from 0:', e)
max_id = 0
return max_id
'''
Parameters:
attributes (dict) : a dictionary containing the following elements:
title: the title of the file
author: the author of the file
pubdate: the date the file was originally published
uploaddate: the timestamp at which the file was uploaded
sourcelink: a link to the original source of the file, or None
coverpath: the (local) path to the file's cover thumbnail
filepath: the (local) path to the file itself
numpages: the number of pages that the file has, or None
filesize: the size of the file
filetype: the type of the file
md5: the md5 hash of the file
sha1: the sha1 hash of the file
sha256: the sha256 hash of the file
id: the unique numeric identifier of the file
tags (iterable of strings) : an iterable of the file's tags
'''
@db_wrapper
def add_file(cur, con, attributes : dict, tags):
# add file to files table
ATTRIBUTE_LIST = [
'title', 'author', 'pubdate', 'uploaddate', 'sourcelink',
'coverpath', 'filepath', 'numpages', 'filesize',
'filetype', 'md5', 'sha1', 'sha256', 'id'
]
query = 'INSERT INTO files VALUES('
query += ', '.join([':' + attr for attr in ATTRIBUTE_LIST])
query += ')'
cur.execute(query, attributes)
# some attributes are added as tags
TAG_ATTRIBUTES = [
'title', 'author', 'pubdate',
'md5', 'sha1', 'sha256'
]
tags = set(tags)
for attr in TAG_ATTRIBUTES:
tags |= set(attributes[attr].split())
tags = {tag.lower() for tag in tags}
# add tags to tag-file id junction table, and update tags table
for tag in tags:
id_data = {'tag_id' : None, 'file_id' : attributes['id']}
## first, check if tag is already in db
print(tag)
cur.execute('SELECT id FROM tags WHERE tag_name = :tag', [tag])
rows = cur.fetchone()
if rows:
# tag is in db, rows contains ID of that tag
id_data['tag_id'] = rows[0]
else:
# determine max tag id so far
try:
max_id = cur.execute('SELECT MAX(id) FROM tags').fetchone()[0]
id_data['tag_id'] = max_id + 1
except:
max_id = 0
id_data['tag_id'] = max_id + 1
# create tags entry in db since it does not yet exist
tag_info = {
'tag_name' : tag, 'number_tagged' : 0, 'id' : max_id + 1
}
cur.execute('''INSERT INTO tags VALUES(
:tag_name, :number_tagged, :id
)''', tag_info)
# create entry in junction table
cur.execute('''
INSERT INTO tags_id_files_id VALUES(:tag_id, :file_id)
''', id_data)
# update count in tags table
cur.execute('''
UPDATE tags
SET number_tagged = number_tagged + 1
WHERE id = :tag_id
''', id_data)
con.commit()
print('Addition of file {} completed.'.format(attributes['id']))
'''
Parameters:
file_id (int): the unique id of the file to be removed
'''
@db_wrapper
def remove_file(cur, con, file_id : int):
# first, retrieve file to get filepath and coverpath for deleting from s3
file_rows = cur.execute('''
SELECT filepath, coverpath
FROM files
WHERE id = :file_id
''', {'file_id' : file_id}).fetchone()
if file_rows is None:
paths_found = False
else:
paths_found = True
filepath, coverpath = file_rows
# delete file from files table
cur.execute('''
DELETE FROM files
WHERE id = :file_id
''', {'file_id' : file_id})
# determine which tags the file had so they can be updated
cur.execute('''
SELECT tag_id
FROM tags_id_files_id
WHERE file_id = :file_id
''', {'file_id' : file_id})
affected_tags = [row[0] for row in cur.fetchall()]
# delete file tags from tags junction table
cur.execute('''
DELETE FROM tags_id_files_id
WHERE file_id = :file_id
''', {'file_id' : file_id})
# decrement tag counts
cur.executemany('''
UPDATE tags
SET number_tagged = number_tagged - 1
WHERE id = ?
''', [(tag_id,) for tag_id in affected_tags])
con.commit()
print('Removal of file {} from database completed. {} tags affected.'.format(
file_id, len(affected_tags)
))
if paths_found:
s3_utils.delete_file(filepath)
s3_utils.delete_cover(coverpath)
print('Successfully removed file and cover for {} from s3.'.format(filepath))
else:
print('Could not remove file and cover from s3 because there were no corresponding rows in the DB')
'''
Parameters:
tag_id (int): the id of the tag to remove
'''
@db_wrapper
def remove_tag(cur, con, tag_id : int):
# get number of affected records
num_affected = cur.execute('''
SELECT number_tagged
FROM tags
WHERE tag_id = :tag_id
''', {'tag_id' : tag_id}).fetchone()[0]
# delete tag from tags table
cur.execute('''
DELETE FROM tags
WHERE id = :tag_id
''', {'tag_id' : tag_id})
# delete tags from tags junction table
cur.execute('''
DELETE FROM tags_id_files_id
WHERE tag_id = :tag_id
''', {'tag_id' : tag_id})
con.commit()
print('Removal of tag {} completed. {} files affected.'.format(
tag_id, num_affected
))
'''
Parameters:
captcha_id (str): the ID (timestamp hash) of a captcha.
answer (int): the correct/expected answer to the captcha.
'''
@db_wrapper
def add_captcha_answer(cur, con, captcha_id, answer):
cur.execute('''
INSERT INTO captchas VALUES(:captcha_id, :answer)
''', {'captcha_id' : captcha_id, 'answer' : answer})
con.commit()
print('Addition of captcha [\'{}\' <=> {}] completed.'.format(
captcha_id[:6] + '...', answer
))
'''
Parameters:
captcha_id (str): the ID (timestamp hash) of a captcha.
Returns the answer associated with captcha_id and, if found,
removes the record from the table.
'''
@db_wrapper
def pop_captcha_answer(cur, con, captcha_id):
results = cur.execute('''
SELECT answer
FROM captchas
WHERE timestamp_hash = :captcha_id
''', {'captcha_id' : captcha_id}).fetchone()
if results == None:
return False
answer = results[0]
cur.execute('''
DELETE FROM captchas
WHERE timestamp_hash = :captcha_id
''', {'captcha_id' : captcha_id})
con.commit()
print('Popped [\'{}\' <=> {}] from captchas.'.format(
captcha_id[:6] + '...', answer
))
return answer
'''
Parameters:
tags (iterable of strings): an iterable of strings to search for as tags
'''
@db_wrapper
def search(cur, con, tags : list):
tags = list(set(filter(lambda x:x is not None, tags)))
# only return those attributes needed for displaying results
cur.execute('''
SELECT files.id, files.coverpath, files.title,
files.numpages, files.author, files.filesize
FROM files
WHERE files.id IN (SELECT junction.file_id
FROM tags_id_files_id AS junction
JOIN tags ON tags.id = junction.tag_id
WHERE tags.tag_name IN ({})
GROUP BY junction.file_id
HAVING COUNT(DISTINCT tags.tag_name) = ?);
'''.format(','.join(['?'] * len(tags))),
tags + [len(tags)])
return cur.fetchall()
@db_wrapper
def search_all(cur, con):
# only return those attributes needed for displaying results
cur.execute('''
SELECT files.id, files.coverpath, files.title,
files.numpages, files.author, files.filesize
FROM files
''')
return cur.fetchall()
'''
Parameters:
file_id (int): the ID of the file that was requested
'''
@db_wrapper
def get_file_info(cur, con, file_id : int) -> dict:
result = cur.execute('''
SELECT *
FROM files
WHERE files.id = ?;
''', (file_id,)).fetchone()
if result == None:
return False
(title, author, pubdate, uploaddate, sourcelink,
coverpath, filepath, numpages, filesize,
filetype, md5, sha1, sha256, _) = result
return {
'title' : title,
'author' : author,
'pubdate' : pubdate,
'uploaddate' : uploaddate,
'sourcelink' : sourcelink,
'coverpath' : coverpath,
'filepath' : filepath,
'numpages' : numpages,
'filesize' : filesize,
'filetype' : filetype,
'md5' : md5,
'sha1' : sha1,
'sha256' : sha256,
'id' : file_id
}
@db_wrapper
def get_download_attrs(cur, con, file_id : int) -> dict:
result = cur.execute('''
SELECT title, author, filepath
FROM files
WHERE files.id = ?;
''', (file_id,)).fetchone()
if result == None:
return False
(title, author, filepath) = result
return {
'title' : title,
'author' : author,
'filepath' : filepath,
'id' : file_id
}
if __name__ == '__main__':
# test that decorator works
print(head('tags', 3))