Use text files instead of sqlite ccc
authorChristian Weiske <cweiske@cweiske.de>
Fri, 5 Jan 2018 19:32:21 +0000 (20:32 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 5 Jan 2018 19:32:21 +0000 (20:32 +0100)
usr/lib/enigma2/python/Plugins/Extensions/MediaPortal/additions/mediatheken/ccc.py

index e81d0c6..55dd11f 100644 (file)
@@ -9,8 +9,6 @@
 # - https://api.media.ccc.de/public/conferences/117
 # - https://api.media.ccc.de/public/events/4377
 
-import json
-import sqlite3
 import os.path as os_path
 
 from Plugins.Extensions.MediaPortal.plugin import _
@@ -50,9 +48,7 @@ class CccOverviewScreen(MPScreen):
                getPage('https://api.media.ccc.de/public/conferences', agent=std_headers, headers={'Content-Type': 'application/json'}).addCallback(self.parseConferences).addErrback(self.dataError)
 
         def parseConferences(self, data):
-               watcheduids = []
-               for row in self.watchdb.getWatched(3):
-                       watcheduids.append(row['uid'])
+               watcheduids = self.watchdb.getWatched(3)
 
                recent = []
                alls   = []
@@ -229,59 +225,34 @@ class CccConferenceScreen(MPScreen):
                        return
 
                self.watchdb.addWatched(guid)
-               self.watchdb.addWatched(self.acronym, group = 'ccc-conferences')
+               confwatchdb = CccWatchDb('ccc-conferences')
+               confwatchdb.addWatched(self.acronym)
                self.session.open(SimplePlayer, [(title, url, image_url)], showPlaylist=False, ltype='ccc')
 
 class CccWatchDb:
        """Store information what videos have been watched already
-          Could be used as generic has-been-watched database for all additions
        """
 
        def __init__(self, group):
-               self.__dbfile = config.mediaportal.watchlistpath.value + "mp_watched.db"
-               self.__conn = None
-               self.group = group
-               self.__connect()
-
-       def __connect(self):
-               if not os_path.exists(self.__dbfile):
-                       self.__conn = sqlite3.connect(self.__dbfile)
-                       self.__createDB()
-               else:
-                       self.__conn = sqlite3.connect(self.__dbfile)
-               self.__conn.row_factory = sqlite3.Row
-
-       def __createDB(self):
-               c = self.__conn.cursor()
-               #sqlite has an automatic special ROWID column
-               c.execute('CREATE TABLE watched (`group` TEXT, uid TEXT, created TEXT, additional TEXT)')
-               c.execute('CREATE UNIQUE INDEX watchkeys ON watched (`group`, uid)')
-               self.__conn.commit()
-               c.close()
-
-       def addWatched(self, uid, additionalData = None, group = None):
-               if group is None:
-                       group = self.group
-               sql = "INSERT OR REPLACE INTO watched (`group`, uid, created, additional) VALUES (?, ?, datetime('now'), ?)"
-               c = self.__conn.cursor()
-               c.execute(sql, (group, uid, json.dumps(additionalData)))
-               self.__conn.commit()
-               c.close()
+               self.__dbfile = config.mediaportal.watchlistpath.value + "mp_" + group
+               self.watched = []
+               if os_path.exists(self.__dbfile):
+                       rawData = open(self.__dbfile, 'r')
+                       for line in rawData:
+                               self.watched.append(line.strip())
+
+       def addWatched(self, uid):
+               #add/move to front so we can later easily cap the list
+               if uid in self.watched:
+                       self.watched.remove(uid)
+               self.watched.insert(0, uid)
+
+               file = open(self.__dbfile, 'w')
+               for uid in self.watched:
+                       file.write('%s\n' % uid)
 
        def hasBeenWatched(self, uid):
-               sql = "SELECT COUNT(*) as num FROM watched WHERE `group` = ? AND uid = ?"
-               c = self.__conn.cursor()
-               c.execute(sql, (self.group, uid))
-               row = c.fetchone()
-               c.close()
-               return row['num'] > 0
+               return uid in self.watched
 
        def getWatched(self, limit = 10, offset = 0):
-               sql = "SELECT * FROM watched WHERE `group` = ?"\
-                     + " ORDER BY created DESC"\
-                     + " LIMIT ?, ?"
-               c = self.__conn.cursor()
-               c.execute(sql, (self.group, offset, limit))
-               rows = c.fetchall()
-               c.close()
-               return rows
+               return self.watched[offset:limit]