vvvvget/db.py

84 lines
2.3 KiB
Python
Raw Normal View History

2018-10-15 20:27:51 +02:00
import os
import sqlite3
class VVVVIDatabase():
def __init__(self, path):
self.path = path
self.is_valid = os.path.isfile(self.path)
def create(self):
con = sqlite3.connect(self.path)
cur = con.cursor()
2018-10-29 23:17:37 +01:00
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));")
2018-10-15 20:27:51 +02:00
con.commit()
con.close()
def last_serie(self):
con = sqlite3.connect(self.path)
cur = con.cursor()
cur.execute("SELECT id FROM series ORDER BY id DESC LIMIT 1;")
rows = cur.fetchall()
last = (rows[0][0] + 1) if len(rows) > 0 else 0
con.commit()
con.close()
return last
def series_id(self):
con = sqlite3.connect(self.path)
cur = con.cursor()
cur.execute("SELECT id, season_id FROM series ORDER BY id, season_id DESC;")
rows = cur.fetchall()
con.commit()
con.close()
# getting series in a useful format for later
series = {}
2018-10-17 23:43:44 +02:00
for i, s in rows:
2018-10-15 20:27:51 +02:00
if series.get(i) is None:
series[i] = []
series[i].append(s)
return series
def series_episodes(self, serie_id):
con = sqlite3.connect(self.path)
cur = con.cursor()
cur.execute("SELECT id, serie_id, season_id FROM episodes ORDER BY id, serie_id, season_id DESC;")
rows = cur.fetchall()
con.commit()
con.close()
# getting series in a useful format for later
series = {}
2018-10-17 23:43:44 +02:00
for i, s in rows:
2018-10-15 20:27:51 +02:00
if series.get(i) is None:
series[i] = []
series[i].append(s)
2018-10-17 23:43:44 +02:00
return series
2018-10-15 20:27:51 +02:00
def insert_serie(self, serie):
con = sqlite3.connect(self.path)
cur = con.cursor()
try:
cur.execute("INSERT INTO series (id, name, season_id, type) VALUES (?, ?, ?, ?);", serie)
con.commit()
con.close()
return True
except sqlite3.IntegrityError:
# serie gia' presente
pass
return False
def insert_episodes(self, eps):
con = sqlite3.connect(self.path)
cur = con.cursor()
for ep in eps:
try:
cur.execute("INSERT INTO episodes (id, serie_id, season_id, type, cdn_url) VALUES (?, ?, ?, ?, ?);", ep)
con.commit()
con.close()
return True
except sqlite3.IntegrityError:
# episodi gia' presenti
pass
2018-10-17 23:43:44 +02:00
return False