-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdatabasehandler.py
More file actions
86 lines (77 loc) · 2.09 KB
/
databasehandler.py
File metadata and controls
86 lines (77 loc) · 2.09 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
import urllib.parse as urlparse
import psycopg2
STATUS = False
def start_dbhandler(audio_db: str):
global STATUS
try:
result = urlparse.urlparse(audio_db)
username = result.username
password = result.password
database = result.path[1:]
hostname = result.hostname
port = result.port
except Exception as e:
print(e)
print("DB url is not valid")
STATUS = False
return None
try:
conn = psycopg2.connect(
database=database,
user=username,
password=password,
host=hostname,
port=port,
)
STATUS = True
except Exception as e:
print(e)
print("Connection establishment failed")
STATUS = False
return None
if conn:
cur = conn.cursor()
try:
cur.execute(
"CREATE TABLE IF NOT EXISTS Audios("
"yt_link TEXT PRIMARY KEY,"
"msg_id INTEGER)"
)
except Exception as e:
print(e)
print("Table creation failed")
STATUS = False
return None
conn.commit()
return conn
return False
def check_in_db(url, conn) -> int or None:
if STATUS:
cur = conn.cursor()
row = []
try:
cur.execute(
"SELECT msg_id from Audios where yt_link = {}".format(
"'" + url + "'")
)
row = cur.fetchall()
except Exception as e:
print(e)
print("Can't run find command!")
if len(row) > 0:
return int(row[0][0])
else:
return None
return None
def add_to_db(url: str, msg_id: int, conn):
if STATUS:
cur = conn.cursor()
cur.execute(
"INSERT INTO Audios(yt_link,msg_id)"
"VALUES ({},{}) ON CONFLICT UPDATE msg_id = {}".format(
"'" + url + "'", msg_id, msg_id
)
)
conn.commit()
else:
print("DB is not active cannot add the entry!")