From 7f0d03753127fc139cb3c040699fd1e5b7443fcf Mon Sep 17 00:00:00 2001 From: agentxan Date: Sat, 11 Mar 2017 17:13:50 -0600 Subject: [PATCH 1/2] Update kodimonitor.py --- resources/lib/kodimonitor.py | 98 +++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index edde1457..0e719e83 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -96,7 +96,8 @@ class KodiMonitor(xbmc.Monitor): item_type = item['type'] except (KeyError, TypeError): log.info("Item is invalid for playstate update") - else: + (kodi_id, item_type) = self._get_kodi_id_from_currentfile() + if kodi_id: if ((settings('useDirectPaths') == "1" and not item_type == "song") or (item_type == "song" and settings('enableMusic') == "true")): # Set up properties for player @@ -180,3 +181,98 @@ class KodiMonitor(xbmc.Monitor): log.info("Could not retrieve item Id") return item_id + + @classmethod + def _get_kodi_id_from_currentfile(cls): + currentfile = None + count = 0 + while not currentfile and count < 2: + try: + currentfile = xbmc.Player().getPlayingFile() + try: + filename = currentfile.rsplit('/', 1)[1] + path = currentfile.rsplit('/', 1)[0] + '/' + except IndexError: + filename = currentfile.rsplit('\\', 1)[1] + path = currentfile.rsplit('\\', 1)[0] + '\\' + log.info('Trying to figure out kodi_id from filename: %s ' + 'and path: %s' % (filename, path)) + except RuntimeError: + count += 1 + xbmc.sleep(200) + + with DatabaseConn('video') as cursor: + query = ' '.join(( + "SELECT idFile, idPath", + "FROM files", + "WHERE strFilename = ?" + )) + cursor.execute(query, (filename,)) + files = cursor.fetchall() + if len(files) == 0: + log.info('Did not find any file, abort') + return + query = ' '.join(( + "SELECT strPath", + "FROM path", + "WHERE idPath = ?" + )) + # result will contain a list of all idFile with matching filename and + # matching path + result = [] + for file in files: + # Use idPath to get path as a string + cursor.execute(query, (file[1],)) + try: + strPath = cursor.fetchone()[0] + except TypeError: + # idPath not found; skip + continue + # For whatever reason, double might have become triple + strPath = strPath.replace('///', '//') + strPath = strPath.replace('\\\\\\', '\\\\') + if strPath == path: + result.append(file[0]) + if len(result) == 0: + log.info('Did not find matching paths, abort') + return + log.info('Result: %s' % result) + # Kodi seems to make ONE temporary entry; we only want the earlier, + # permanent one + if len(result) > 2: + log.info('We found too many items with matching filenames and ' + ' paths, aborting') + return + idFile = result[0] + log.info('idFile: %s' % idFile) + + # Try movies first + query = ' '.join(( + "SELECT idMovie", + "FROM movie", + "WHERE idFile = ?" + )) + cursor.execute(query, (idFile,)) + try: + kodi_id = cursor.fetchone()[0] + item_type = 'movie' + log.info('Found kodi_id = %s item_type = %s' % (kodi_id, item_type)) + except TypeError: + # Try tv shows next + query = ' '.join(( + "SELECT idEpisode", + "FROM episode", + "WHERE idFile = ?" + )) + cursor.execute(query, (idFile,)) + try: + kodi_id = cursor.fetchone()[0] + item_type = 'episode' + log.info('Found kodi_id = %s item_type = %s' % (kodi_id, item_type)) + except TypeError: + log.info('Unexpectantly did not find a match!') + return + + return kodi_id, item_type + + From 9fe0e616519832442e96b44bcd88038d4e8f8ec4 Mon Sep 17 00:00:00 2001 From: agentxan Date: Mon, 27 Mar 2017 11:31:26 -0500 Subject: [PATCH 2/2] Update kodimonitor.py --- resources/lib/kodimonitor.py | 112 ++++------------------------------- 1 file changed, 12 insertions(+), 100 deletions(-) diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index 0e719e83..9c1d6a5f 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -18,6 +18,7 @@ from database import DatabaseConn ################################################################################################# log = logging.getLogger("EMBY."+__name__) +KODI = int(xbmc.getInfoLabel('System.BuildVersion')[:2]) ################################################################################################# @@ -91,13 +92,19 @@ class KodiMonitor(xbmc.Monitor): def _on_play_(self, data): # Set up report progress for emby playback try: - item = data['item'] - kodi_id = item['id'] - item_type = item['type'] + if KODI >= 17: + item = xbmc.Player().getVideoInfoTag() + kodi_id = item.getDbId() + item_type = item.getMediaType() + log.info("kodi_id: %s item_type: %s", kodi_id, item_type) + else: + item = data['item'] + kodi_id = item['id'] + item_type = item['type'] + log.info("kodi_id: %s item_type: %s", kodi_id, item_type) except (KeyError, TypeError): log.info("Item is invalid for playstate update") - (kodi_id, item_type) = self._get_kodi_id_from_currentfile() - if kodi_id: + else: if ((settings('useDirectPaths') == "1" and not item_type == "song") or (item_type == "song" and settings('enableMusic') == "true")): # Set up properties for player @@ -181,98 +188,3 @@ class KodiMonitor(xbmc.Monitor): log.info("Could not retrieve item Id") return item_id - - @classmethod - def _get_kodi_id_from_currentfile(cls): - currentfile = None - count = 0 - while not currentfile and count < 2: - try: - currentfile = xbmc.Player().getPlayingFile() - try: - filename = currentfile.rsplit('/', 1)[1] - path = currentfile.rsplit('/', 1)[0] + '/' - except IndexError: - filename = currentfile.rsplit('\\', 1)[1] - path = currentfile.rsplit('\\', 1)[0] + '\\' - log.info('Trying to figure out kodi_id from filename: %s ' - 'and path: %s' % (filename, path)) - except RuntimeError: - count += 1 - xbmc.sleep(200) - - with DatabaseConn('video') as cursor: - query = ' '.join(( - "SELECT idFile, idPath", - "FROM files", - "WHERE strFilename = ?" - )) - cursor.execute(query, (filename,)) - files = cursor.fetchall() - if len(files) == 0: - log.info('Did not find any file, abort') - return - query = ' '.join(( - "SELECT strPath", - "FROM path", - "WHERE idPath = ?" - )) - # result will contain a list of all idFile with matching filename and - # matching path - result = [] - for file in files: - # Use idPath to get path as a string - cursor.execute(query, (file[1],)) - try: - strPath = cursor.fetchone()[0] - except TypeError: - # idPath not found; skip - continue - # For whatever reason, double might have become triple - strPath = strPath.replace('///', '//') - strPath = strPath.replace('\\\\\\', '\\\\') - if strPath == path: - result.append(file[0]) - if len(result) == 0: - log.info('Did not find matching paths, abort') - return - log.info('Result: %s' % result) - # Kodi seems to make ONE temporary entry; we only want the earlier, - # permanent one - if len(result) > 2: - log.info('We found too many items with matching filenames and ' - ' paths, aborting') - return - idFile = result[0] - log.info('idFile: %s' % idFile) - - # Try movies first - query = ' '.join(( - "SELECT idMovie", - "FROM movie", - "WHERE idFile = ?" - )) - cursor.execute(query, (idFile,)) - try: - kodi_id = cursor.fetchone()[0] - item_type = 'movie' - log.info('Found kodi_id = %s item_type = %s' % (kodi_id, item_type)) - except TypeError: - # Try tv shows next - query = ' '.join(( - "SELECT idEpisode", - "FROM episode", - "WHERE idFile = ?" - )) - cursor.execute(query, (idFile,)) - try: - kodi_id = cursor.fetchone()[0] - item_type = 'episode' - log.info('Found kodi_id = %s item_type = %s' % (kodi_id, item_type)) - except TypeError: - log.info('Unexpectantly did not find a match!') - return - - return kodi_id, item_type - -