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 a7b615ab..c810cb8f 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): @@ -107,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": @@ -423,6 +400,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) @@ -470,6 +449,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') @@ -639,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