Enigma2 plugin to to play various online streams (mostly Latvian).

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import traceback
  3. import sqlite3
  4. DB_VERSION = 1
  5. class CueSheetDAO(object):
  6. instance = None
  7. def __init__(self, db_path):
  8. CueSheetDAO.instance = self
  9. self.db_path = "%s_v%d.db"%(os.path.splitext(db_path)[0],DB_VERSION)
  10. db_is_new = not os.path.exists(self.db_path)
  11. print '[CueSheetDAO] init', self.db_path
  12. if db_is_new:
  13. print '[CueSheetDAO] creating schema'
  14. self.create_schema()
  15. else:
  16. print '[CueSheetDAO] database exists, assume schema does, too.'
  17. def create_schema(self):
  18. with sqlite3.connect(self.db_path) as conn:
  19. schema = """
  20. create table cuesheet (
  21. id integer primary key autoincrement not null,
  22. path text unique
  23. );
  24. create table mark (
  25. id integer primary key autoincrement not null,
  26. time integer,
  27. type integer,
  28. cuesheet_id integer not null references cuesheet(id),
  29. UNIQUE (time, cuesheet_id) ON CONFLICT IGNORE
  30. );
  31. """
  32. conn.executescript(schema)
  33. def clean_db(self):
  34. try:
  35. os.remove(self.db_path)
  36. except OSError as e:
  37. print '[CueSheetDAO] error when cleaning db', str(e)
  38. return False
  39. else:
  40. self.create_schema()
  41. return True
  42. def get_cut_list(self, path):
  43. print '[CueSheetDAO] getCutList for %s' % path.encode('utf-8')
  44. with sqlite3.connect(self.db_path) as conn:
  45. conn.row_factory = sqlite3.Row
  46. cutlist = []
  47. cursor = conn.cursor()
  48. query = """
  49. select time, type
  50. from mark
  51. inner join cuesheet
  52. on mark.cuesheet_id = cuesheet.id
  53. where cuesheet.path = ?
  54. """
  55. cursor.execute(query, (path,))
  56. for row in cursor.fetchall():
  57. cutlist.append((row['time'], row['type']))
  58. if len(cutlist) > 0:
  59. print '[CueSheetDAO] getCutList - succesfull'
  60. return cutlist
  61. def set_cut_list(self, path, cutlist):
  62. print '[CueSheetDAO] setCutList for %s' % path.encode('utf-8')
  63. with sqlite3.connect(self.db_path) as conn:
  64. try:
  65. cursor = conn.cursor()
  66. cursor.execute("insert or ignore into cuesheet (path) values (?)", (path,))
  67. query = """
  68. delete from mark
  69. where cuesheet_id in
  70. (select id from cuesheet where path = ?)
  71. """
  72. cursor.execute(query, (path,))
  73. cuesheet_id = cursor.execute("select id from cuesheet where path = ?", (path,)).fetchone()[0]
  74. cursor.executemany("insert into mark (time, type, cuesheet_id) values (?,?,?)", ((time, type, cuesheet_id) for time, type in cutlist))
  75. except Exception:
  76. traceback.print_exc()
  77. conn.rollback()
  78. else:
  79. print '[CueSheetDAO] setCutList for %s was succesfull' % path.encode('utf-8')
  80. conn.commit()