Add database discovery

This commit is contained in:
angelblue05 2019-01-10 17:38:57 -06:00
parent d43b261f0b
commit 23686f975f
5 changed files with 91 additions and 9 deletions

View file

@ -922,6 +922,14 @@ msgctxt "#33188"
msgid "Would you like to sync Rotten Tomatoes ratings?"
msgstr ""
msgctxt "#33189"
msgid "The database version detected is unsupported. Syncing may not work, proceed anyway?"
msgstr ""
msgctxt "#33190"
msgid "Enable Kodi database discovery"
msgstr ""
msgctxt "#33191"
msgid "Restart Emby for Kodi to apply this change?"
msgstr ""

View file

@ -31,6 +31,7 @@ class Database(object):
db.conn.commit()
'''
timeout = 120
discovered = False
def __init__(self, file=None, commit_close=True):
@ -42,17 +43,16 @@ class Database(object):
def __enter__(self):
''' Open the connection and return the Database class.
This is to allow for both the cursor and conn to be accessible.
at any time.
This is to allow for the cursor, conn and others to be accessible.
'''
self.path = self._sql(self.db_file)
self.conn = sqlite3.connect(self.path, timeout=self.timeout)
self.cursor = self.conn.cursor()
if self.db_file in ('video', 'music', 'texture', 'emby'):
self.conn.execute("PRAGMA journal_mode=WAL")
self.conn.execute("PRAGMA journal_mode=WAL") # to avoid writing conflict with kodi
LOG.info("--->[ database: %s ] %s", self.db_file, id(self.conn))
LOG.debug("--->[ database: %s ] %s", self.db_file, id(self.conn))
if not window('emby_db_check.bool') and self.db_file == 'emby':
@ -71,10 +71,32 @@ class Database(object):
return path
def _discover_database(self, database):
''' Grab the first database encountered, by most recent.
Will likely not work, but heck.
'''
types = {
'video': "MyVideos",
'music': "MyMusic",
'texture': "Textures"
}
database = types[database]
dirs, files = xbmcvfs.listdir(xbmc.translatePath("special://database/").decode('utf-8'))
for file in reversed(files):
if file.startswith(database) and not file.endswith('-wal') and not file.endswith('-shm'):
LOG.info("Found database: %s", file)
self.discovered = True
return xbmc.translatePath("special://database/%s" % file.decode('utf-8')).decode('utf-8')
def _sql(self, file):
''' Get the database path based on the file objects/obj_map.json
Compatible check, in the event multiple db version are supported with the same Kodi version.
Discover by file as a last resort.
'''
databases = obj.Objects().objects
@ -91,13 +113,13 @@ class Database(object):
databases[file] = self._get_database(databases[alt_file])
return databases[file]
except IndexError: # No other db options
break
except KeyError: # No other db options
databases[file] = self._discover_database(file)
return databases[file]
except Exception:
pass
return xbmc.translatePath(databases[file]).decode('utf-8')
def __exit__(self, exc_type, exc_val, exc_tb):
''' Close the connection and cursor.

View file

@ -53,6 +53,7 @@ class Service(xbmc.Monitor):
self.settings['enable_context'] = settings('enableContext.bool')
self.settings['enable_context_transcode'] = settings('enableContextTranscode.bool')
self.settings['kodi_companion'] = settings('kodiCompanion.bool')
self.settings['enable_db_discovery'] = settings('AskDiscoverDatabase.bool')
window('emby_logLevel', value=str(self.settings['log_level']))
window('emby_kodiProfile', value=self.settings['profile'])
settings('platformDetected', client.get_platform())
@ -122,6 +123,7 @@ class Service(xbmc.Monitor):
window('emby.restart', clear=True)
dialog("notification", heading="{emby}", message=_(33193), icon="{emby}", time=1000, sound=False)
reload(objects)
raise Exception('RestartService')
@ -452,6 +454,14 @@ class Service(xbmc.Monitor):
self.settings['enable_context_transcode'] = settings('enableContextTranscode.bool')
LOG.warn("New context transcode setting: %s", self.settings['enable_context_transcode'])
if settings('AskDiscoverDatabase.bool') != self.settings['enable_db_discovery']:
LOG.warn(self.settings['enable_db_discovery'])
self.settings['enable_db_discovery'] = settings('AskDiscoverDatabase.bool')
LOG.warn("Enable database discovery: %s", self.settings['enable_db_discovery'])
if dialog("yesno", heading="{emby}", line1=_(33191)):
window('emby.restart.bool', True)
if settings('useDirectPaths') != self.settings['mode'] and self.library_thread.started:
self.settings['mode'] = settings('useDirectPaths')

View file

@ -116,6 +116,27 @@ class Library(threading.Thread):
LOG.warn("---<[ library ]")
def test_databases(self):
''' Open the databases to test if the file exists.
'''
with Database('video') as kodidb:
with Database('music') as musicdb:
if kodidb.discovered or musicdb.discovered:
if kodidb.path != settings('DiscoveredDatabase'):
LOG.info("Newly discovered database: %s", kodidb.path)
settings('DiscoveredDatabase', kodidb.path)
self.monitor.settings['enable_db_discovery'] = False
settings('AskDiscoverDatabase.bool', False)
return False
else:
settings('DiscoveredDatabase', "")
return True
@stop()
def service(self):
@ -294,8 +315,28 @@ class Library(threading.Thread):
def startup(self):
''' Run at startup. Will check for the server plugin.
''' Run at startup.
Check databases.
Check for the server plugin.
'''
if not self.test_databases():
if settings('AskDiscoverDatabase.bool'):
self.monitor.settings['enable_db_discovery'] = False
settings('AskDiscoverDatabase.bool', False)
result = dialog("yesno", heading="{emby}", line1=_(33189))
settings('DiscoverDatabase.bool', result == 1)
if not result:
LOG.info("Do not discover database again.")
return False
elif not settings('DiscoverDatabase.bool'):
LOG.info("Do not re-discover database again.")
return False
Views().get_views()
Views().get_nodes()

View file

@ -24,6 +24,7 @@
<setting label="30507" id="syncIndicator" type="number" default="99" visible="eq(-1,true)" subsetting="true"/>
<setting label="33185" id="syncDuringPlay" type="bool" default="true" />
<setting label="30536" id="dbSyncScreensaver" type="bool" default="true" />
<setting label="33190" id="AskDiscoverDatabase" type="bool" default="true" />
<setting label="33111" type="lsep" />
<setting label="30511" id="useDirectPaths" type="enum" lvalues="33036|33037" default="1" />