Fix videos not being correctly marked as played

- Hopefully fixes #423
This commit is contained in:
croneter 2018-03-11 20:10:02 +01:00
parent 46adc51cf6
commit 5012ab84c8
2 changed files with 66 additions and 1 deletions

View file

@ -655,6 +655,21 @@ class KodiDBMethods(object):
"""
self.cursor.execute("DELETE FROM bookmark")
def get_playcount(self, file_id):
"""
Returns the playcount for the item file_id or None if not found
"""
query = '''
SELECT playCount FROM files
WHERE idFile = ? LIMIT 1
'''
self.cursor.execute(query, (file_id, ))
try:
answ = self.cursor.fetchone()[0]
except TypeError:
answ = None
return answ
def addPlaystate(self, file_id, resume_seconds, total_seconds, playcount,
dateplayed):
# Delete existing resume point

View file

@ -6,9 +6,11 @@ import copy
from xbmc import Player
import kodidb_functions as kodidb
import plexdb_functions as plexdb
from downloadutils import DownloadUtils as DU
from plexbmchelper.subscribers import LOCKER
import playqueue as PQ
from utils import kodi_time_to_millis, unix_date_to_kodi, unix_timestamp
import variables as v
import state
@ -38,6 +40,11 @@ def playback_cleanup():
DU().downloadUrl(
'{server}/video/:/transcode/universal/stop',
parameters={'session': v.PKC_MACHINE_IDENTIFIER})
if playerid == 1:
# Bookmarks might not be pickup up correctly, so let's do them
# manually. Applies to addon paths, but direct paths might have
# started playback via PMS
_record_playstate(status)
# Reset the player's status
status = copy.deepcopy(state.PLAYSTATE)
# As all playback has halted, reset the players that have been active
@ -45,6 +52,49 @@ def playback_cleanup():
LOG.debug('Finished PKC playback cleanup')
def _record_playstate(status):
if not status['plex_id']:
LOG.debug('No Plex id found to record playstate for status %s', status)
return
with plexdb.Get_Plex_DB() as plex_db:
kodi_db_item = plex_db.getItem_byId(status['plex_id'])
if kodi_db_item is None:
# Item not (yet) in Kodi library
LOG.debug('No playstate update due to Plex id not found: %s', status)
return
time = float(kodi_time_to_millis(status['time'])) / 1000
totaltime = float(kodi_time_to_millis(status['totaltime'])) / 1000
try:
progress = time / totaltime
except ZeroDivisionError:
progress = 0.0
LOG.debug('Playback progress %s (%s of %s seconds)',
progress, time, totaltime)
playcount = status['playcount']
if playcount is None:
LOG.info('playcount not found, looking it up in the Kodi DB')
with kodidb.GetKodiDB('video') as kodi_db:
playcount = kodi_db.get_playcount(kodi_db_item[1])
playcount = 0 if playcount is None else playcount
if time < v.IGNORE_SECONDS_AT_START:
LOG.debug('Ignoring playback less than %s seconds',
v.IGNORE_SECONDS_AT_START)
# Annoying Plex bug - it'll reset an already watched video to unwatched
playcount = 0
time = 0
elif progress >= v.MARK_PLAYED_AT:
LOG.debug('Recording entirely played video since progress > %s',
v.MARK_PLAYED_AT)
playcount += 1
time = 0
with kodidb.GetKodiDB('video') as kodi_db:
kodi_db.addPlaystate(kodi_db_item[1],
time,
totaltime,
playcount,
unix_date_to_kodi(unix_timestamp()))
class PKC_Player(Player):
def __init__(self):
Player.__init__(self)