2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
###############################################################################
|
2017-12-10 00:35:08 +11:00
|
|
|
from logging import getLogger
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-01-22 04:31:49 +11:00
|
|
|
from xbmc import Player
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-01-22 04:31:49 +11:00
|
|
|
from utils import window, DateToKodi, getUnixTimestamp, kodi_time_to_millis
|
|
|
|
from downloadutils import DownloadUtils as DU
|
2017-01-05 06:57:16 +11:00
|
|
|
import plexdb_functions as plexdb
|
2016-05-17 02:19:05 +10:00
|
|
|
import kodidb_functions as kodidb
|
2018-01-22 04:31:49 +11:00
|
|
|
from plexbmchelper.subscribers import LOCKER
|
2017-01-25 02:04:42 +11:00
|
|
|
import variables as v
|
2017-05-18 04:22:16 +10:00
|
|
|
import state
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-12-10 00:35:08 +11:00
|
|
|
LOG = getLogger("PLEX." + __name__)
|
2016-09-02 03:41:55 +10:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-12-10 00:35:08 +11:00
|
|
|
class PKC_Player(Player):
|
2015-12-25 07:07:00 +11:00
|
|
|
def __init__(self):
|
2017-12-10 00:35:08 +11:00
|
|
|
Player.__init__(self)
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("Started playback monitor.")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-17 19:13:37 +11:00
|
|
|
def onPlayBackStarted(self):
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
2016-09-02 03:41:55 +10:00
|
|
|
Will be called when xbmc starts playing a file.
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
2018-01-22 04:31:49 +11:00
|
|
|
pass
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackPaused(self):
|
2018-01-22 04:31:49 +11:00
|
|
|
"""
|
|
|
|
Will be called when playback is paused
|
|
|
|
"""
|
|
|
|
pass
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackResumed(self):
|
2018-01-22 04:31:49 +11:00
|
|
|
"""
|
|
|
|
Will be called when playback is resumed
|
|
|
|
"""
|
|
|
|
pass
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackSeek(self, time, seekOffset):
|
2018-01-22 04:31:49 +11:00
|
|
|
"""
|
|
|
|
Will be called when user seeks to a certain time during playback
|
|
|
|
"""
|
|
|
|
pass
|
2016-06-01 03:02:11 +10:00
|
|
|
|
2016-02-21 10:21:39 +11:00
|
|
|
def onPlayBackStopped(self):
|
2018-01-22 04:31:49 +11:00
|
|
|
"""
|
|
|
|
Will be called when playback is stopped by the user
|
|
|
|
"""
|
2017-12-09 05:43:06 +11:00
|
|
|
LOG.info("ONPLAYBACK_STOPPED")
|
2018-01-22 04:31:49 +11:00
|
|
|
self.cleanup_playback()
|
2016-03-17 04:01:07 +11:00
|
|
|
|
2018-01-22 04:31:49 +11:00
|
|
|
def onPlayBackEnded(self):
|
|
|
|
"""
|
|
|
|
Will be called when playback ends due to the media file being finished
|
|
|
|
"""
|
|
|
|
LOG.info("ONPLAYBACK_ENDED")
|
|
|
|
self.cleanup_playback()
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-01-22 04:31:49 +11:00
|
|
|
@LOCKER.lockthis
|
|
|
|
def cleanup_playback(self):
|
|
|
|
"""
|
|
|
|
PKC cleanup after playback ends/is stopped
|
|
|
|
"""
|
|
|
|
# We might have saved a transient token from a user flinging media via
|
|
|
|
# Companion (if we could not use the playqueue to store the token)
|
|
|
|
state.PLEX_TRANSIENT_TOKEN = None
|
2016-11-07 01:37:22 +11:00
|
|
|
for item in ('plex_currently_playing_itemid',
|
|
|
|
'plex_customplaylist',
|
|
|
|
'plex_customplaylist.seektime',
|
|
|
|
'plex_forcetranscode'):
|
|
|
|
window(item, clear=True)
|
2018-01-22 04:31:49 +11:00
|
|
|
for playerid in state.ACTIVE_PLAYERS:
|
|
|
|
status = state.PLAYER_STATES[playerid]
|
|
|
|
# Check whether we need to mark an item as completely watched
|
|
|
|
if not status['kodi_id'] or not status['plex_id']:
|
|
|
|
LOG.info('No PKC info safed for the element just played by Kodi'
|
|
|
|
' player %s', playerid)
|
2017-05-25 21:58:15 +10:00
|
|
|
continue
|
2018-01-22 04:31:49 +11:00
|
|
|
# Stop transcoding
|
|
|
|
if status['playmethod'] == 'Transcode':
|
|
|
|
LOG.info('Tell the PMS to stop transcoding')
|
|
|
|
DU().downloadUrl(
|
|
|
|
'{server}/video/:/transcode/universal/stop',
|
|
|
|
parameters={'session': v.PKC_MACHINE_IDENTIFIER})
|
|
|
|
if status['plex_type'] == v.PLEX_TYPE_SONG:
|
|
|
|
LOG.debug('Song has been played, not cleaning up playstate')
|
|
|
|
continue
|
|
|
|
resume = kodi_time_to_millis(status['time'])
|
|
|
|
runtime = kodi_time_to_millis(status['totaltime'])
|
|
|
|
LOG.info('Item playback progress %s out of %s', resume, runtime)
|
|
|
|
if not resume or not runtime:
|
2017-05-25 22:00:30 +10:00
|
|
|
continue
|
2018-01-22 04:31:49 +11:00
|
|
|
complete = float(resume) / float(runtime)
|
|
|
|
LOG.info("Percent complete: %s. Mark played at: %s",
|
|
|
|
complete, v.MARK_PLAYED_AT)
|
|
|
|
if complete >= v.MARK_PLAYED_AT:
|
2017-05-25 22:00:30 +10:00
|
|
|
# Tell Kodi that we've finished watching (Plex knows)
|
2018-01-22 04:31:49 +11:00
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
|
|
|
plex_dbitem = plex_db.getItem_byId(status['plex_id'])
|
|
|
|
file_id = plex_dbitem[1] if plex_dbitem else None
|
|
|
|
if file_id is None:
|
|
|
|
LOG.error('No file_id found for %s', status)
|
|
|
|
continue
|
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
|
|
|
kodi_db.addPlaystate(
|
|
|
|
file_id,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
status['playcount'] + 1,
|
|
|
|
DateToKodi(getUnixTimestamp()))
|
|
|
|
LOG.info('Marked plex element %s as completely watched',
|
|
|
|
status['plex_id'])
|
|
|
|
# As all playback has halted, reset the players that have been active
|
|
|
|
state.ACTIVE_PLAYERS = []
|
|
|
|
for playerid in state.PLAYER_STATES:
|
|
|
|
state.PLAYER_STATES[playerid] = dict(state.PLAYSTATE)
|
|
|
|
LOG.info('Finished PKC playback cleanup')
|