forked from vitiko98/qobuz-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
39 lines (32 loc) · 1.11 KB
/
db.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
import logging
import sqlite3
from qobuz_dl.color import YELLOW, RED
logger = logging.getLogger(__name__)
def create_db(db_path):
with sqlite3.connect(db_path) as conn:
try:
conn.execute("CREATE TABLE downloads (id TEXT UNIQUE NOT NULL);")
logger.info(f"{YELLOW}Download-IDs database created")
except sqlite3.OperationalError:
pass
return db_path
def handle_download_id(db_path, item_id, add_id=False):
if not db_path:
return
with sqlite3.connect(db_path) as conn:
# If add_if is False return a string to know if the ID is in the DB
# Otherwise just add the ID to the DB
if add_id:
try:
conn.execute(
"INSERT INTO downloads (id) VALUES (?)",
(item_id,),
)
conn.commit()
except sqlite3.Error as e:
logger.error(f"{RED}Unexpected DB error: {e}")
else:
return conn.execute(
"SELECT id FROM downloads where id=?",
(item_id,),
).fetchone()