-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdb_util.py
61 lines (49 loc) · 2.3 KB
/
db_util.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
#!/usr/bin/python3
import sqlite3
import os
class database:
# Initialise the database
def __init__(self):
self.conn = sqlite3.connect("events.db")
self.c = self.conn.cursor()
# Connect to the database. If the file doesn't exist, it creates it.
def db_connect(self):
# Thanks to stackoverflow for help with this one.
# https://stackoverflow.com/a/1604121
database_exists = self.c.execute("SELECT name FROM sqlite_master WHERE name='downloads'").fetchone()
if not database_exists:
# id = DiceVideoId
# name = the name of event/episode
# quality = bitrate
# partial = true/false
# date = timestamp
self.c.execute("CREATE TABLE downloads (id integer unique, name text, quality text, partial_download text, date integer)")
self.conn.commit()
# Insert download into the database
def db_ins(self, video_id, video_name, video_qual, partial, timestamp):
try:
self.c.execute(f"INSERT INTO downloads VALUES ('{video_id}', '{video_name}', '{video_qual}', '{partial}', '{timestamp}')")
self.conn.commit()
except sqlite3.IntegrityError:
print(f"Error: Couldn't add {video_id} to the database. ID already exists.")
# Update download information in the database
def db_upd(self, video_id, video_name, video_qual, partial, timestamp):
self.c.execute(f"UPDATE downloads SET name = '{video_name}', quality = '{video_qual}', partial_download = '{partial}', date = '{timestamp}' WHERE id = {video_id}")
self.conn.commit()
# Query the database for previously downloaded episode
def db_query(self, video_id, is_partial_download):
if not is_partial_download:
result = self.c.execute(f"SELECT date FROM downloads WHERE id = '{video_id}'")
else:
result = self.c.execute(f"SELECT date FROM downloads WHERE id = '{video_id}' AND partial_download = 'True'")
if result.fetchone():
return True
else:
return False
# Close the database, and commit any final changes
def db_close(self):
self.conn.commit()
self.conn.close()
if __name__ == "__main__":
print("Please run python main.py instead.")
pass