1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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()
- cur.execute("CREATE TABLE series (id INTEGER, name TEXT NOT NULL, season_id INTEGER, type TEXT, PRIMARY KEY (id, season_id));")
- cur.execute("CREATE TABLE episodes (id INTEGER, serie_id INTEGER, season_id INTEGER, cdn_url TEXT NOT NULL, type TEXT NOT NULL);")
- 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 = {}
- for i,s in rows:
- 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 = {}
- for i,s in rows:
- if series.get(i) is None:
- series[i] = []
- series[i].append(s)
- return series
- 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()
- try:
- cur.executemany("INSERT INTO episodes (id, serie_id, season_id, type, cdn_url) VALUES (?, ?, ?, ?, ?);", eps)
- con.commit()
- con.close()
- return True
- except sqlite3.IntegrityError:
- # episodi gia' presenti
- pass
- return False
|