db.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os
  2. import sqlite3
  3. class VVVVIDatabase():
  4. def __init__(self, path):
  5. self.path = path
  6. self.is_valid = os.path.isfile(self.path)
  7. self.con = sqlite3.connect(self.path)
  8. def __del__(self):
  9. self.con.close()
  10. def create(self):
  11. cur = self.con.cursor()
  12. 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));")
  13. 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));")
  14. self.con.commit()
  15. def last_serie(self):
  16. cur = self.con.cursor()
  17. cur.execute("SELECT id FROM series ORDER BY id DESC LIMIT 1;")
  18. rows = cur.fetchall()
  19. last = (rows[0][0] + 1) if len(rows) > 0 else 0
  20. self.con.commit()
  21. return last
  22. def series_id(self):
  23. cur = self.con.cursor()
  24. cur.execute("SELECT id, season_id FROM series ORDER BY id, season_id DESC;")
  25. rows = cur.fetchall()
  26. self.con.commit()
  27. # getting series in a useful format for later
  28. series = {}
  29. for i, s in rows:
  30. if series.get(i) is None:
  31. series[i] = []
  32. series[i].append(s)
  33. return series
  34. def series_episodes(self, serie_id):
  35. cur = self.con.cursor()
  36. cur.execute("SELECT id, serie_id, season_id FROM episodes ORDER BY id, serie_id, season_id DESC;")
  37. rows = cur.fetchall()
  38. self.con.commit()
  39. # getting series in a useful format for later
  40. series = {}
  41. for i, s in rows:
  42. if series.get(i) is None:
  43. series[i] = []
  44. series[i].append(s)
  45. return series
  46. def insert_serie(self, serie):
  47. cur = self.con.cursor()
  48. try:
  49. cur.execute("INSERT INTO series (id, name, season_id, type) VALUES (?, ?, ?, ?);", serie)
  50. self.con.commit()
  51. return True
  52. except sqlite3.IntegrityError:
  53. # serie gia' presente
  54. pass
  55. return False
  56. def insert_episodes(self, eps):
  57. cur = self.con.cursor()
  58. for ep in eps:
  59. try:
  60. cur.execute("INSERT INTO episodes (id, serie_id, season_id, type, cdn_url) VALUES (?, ?, ?, ?, ?);", ep)
  61. self.con.commit()
  62. except sqlite3.IntegrityError:
  63. # episodi gia' presenti
  64. pass