diff --git a/resources/lib/KodiMonitor.py b/resources/lib/KodiMonitor.py index fd1f1544..fcf4f867 100644 --- a/resources/lib/KodiMonitor.py +++ b/resources/lib/KodiMonitor.py @@ -16,13 +16,18 @@ from DownloadUtils import DownloadUtils from PlaybackUtils import PlaybackUtils -class Kodi_Monitor(xbmc.Monitor): +class Kodi_Monitor( xbmc.Monitor ): WINDOW = xbmcgui.Window(10000) def __init__(self, *args, **kwargs): xbmc.Monitor.__init__(self) + def logMsg(self, msg, lvl = 1): + + className = self.__class__.__name__ + utils.logMsg("%s %s" % ("EMBY", className), msg, int(lvl)) + def onDatabaseUpdated(self, database): pass @@ -97,23 +102,26 @@ class Kodi_Monitor(xbmc.Monitor): WINDOW.setProperty(playurl+"AudioStreamIndex", str(mediaSources[0].get('DefaultAudioStreamIndex'))) if mediaSources[0].get('DefaultSubtitleStreamIndex') != None: WINDOW.setProperty(playurl+"SubtitleStreamIndex", str(mediaSources[0].get('DefaultSubtitleStreamIndex'))) + if method == "VideoLibrary.OnUpdate": + # Triggers 4 times, the following is only for manually marking as watched/unwatched jsondata = json.loads(data) - if jsondata != None: - - playcount = None - playcount = jsondata.get("playcount") - item = jsondata.get("item").get("id") - type = jsondata.get("item").get("type") - prop = WINDOW.getProperty('Played%s%s' % (type,item)) - processWatched = WINDOW.getProperty('played_skipWatched') - - if (playcount != None) and (prop != "true") and (processWatched != "true"): - WINDOW.setProperty("Played%s%s" % (type,item), "true") - utils.logMsg("MB# Sync","Kodi_Monitor--> VideoLibrary.OnUpdate : " + str(data),2) + + try: + playcount = jsondata['playcount'] + item = jsondata['item']['id'] + type = jsondata['item']['type'] + prop = utils.window('Played%s%s' % (type, item)) + except: + self.logMsg("Could not process VideoLibrary.OnUpdate data.", 1) + else: + self.logMsg("VideoLibrary.OnUpdate: %s" % data, 2) + if prop != "true": + # Set property to prevent the multi triggering + utils.window('Played%s%s' % (type, item), "true") WriteKodiVideoDB().updatePlayCountFromKodi(item, type, playcount) - - self.clearProperty(type,item) + + self.clearProperty(type, item) if method == "System.OnWake": xbmc.sleep(10000) #Allow network to wake up @@ -145,13 +153,11 @@ class Kodi_Monitor(xbmc.Monitor): xbmc.log('Deleting via URL: ' + url) DownloadUtils().downloadUrl(url, type="DELETE") - def clearProperty(self,type,id): + def clearProperty(self, type, id): # The sleep is necessary since VideoLibrary.OnUpdate - # triggers 3 times in a row. + # triggers 4 times in a row. xbmc.sleep(100) - self.WINDOW.clearProperty("Played%s%s" % (type,id)) - self.WINDOW.clearProperty('played_skipWatched') + utils.window('Played%s%s' % (type,id), clear=True) - #clear the widget cache - self.WINDOW.setProperty('clearwidgetcache','clear') - \ No newline at end of file + # Clear the widget cache + utils.window('clearwidgetcache', value="clear") \ No newline at end of file diff --git a/resources/lib/Player.py b/resources/lib/Player.py index 2ce90c09..20926bfa 100644 --- a/resources/lib/Player.py +++ b/resources/lib/Player.py @@ -378,8 +378,8 @@ class Player( xbmc.Player ): data = self.played_information.get(item) if data: - self.logMsg("Item path: %s" % item, 1) - self.logMsg("Item data: %s" % str(data), 1) + self.logMsg("Item path: %s" % item, 2) + self.logMsg("Item data: %s" % str(data), 2) runtime = data.get('runtime') currentPosition = data.get('currentPosition') @@ -390,14 +390,12 @@ class Player( xbmc.Player ): playMethod = data.get('playmethod') if currentPosition and runtime: - self.logMsg("RuntimeTicks: %s" % runtime, 1) percentComplete = (currentPosition * 10000000) / int(runtime) markPlayedAt = float(utils.settings('markPlayed')) / 100 - self.logMsg("Percent complete: %s Mark played at: %s" % (percentComplete, markPlayedAt)) - if percentComplete < markPlayedAt: - # Do not mark as watched for Kodi Monitor - utils.window('played_skipWatched', value="true") + self.logMsg("Percent complete: %s Mark played at: %s" % (percentComplete, markPlayedAt), 1) + # Prevent manually mark as watched in Kodi monitor > WriteKodiVideoDB().UpdatePlaycountFromKodi() + utils.window('SkipWatched%s' % itemId, "true") self.stopPlayback(data) offerDelete = False @@ -430,8 +428,8 @@ class Player( xbmc.Player ): self.logMsg("stopPlayback called", 2) - itemId = data.get('item_id') - currentPosition = data.get('currentPosition') + itemId = data['item_id'] + currentPosition = data['currentPosition'] positionTicks = int(currentPosition * 10000000) url = "{server}/mediabrowser/Sessions/Playing/Stopped" diff --git a/resources/lib/UserClient.py b/resources/lib/UserClient.py index 7006abcd..4ea8cd57 100644 --- a/resources/lib/UserClient.py +++ b/resources/lib/UserClient.py @@ -184,6 +184,12 @@ class UserClient(threading.Thread): # Set user image for skin display self.WINDOW.setProperty("EmbyUserImage",API().getUserArtwork(result,"Primary")) + # Load the resume point from Emby and set as setting + url = "{server}/mediabrowser/System/Configuration?format=json" + result = self.doUtils.downloadUrl(url) + + utils.settings('markPlayed', value=str(result['MaxResumePct'])) + return True def getPublicUsers(self): diff --git a/resources/lib/WriteKodiVideoDB.py b/resources/lib/WriteKodiVideoDB.py index ca63433d..d6a10ee6 100644 --- a/resources/lib/WriteKodiVideoDB.py +++ b/resources/lib/WriteKodiVideoDB.py @@ -57,16 +57,21 @@ class WriteKodiVideoDB(): # Could not find the Emby Id self.logMsg("Emby Id not found.", 2) else: - # Found the Emby Id, let Emby server know of new playcount - watchedurl = "{server}/mediabrowser/Users/{UserId}/PlayedItems/%s" % emby_id - if playcount != 0: - doUtils.downloadUrl(watchedurl, type = "POST") - self.logMsg("Mark as watched for Id: %s, playcount: %s." % (emby_id, playcount), 1) + # Stop from manually marking as watched unwatched, with actual playback. + # Window property is set in Player.py + if utils.window('SkipWatched%s' % emby_id) == "true": + utils.window('SkipWatched%s' % emby_id, clear=True) else: - doUtils.downloadUrl(watchedurl, type = "DELETE") - self.logMsg("Mark as unwatched for Id: %s, playcount: %s." % (emby_id, playcount), 1) - # Erase any resume point associated - self.setKodiResumePoint(id, 0, 0, cursor, playcount) + # Found the Emby Id, let Emby server know of new playcount + watchedurl = "{server}/mediabrowser/Users/{UserId}/PlayedItems/%s" % emby_id + if playcount != 0: + doUtils.downloadUrl(watchedurl, type = "POST") + self.logMsg("Mark as watched for Id: %s, playcount: %s." % (emby_id, playcount), 1) + else: + doUtils.downloadUrl(watchedurl, type = "DELETE") + self.logMsg("Mark as unwatched for Id: %s, playcount: %s." % (emby_id, playcount), 1) + # Erase any resume point associated + self.setKodiResumePoint(id, 0, 0, cursor, playcount) finally: cursor.close diff --git a/resources/settings.xml b/resources/settings.xml index 1fb1ba6d..c496e3e4 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -36,13 +36,13 @@ - +