Merge pull request #1008 from croneter/fix-viewcount
Fix PKC increasing the Plex watch count by 2 instead of 1
This commit is contained in:
commit
8a3e580975
2 changed files with 43 additions and 25 deletions
|
@ -53,6 +53,9 @@ class PlayState(object):
|
||||||
}
|
}
|
||||||
self.played_info = {}
|
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
|
# Set by SpecialMonitor - did user choose to resume playback or start from the
|
||||||
# beginning?
|
# beginning?
|
||||||
# Set to None if resume dialog has not been shown
|
# Set to None if resume dialog has not been shown
|
||||||
|
|
|
@ -24,8 +24,7 @@ from . import backgroundthread, app, variables as v
|
||||||
LOG = getLogger('PLEX.kodimonitor')
|
LOG = getLogger('PLEX.kodimonitor')
|
||||||
|
|
||||||
# "Start from beginning", "Play from beginning"
|
# "Start from beginning", "Play from beginning"
|
||||||
STRINGS = (utils.try_encode(utils.lang(12021)),
|
STRINGS = (utils.lang(12021).encode('utf-8'), utils.lang(12023).encode('utf-8'))
|
||||||
utils.try_encode(utils.lang(12023)))
|
|
||||||
|
|
||||||
|
|
||||||
class KodiMonitor(xbmc.Monitor):
|
class KodiMonitor(xbmc.Monitor):
|
||||||
|
@ -107,29 +106,7 @@ class KodiMonitor(xbmc.Monitor):
|
||||||
with app.APP.lock_playqueues:
|
with app.APP.lock_playqueues:
|
||||||
self._playlist_onclear(data)
|
self._playlist_onclear(data)
|
||||||
elif method == "VideoLibrary.OnUpdate":
|
elif method == "VideoLibrary.OnUpdate":
|
||||||
# Manually marking as watched/unwatched
|
_videolibrary_onupdate(data)
|
||||||
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')
|
|
||||||
elif method == "VideoLibrary.OnRemove":
|
elif method == "VideoLibrary.OnRemove":
|
||||||
pass
|
pass
|
||||||
elif method == "System.OnSleep":
|
elif method == "System.OnSleep":
|
||||||
|
@ -423,6 +400,8 @@ class KodiMonitor(xbmc.Monitor):
|
||||||
container_key = '/playQueues/%s' % playqueue.id
|
container_key = '/playQueues/%s' % playqueue.id
|
||||||
else:
|
else:
|
||||||
container_key = '/library/metadata/%s' % plex_id
|
container_key = '/library/metadata/%s' % plex_id
|
||||||
|
# Remember the currently playing item
|
||||||
|
app.PLAYSTATE.item = item
|
||||||
# Remember that this player has been active
|
# Remember that this player has been active
|
||||||
app.PLAYSTATE.active_players.add(playerid)
|
app.PLAYSTATE.active_players.add(playerid)
|
||||||
status.update(info)
|
status.update(info)
|
||||||
|
@ -470,6 +449,7 @@ def _playback_cleanup(ended=False):
|
||||||
app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template)
|
app.PLAYSTATE.player_states[playerid] = copy.deepcopy(app.PLAYSTATE.template)
|
||||||
# As all playback has halted, reset the players that have been active
|
# As all playback has halted, reset the players that have been active
|
||||||
app.PLAYSTATE.active_players = set()
|
app.PLAYSTATE.active_players = set()
|
||||||
|
app.PLAYSTATE.item = None
|
||||||
LOG.info('Finished PKC playback cleanup')
|
LOG.info('Finished PKC playback cleanup')
|
||||||
|
|
||||||
|
|
||||||
|
@ -639,6 +619,41 @@ def _notify_upnext(item):
|
||||||
xbmc.executebuiltin('NotifyAll(%s, %s, %s)' % (sender, method, data))
|
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):
|
class ContextMonitor(backgroundthread.KillableThread):
|
||||||
"""
|
"""
|
||||||
Detect the resume dialog for widgets. Could also be used to detect
|
Detect the resume dialog for widgets. Could also be used to detect
|
||||||
|
|
Loading…
Reference in a new issue