/
psql.py
596 lines (517 loc) · 20.5 KB
/
psql.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# YouTubeMDBot
# Copyright (C) 2019 - Javinator9889
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from abc import ABC
from concurrent.futures import Future
from datetime import datetime
from threading import Condition
from threading import Lock
from threading import Thread
from typing import List
from typing import Optional
from collections import deque
import os
import psycopg2
from .. import DB_HOST
from .. import DB_NAME
from .. import DB_PASSWORD
from .. import DB_PORT
from .. import DB_USER
instance_lock = Lock()
class Query:
def __init__(self,
statement: str,
values: tuple = None,
returning_id: bool = False):
self.statement = statement
self.values = values
self.returning_id = returning_id
self.return_value = Future()
class PostgreSQLItem:
__instance = None
def __new__(cls,
**kwargs):
with instance_lock:
if PostgreSQLItem.__instance is None:
cls.__instance = object.__new__(cls)
cls.__instance.must_initialize = True
return cls.__instance
def __init__(self, min_ops: int = 100, **kwargs):
if self.must_initialize:
self.connection = psycopg2.connect(user=DB_USER,
password=DB_PASSWORD,
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME)
self.min_ops = min_ops
self.lock = Lock()
self.close = False
self.pending_ops = deque()
self.waiting_ops = deque()
self.updating_database = False
self.iucond = Condition()
self.qcond = Condition()
self._iuthread = Thread(target=self.__iuhandler,
name="iuthread")
self._qthread = Thread(target=self.__qhandler,
name="qthread")
if not self._iuthread.is_alive():
self._iuthread.start()
if not self._qthread.is_alive():
self._qthread.start()
self.must_initialize = False
for key, value in kwargs.items():
setattr(self, key, value)
@property
def close(self) -> bool:
with self.lock:
return self.__close
@close.setter
def close(self, value: bool):
with self.lock:
self.__close = value
@property
def updating_database(self) -> bool:
with self.lock:
return self.__updating_database
@updating_database.setter
def updating_database(self, value: bool):
with self.lock:
self.__updating_database = value
def __iuhandler(self):
while not self.close:
with self.iucond:
self.iucond.wait_for(
lambda: len(self.pending_ops) >= self.min_ops or self.close
)
print(f"[iuhandler] - condition is true: pending ops: "
f"{len(self.pending_ops)} or closed: {self.close}")
self.updating_database = True
with self.connection.cursor() as cursor:
while len(self.pending_ops) > 0:
query = self.pending_ops.pop()
if query is None:
continue
cursor.execute(query.statement, query.values)
if query.returning_id:
query.return_value.set_result(cursor.fetchone()[0])
self.connection.commit()
self.updating_database = False
with self.qcond:
self.qcond.notify_all()
print("iuhandler exited")
def __qhandler(self):
while not self.close:
with self.qcond:
self.qcond.wait_for(
lambda: len(self.waiting_ops) > 0 and
not self.updating_database or self.close
)
print(f"[qhandler] - condition is true: waiting ops: "
f"{len(self.waiting_ops)} and not {self.updating_database} "
f"or closed: {self.close}")
while len(self.waiting_ops) > 0:
query = self.waiting_ops.pop()
if query is None:
continue
self.pending_ops.append(query)
with self.iucond:
self.iucond.notify_all()
print("qhandler exited")
def insert(self, query: str, args=(), returning_id: bool = False) -> Query:
if not self.close:
insert_query = Query(query, args, returning_id)
self.waiting_ops.append(insert_query)
with self.qcond:
self.qcond.notify_all()
return insert_query
def update(self, query: str, args=()):
if not self.close:
update_query = Query(query, args)
self.waiting_ops.append(update_query)
with self.qcond:
self.qcond.notify_all()
def fetchone(self, query: str, args=()) -> list:
if not self.close:
with self.connection.cursor() as cursor:
cursor.execute(query, args)
return cursor.fetchone()
def fetchmany(self, query: str, rows: int, args=()) -> list:
if not self.close:
with self.connection.cursor() as cursor:
cursor.execute(query, args)
return cursor.fetchmany(rows)
def fetchall(self, query: str, args=()) -> list:
if not self.close:
with self.connection.cursor() as cursor:
cursor.execute(query, args)
return cursor.fetchall()
def delete(self, query: str, args=()):
if not self.close:
with self.connection.cursor() as cursor:
cursor.execute(query, args)
self.connection.commit()
def callproc(self, proc: str, args=()) -> list:
if not self.close:
with self.connection.cursor() as cursor:
cursor.callproc(proc, args)
return cursor.fetchall()
def stop(self):
print("deleting class")
self.close = True
print(f"is there any waiting operation? {len(self.waiting_ops) > 0}")
# if len(self.waiting_ops) > 0:
with self.qcond:
self.qcond.notify_all()
self._qthread.join()
print(f"is there any pending operation? {len(self.pending_ops) > 0}")
# if len(self.pending_ops) > 0:
with self.iucond:
self.iucond.notify_all()
self._iuthread.join()
print("closing db connection")
self.connection.close()
def __del__(self):
try:
self.stop()
except Exception as e:
print(e)
class PostgreSQLBase(ABC):
def __init__(self, item: PostgreSQLItem):
self.postgres_item = item
@property
def connection(self):
return self.postgres_item.connection
def insert(self, query: str, args=(), returning_id: bool = False) -> Query:
return self.postgres_item.insert(query, args, returning_id)
def update(self, query: str, args=()):
self.postgres_item.update(query, args)
def fetchone(self, query: str, args=()) -> list:
return self.postgres_item.fetchone(query, args)
def fetchmany(self, query: str, rows: int, args=()) -> list:
return self.postgres_item.fetchmany(query, rows, args)
def fetchall(self, query: str, args=()) -> list:
return self.postgres_item.fetchall(query, args)
def delete(self, query: str, args=()):
self.postgres_item.delete(query, args)
def callproc(self, proc: str, args=()) -> list:
return self.callproc(proc, args)
class Initializer(PostgreSQLBase):
def init(self):
import re
self.postgres_item.updating_database = True
dirname = os.path.dirname(os.path.realpath(__file__))
filename = f"{dirname}/psql_model.sql"
re_combine_whitespace = re.compile(r"\s+")
print(f"Found file: {filename}")
instructions = list()
current_instruction = ''
with open(filename, 'r') as file:
while (line := file.readline()) != '':
if line.startswith("--") and line[2] != '#':
continue
elif line.startswith("--#"):
current_instruction = re_combine_whitespace \
.sub(' ', current_instruction).strip()
instructions.append(current_instruction)
current_instruction = ''
else:
line = re_combine_whitespace.sub(' ', line)
current_instruction += line
print("Executing instructions")
with self.connection.cursor() as cursor:
for instruction in instructions:
print(f"Running instruction {instruction}")
cursor.execute(instruction)
self.connection.commit()
self.postgres_item.updating_database = False
with self.postgres_item.qcond:
self.postgres_item.qcond.notify_all()
class UserDB(PostgreSQLBase):
def get_user_information(self, user_id: int) -> dict:
data = self.fetchone(
"""SELECT id, name, tag, lang, first_access
FROM youtubemd.User WHERE id = %s""", (user_id,)
)
return {
"id": data[0],
"name": data[1],
"tag": data[2],
"lang": data[3],
"first_access": data[4]
}
def get_user_history(self, user_id: int) -> list:
data = self.fetchall(
"""SELECT id, file_id, metadata_id, date FROM youtubemd.History
WHERE user_id = %s""", (user_id,)
)
result = []
for row in data:
result.append({
"id": row[0],
"file_id": row[1],
"metadata_id": row[2],
"date": row[3]
})
return result
def register_new_user(self, user_id: int, name: str, tag: str, lang: str):
now = datetime.now()
query = """
INSERT INTO youtubemd.User (id, name, tag, lang, first_access)
VALUES (%s, %s, %s, %s, %s)
"""
self.insert(query, (user_id, name, tag, lang, now))
self.insert(
"""INSERT INTO youtubemd.Preferences (user_id) VALUES (%s)""",
(user_id,)
)
def update_username(self, user_id: int, name: str):
self.update(
"""UPDATE youtubemd.User SET name = %s WHERE user_id = %s""",
(name, user_id)
)
def update_user_lang(self, user_id: int, lang: str):
self.update(
"""UPDATE youtubemd.User SET lang = %s WHERE user_id = %s""",
(lang, user_id)
)
def update_user_tag(self, user_id: int, tag: str):
self.update(
"""UPDATE youtubemd.User SET tag = %s WHERE user_id = %s""",
(tag, user_id)
)
def register_new_download(self,
user_id: int,
file_id: str,
metadata_id: str):
now = datetime.now()
self.insert(
"""INSERT INTO youtubemd.History
(file_id, user_id, metadata_id, date)
VALUES (%s, %s, %s, %s)""",
(file_id, metadata_id, user_id, now.date())
)
class PreferencesDB(PostgreSQLBase):
def get_user_preferences(self, user_id: int) -> dict:
data = self.fetchone(
"""SELECT audio_format, audio_quality, send_song_link,
ask_for_metadata FROM youtubemd.Preferences WHERE User_id = %s""",
(user_id,)
)
return {
"audio_format": data[0],
"audio_quality": data[1],
"send_song_link": data[2],
"ask_for_metadata": data[3]
}
def update_user_audio_format(self, user_id: int, audio_format: str):
self.update(
"""UPDATE youtubemd.Preferences SET audio_format = %s WHERE
user_id = %s""", (audio_format, user_id)
)
def update_user_audio_quality(self, user_id: int, audio_quality: str):
self.update(
"""UPDATE youtubemd.Preferences SET audio_quality = %s WHERE
user_id = %s""", (audio_quality, user_id)
)
def update_user_behaviour(self, user_id: int, behaviour: str):
self.update(
"""UPDATE youtubemd.Preferences SET song_behaviour = %s WHERE
user_id = %s""", (behaviour, user_id)
)
def update_user_send_song_link(self, user_id: int, send_song_link: bool):
self.update(
"""UPDATE youtubemd.Preferences SET send_song_link = %s WHERE
user_id = %s""", (send_song_link, user_id)
)
class YouTubeDB(PostgreSQLBase):
def set_youtube_id(self, youtube_id: str):
self.insert(
"""INSERT INTO youtubemd.YouTube(id) VALUES (%s)
ON CONFLICT DO NOTHING""",
(youtube_id,)
)
def increment_times_requested(self, youtube_id: str):
self.update(
"""UPDATE youtubemd.YouTube SET
times_requested = times_requested + 1 WHERE id = %s""",
(youtube_id,)
)
def is_id_registered(self, youtube_id: str) -> bool:
return self.fetchone(
"""SELECT EXISTS(SELECT 1 FROM youtubemd.YouTube WHERE id = %s)""",
(youtube_id,)
)[0]
class MetadataDB(PostgreSQLBase):
def register_new_metadata(self,
artist: str,
album: str,
cover: bytes,
release_id: str,
recording_id: str,
duration: int,
title: str,
youtube_id: str,
is_custom_metadata: bool = False) -> int:
metadata_id = self.insert(
"""INSERT INTO youtubemd.Metadata(artist, album, cover,
release_id, recording_id, duration, title, custom_metadata)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING id""",
(artist, album, cover, release_id, recording_id, duration, title,
is_custom_metadata), returning_id=True
).return_value.result()
self.insert(
"""INSERT INTO youtubemd.Video_Has_Metadata(id, metadata_id)
VALUES (%s, %s)""", (youtube_id, metadata_id)
)
return metadata_id
def get_metadata_for_id(self, metadata_id: int) -> dict:
data = self.fetchone(
"""SELECT artist, album, cover, release_id, recording_id,
duration, title, custom_metadata FROM youtubemd.Metadata
WHERE id = %s""", (metadata_id,)
)
return {
"artist": data[0],
"album": data[1],
"cover": data[2],
"release_id": data[3],
"recording_id": data[4],
"duration": data[5],
"title": data[6],
"custom_metadata": data[7]
}
def get_metadata_for_youtube_id(self, youtube_id: str) -> dict:
data = self.get_metadata_id_for_youtube_id(youtube_id)
return self.get_metadata_for_id(data)
def get_metadata_id_for_youtube_id(self, youtube_id: str) -> int:
return self.fetchone(
"""SELECT metadata_id FROM youtubemd.Video_Has_Metadata
WHERE id = %s""", (youtube_id,)
)[0]
def update_metadata(self,
metadata_id: int,
artist: str,
album: str,
cover: bytes,
release_id: str,
recording_id: str,
duration: int,
title: str,
is_custom_metadata: bool = False):
self.update(
"""UPDATE youtubemd.Metadata SET
artist = %s,
album = %s,
cover = %s,
release_id = %s,
recording_id = %s,
duration = %s,
title = %s,
custom_metadata = %s WHERE id = %s""",
(artist, album, cover, release_id, recording_id, duration, title,
is_custom_metadata, metadata_id)
)
class FileDB(PostgreSQLBase):
def new_file(self,
file_id: str,
metadata_id: int,
audio_quality: str,
size: int,
user_id: int):
self.insert(
"""INSERT INTO youtubemd.File(id, metadata_id, audio_quality, size)
VALUES (%s, %s, %s, %s) ON CONFLICT DO NOTHING""",
(file_id, metadata_id, audio_quality, size)
)
date = datetime.now().date()
self.insert(
"""INSERT INTO youtubemd.History(file_id, user_id, metadata_id,
date) VALUES (%s, %s, %s, %s) ON CONFLICT DO NOTHING""",
(file_id, user_id, metadata_id, date)
)
def get_file_info_by_id(self, file_id: str) -> dict:
data = self.fetchone(
"""SELECT id, metadata_id, audio_quality, size
FROM youtubemd.File WHERE file_id = %s""", (file_id,)
)
return {
"id": data[0],
"metadata_id": data[1],
"audio_quality": data[2],
"size": data[3]
}
def get_file_for_youtube_id(self, youtube_id: str) -> Optional[str]:
youtube_database = YouTubeDB(self.postgres_item)
if youtube_database.is_id_registered(youtube_id):
metadata = MetadataDB(self.postgres_item)
metadata_id = metadata.get_metadata_id_for_youtube_id(youtube_id)
return self.get_file_for_metadata_id(metadata_id)
else:
return None
def get_file_for_metadata_id(self, metadata_id: int) -> str:
return self.fetchone(
"""SELECT file_id FROM youtubemd.File WHERE metadata_id = %s""",
(metadata_id,)
)[0]
class HistoryDB(PostgreSQLBase):
def get_history_for_user_id(self, user_id: int) -> List[dict]:
data = self.fetchall(
"""SELECT id, file_id, user_id, metadata_id, date FROM
youtubemd.History WHERE user_id = %s""", (user_id,)
)
history = list()
for value in data:
history.append(
{
"id": value[0],
"file_id": value[1],
"user_id": value[2],
"metadata_id": value[3]
}
)
return history
def remove_history_entry(self, history_id: int):
self.delete(
"""DELETE FROM youtubemd.History WHERE id = %s""", (history_id,)
)
def remove_all_history_for_user(self, user_id: int):
self.delete(
"""DELETE FROM youtubemd.History WHERE user_id = %s""", (user_id,)
)
class YouTubeStatsDB(PostgreSQLBase):
def _get_top_ten(self, procedure: str, name: str) -> List[dict]:
data = self.callproc(procedure)
top_ten = list()
for values in data:
top_ten.append(
{
"id": values[0],
name: values[1]
}
)
return top_ten
def get_top_ten_daily(self) -> List[dict]:
return self._get_top_ten("youtubemd.top_10_daily", "daily_requests")
def get_top_ten_weekly(self) -> List[dict]:
return self._get_top_ten("youtubemd.top_10_weekly", "weekly_requests")
def get_top_ten_monthly(self) -> List[dict]:
return self._get_top_ten("youtubemd.top_10_monthly", "monthly_requests")
def clear_top_ten_daily(self):
self.callproc("youtubemd.clear_daily_stats")
def clear_top_ten_weekly(self):
self.callproc("youtubemd.clear_weekly_stats")
def clear_top_ten_monthly(self):
self.callproc("youtubemd.clear_monthly_stats")