From ae0e121b13e2643d6df951a9a38016c74084fd5a Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 5 Oct 2019 10:46:33 +0200 Subject: [PATCH 1/3] Optimize code --- resources/lib/kodimonitor.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index a7b615ab..759ef7e3 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -24,8 +24,7 @@ from . import backgroundthread, app, variables as v LOG = getLogger('PLEX.kodimonitor') # "Start from beginning", "Play from beginning" -STRINGS = (utils.try_encode(utils.lang(12021)), - utils.try_encode(utils.lang(12023))) +STRINGS = (utils.lang(12021).encode('utf-8'), utils.lang(12023).encode('utf-8')) class KodiMonitor(xbmc.Monitor): From d75e2a0109b1aeb411fb3724a9b3618af915f321 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 5 Oct 2019 11:29:08 +0200 Subject: [PATCH 2/3] Remember currently playing item directly --- resources/lib/app/playstate.py | 3 +++ resources/lib/kodimonitor.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/resources/lib/app/playstate.py b/resources/lib/app/playstate.py index 0266c65d..d5235ff4 100644 --- a/resources/lib/app/playstate.py +++ b/resources/lib/app/playstate.py @@ -53,6 +53,9 @@ class PlayState(object): } self.played_info = {} + # Currently playing PKC item, a PlaylistItem() + self.item = None + # Set by SpecialMonitor - did user choose to resume playback or start from the # beginning? # Set to None if resume dialog has not been shown diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index 759ef7e3..8afde9be 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -422,6 +422,8 @@ class KodiMonitor(xbmc.Monitor): container_key = '/playQueues/%s' % playqueue.id else: container_key = '/library/metadata/%s' % plex_id + # Remember the currently playing item + app.PLAYSTATE.item = item # Remember that this player has been active app.PLAYSTATE.active_players.add(playerid) status.update(info) @@ -469,6 +471,7 @@ def _playback_cleanup(ended=False): app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template) # As all playback has halted, reset the players that have been active app.PLAYSTATE.active_players = set() + app.PLAYSTATE.item = None LOG.info('Finished PKC playback cleanup') From 7ea9222e475eafc42ce253fe0a61ee8fd6b1e062 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 5 Oct 2019 10:50:55 +0200 Subject: [PATCH 3/3] Fix PKC increasing the Plex watch-count by 2 instead of 1 --- resources/lib/kodimonitor.py | 59 ++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/resources/lib/kodimonitor.py b/resources/lib/kodimonitor.py index 8afde9be..c810cb8f 100644 --- a/resources/lib/kodimonitor.py +++ b/resources/lib/kodimonitor.py @@ -106,29 +106,7 @@ class KodiMonitor(xbmc.Monitor): with app.APP.lock_playqueues: self._playlist_onclear(data) elif method == "VideoLibrary.OnUpdate": - # Manually marking as watched/unwatched - playcount = data.get('playcount') - item = data.get('item') - if playcount is None or item is None: - return - try: - kodi_id = item['id'] - kodi_type = item['type'] - except (KeyError, TypeError): - LOG.info("Item is invalid for playstate update.") - return - # Send notification to the server. - with PlexDB() as plexdb: - db_item = plexdb.item_by_kodi_id(kodi_id, kodi_type) - if not db_item: - LOG.error("Could not find plex_id in plex database for a " - "video library update") - else: - # notify the server - if playcount > 0: - PF.scrobble(db_item['plex_id'], 'watched') - else: - PF.scrobble(db_item['plex_id'], 'unwatched') + _videolibrary_onupdate(data) elif method == "VideoLibrary.OnRemove": pass elif method == "System.OnSleep": @@ -641,6 +619,41 @@ def _notify_upnext(item): xbmc.executebuiltin('NotifyAll(%s, %s, %s)' % (sender, method, data)) +def _videolibrary_onupdate(data): + """ + A specific Kodi library item has been updated. This seems to happen if the + user marks an item as watched/unwatched or if playback of the item just + stopped + """ + playcount = data.get('playcount') + item = data.get('item') + if playcount is None or item is None: + return + try: + kodi_id = item['id'] + kodi_type = item['type'] + except (KeyError, TypeError): + LOG.info("Item is invalid for playstate update.") + return + if app.PLAYSTATE.item and kodi_id == app.PLAYSTATE.item.kodi_id and \ + kodi_type == app.PLAYSTATE.item.kodi_type: + # Kodi updates an item immediately after playback. Hence we do NOT + # increase or decrease the viewcount + return + # Send notification to the server. + with PlexDB(lock=False) as plexdb: + db_item = plexdb.item_by_kodi_id(kodi_id, kodi_type) + if not db_item: + LOG.error("Could not find plex_id in plex database for a " + "video library update") + return + # notify the server + if playcount > 0: + PF.scrobble(db_item['plex_id'], 'watched') + else: + PF.scrobble(db_item['plex_id'], 'unwatched') + + class ContextMonitor(backgroundthread.KillableThread): """ Detect the resume dialog for widgets. Could also be used to detect