db.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. def create(self):
  8. con = sqlite3.connect(self.path)
  9. cur = con.cursor()
  10. cur.execute("CREATE TABLE series (id INTEGER, name TEXT NOT NULL, season_id INTEGER, type TEXT, PRIMARY KEY (id, season_id));")
  11. cur.execute("CREATE TABLE episodes (id INTEGER, serie_id INTEGER, season_id INTEGER, cdn_url TEXT NOT NULL, type TEXT NOT NULL);")
  12. con.commit()
  13. con.close()
  14. def last_serie(self):
  15. con = sqlite3.connect(self.path)
  16. cur = 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. con.commit()
  21. con.close()
  22. return last
  23. def series_id(self):
  24. con = sqlite3.connect(self.path)
  25. cur = con.cursor()
  26. cur.execute("SELECT id, season_id FROM series ORDER BY id, season_id DESC;")
  27. rows = cur.fetchall()
  28. con.commit()
  29. con.close()
  30. # getting series in a useful format for later
  31. series = {}
  32. for i,s in rows:
  33. if series.get(i) is None:
  34. series[i] = []
  35. series[i].append(s)
  36. return series
  37. def series_episodes(self, serie_id):
  38. con = sqlite3.connect(self.path)
  39. cur = con.cursor()
  40. cur.execute("SELECT id, serie_id, season_id FROM episodes ORDER BY id, serie_id, season_id DESC;")
  41. rows = cur.fetchall()
  42. con.commit()
  43. con.close()
  44. # getting series in a useful format for later
  45. series = {}
  46. for i,s in rows:
  47. if series.get(i) is None:
  48. series[i] = []
  49. series[i].append(s)
  50. return series
  51. def insert_serie(self, serie):
  52. con = sqlite3.connect(self.path)
  53. cur = con.cursor()
  54. try:
  55. cur.execute("INSERT INTO series (id, name, season_id, type) VALUES (?, ?, ?, ?);", serie)
  56. con.commit()
  57. con.close()
  58. return True
  59. except sqlite3.IntegrityError:
  60. # serie gia' presente
  61. pass
  62. return False
  63. def insert_episodes(self, eps):
  64. con = sqlite3.connect(self.path)
  65. cur = con.cursor()
  66. try:
  67. cur.executemany("INSERT INTO episodes (id, serie_id, season_id, type, cdn_url) VALUES (?, ?, ?, ?, ?);", eps)
  68. con.commit()
  69. con.close()
  70. return True
  71. except sqlite3.IntegrityError:
  72. # episodi gia' presenti
  73. pass
  74. return False