fix insert episodes

This commit is contained in:
thezero 2019-03-02 12:58:10 +01:00
parent 7e10077d33
commit ccd6df6a81

42
db.py
View File

@ -6,32 +6,30 @@ class VVVVIDatabase():
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path
self.is_valid = os.path.isfile(self.path) self.is_valid = os.path.isfile(self.path)
self.con = sqlite3.connect(self.path)
def __del__(self):
self.con.close()
def create(self): def create(self):
con = sqlite3.connect(self.path) cur = self.con.cursor()
cur = con.cursor()
cur.execute("CREATE TABLE series (id INTEGER NOT NULL, name TEXT NOT NULL, season_id INTEGER NOT NULL, type TEXT NOT NULL, PRIMARY KEY (id, season_id));") cur.execute("CREATE TABLE series (id INTEGER NOT NULL, name TEXT NOT NULL, season_id INTEGER NOT NULL, type TEXT NOT NULL, PRIMARY KEY (id, season_id));")
cur.execute("CREATE TABLE episodes (id INTEGER NOT NULL, serie_id INTEGER NOT NULL, season_id INTEGER NOT NULL, cdn_url TEXT NOT NULL, type TEXT NOT NULL, PRIMARY KEY (id, serie_id, season_id, cdn_url, type));") cur.execute("CREATE TABLE episodes (id INTEGER NOT NULL, serie_id INTEGER NOT NULL, season_id INTEGER NOT NULL, cdn_url TEXT NOT NULL, type TEXT NOT NULL, PRIMARY KEY (id, serie_id, season_id, cdn_url, type));")
con.commit() self.con.commit()
con.close()
def last_serie(self): def last_serie(self):
con = sqlite3.connect(self.path) cur = self.con.cursor()
cur = con.cursor()
cur.execute("SELECT id FROM series ORDER BY id DESC LIMIT 1;") cur.execute("SELECT id FROM series ORDER BY id DESC LIMIT 1;")
rows = cur.fetchall() rows = cur.fetchall()
last = (rows[0][0] + 1) if len(rows) > 0 else 0 last = (rows[0][0] + 1) if len(rows) > 0 else 0
con.commit() self.con.commit()
con.close()
return last return last
def series_id(self): def series_id(self):
con = sqlite3.connect(self.path) cur = self.con.cursor()
cur = con.cursor()
cur.execute("SELECT id, season_id FROM series ORDER BY id, season_id DESC;") cur.execute("SELECT id, season_id FROM series ORDER BY id, season_id DESC;")
rows = cur.fetchall() rows = cur.fetchall()
con.commit() self.con.commit()
con.close()
# getting series in a useful format for later # getting series in a useful format for later
series = {} series = {}
for i, s in rows: for i, s in rows:
@ -41,12 +39,10 @@ class VVVVIDatabase():
return series return series
def series_episodes(self, serie_id): def series_episodes(self, serie_id):
con = sqlite3.connect(self.path) cur = self.con.cursor()
cur = con.cursor()
cur.execute("SELECT id, serie_id, season_id FROM episodes ORDER BY id, serie_id, season_id DESC;") cur.execute("SELECT id, serie_id, season_id FROM episodes ORDER BY id, serie_id, season_id DESC;")
rows = cur.fetchall() rows = cur.fetchall()
con.commit() self.con.commit()
con.close()
# getting series in a useful format for later # getting series in a useful format for later
series = {} series = {}
for i, s in rows: for i, s in rows:
@ -56,12 +52,10 @@ class VVVVIDatabase():
return series return series
def insert_serie(self, serie): def insert_serie(self, serie):
con = sqlite3.connect(self.path) cur = self.con.cursor()
cur = con.cursor()
try: try:
cur.execute("INSERT INTO series (id, name, season_id, type) VALUES (?, ?, ?, ?);", serie) cur.execute("INSERT INTO series (id, name, season_id, type) VALUES (?, ?, ?, ?);", serie)
con.commit() self.con.commit()
con.close()
return True return True
except sqlite3.IntegrityError: except sqlite3.IntegrityError:
# serie gia' presente # serie gia' presente
@ -69,15 +63,11 @@ class VVVVIDatabase():
return False return False
def insert_episodes(self, eps): def insert_episodes(self, eps):
con = sqlite3.connect(self.path) cur = self.con.cursor()
cur = con.cursor()
for ep in eps: for ep in eps:
try: try:
cur.execute("INSERT INTO episodes (id, serie_id, season_id, type, cdn_url) VALUES (?, ?, ?, ?, ?);", ep) cur.execute("INSERT INTO episodes (id, serie_id, season_id, type, cdn_url) VALUES (?, ?, ?, ?, ?);", ep)
con.commit() self.con.commit()
con.close()
return True
except sqlite3.IntegrityError: except sqlite3.IntegrityError:
# episodi gia' presenti # episodi gia' presenti
pass pass
return False