Fix database discovery

Add table verification + date modified verification
This commit is contained in:
angelblue05 2019-01-11 19:57:20 -06:00
parent 7da421d970
commit ddc6b01055
2 changed files with 29 additions and 9 deletions

View File

@ -66,9 +66,20 @@ class Database(object):
path = xbmc.translatePath(path).decode('utf-8') path = xbmc.translatePath(path).decode('utf-8')
if not xbmcvfs.exists(path) and not silent: if not silent:
if not xbmcvfs.exists(path):
raise Exception("Database: %s missing" % path) raise Exception("Database: %s missing" % path)
conn = sqlite3.connect(path)
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
conn.close()
if not len(tables):
raise Exception("Database: %s malformed?" % path)
return path return path
def _discover_database(self, database): def _discover_database(self, database):
@ -76,21 +87,32 @@ class Database(object):
''' Grab the first database encountered, by most recent. ''' Grab the first database encountered, by most recent.
Will likely not work, but heck. Will likely not work, but heck.
''' '''
databases = xbmc.translatePath("special://database/").decode('utf-8')
types = { types = {
'video': "MyVideos", 'video': "MyVideos",
'music': "MyMusic", 'music': "MyMusic",
'texture': "Textures" 'texture': "Textures"
} }
database = types[database] database = types[database]
dirs, files = xbmcvfs.listdir(xbmc.translatePath("special://database/").decode('utf-8')) dirs, files = xbmcvfs.listdir(databases)
modified = {'file': None, 'time': 0}
for file in reversed(files): for file in reversed(files):
if file.startswith(database) and not file.endswith('-wal') and not file.endswith('-shm'): if file.startswith(database) and not file.endswith('-wal') and not file.endswith('-shm'):
LOG.info("Found database: %s", file) st = xbmcvfs.Stat(databases + file.decode('utf-8'))
modified_int = st.st_mtime()
LOG.debug("Database detected: %s time: %s", file.decode('utf-8'), modified_int)
if modified_int > modified['time']:
modified['time'] = modified_int
modified['file'] = file.decode('utf-8')
LOG.info("Loading discovered database: %s", modified)
self.discovered = True self.discovered = True
return xbmc.translatePath("special://database/%s" % file.decode('utf-8')).decode('utf-8') return xbmc.translatePath("special://database/%s" % modified['file'])
def _sql(self, file): def _sql(self, file):

View File

@ -128,8 +128,6 @@ class Library(threading.Thread):
LOG.info("Newly discovered database: %s", kodidb.path) LOG.info("Newly discovered database: %s", kodidb.path)
settings('DiscoveredDatabase', kodidb.path) settings('DiscoveredDatabase', kodidb.path)
self.monitor.settings['enable_db_discovery'] = False
settings('AskDiscoverDatabase.bool', False)
return False return False
else: else: