2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
PKC Kodi Monitoring implementation
|
|
|
|
"""
|
2017-09-03 20:46:41 +10:00
|
|
|
from logging import getLogger
|
2017-01-09 01:03:41 +11:00
|
|
|
from json import loads
|
2018-01-22 21:20:37 +11:00
|
|
|
from threading import Thread
|
2018-03-12 04:54:05 +11:00
|
|
|
import copy
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-04-09 16:13:54 +10:00
|
|
|
import xbmc
|
2018-01-22 21:20:37 +11:00
|
|
|
from xbmcgui import Window
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-01-05 06:57:16 +11:00
|
|
|
import plexdb_functions as plexdb
|
2018-04-09 16:13:54 +10:00
|
|
|
import kodidb_functions as kodidb
|
|
|
|
from utils import window, settings, plex_command, thread_methods, try_encode, \
|
|
|
|
kodi_time_to_millis, unix_date_to_kodi, unix_timestamp
|
2017-01-25 02:04:42 +11:00
|
|
|
from PlexFunctions import scrobble
|
2018-04-09 16:17:47 +10:00
|
|
|
from downloadutils import DownloadUtils as DU
|
2017-12-14 06:14:27 +11:00
|
|
|
from kodidb_functions import kodiid_from_filename
|
2017-12-21 19:28:06 +11:00
|
|
|
from plexbmchelper.subscribers import LOCKER
|
2018-01-26 03:15:38 +11:00
|
|
|
from playback import playback_triage
|
2018-02-10 03:48:25 +11:00
|
|
|
from initialsetup import set_replace_paths
|
2018-01-11 06:14:05 +11:00
|
|
|
import playqueue as PQ
|
2017-12-11 05:01:22 +11:00
|
|
|
import json_rpc as js
|
2017-12-21 19:28:06 +11:00
|
|
|
import playlist_func as PL
|
2017-05-17 18:09:57 +10:00
|
|
|
import state
|
2018-04-09 16:13:54 +10:00
|
|
|
import variables as v
|
2016-01-13 00:14:49 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG = getLogger("PLEX." + __name__)
|
2016-08-31 00:57:47 +10:00
|
|
|
|
2017-08-21 15:42:11 +10:00
|
|
|
# settings: window-variable
|
|
|
|
WINDOW_SETTINGS = {
|
|
|
|
'plex_restricteduser': 'plex_restricteduser',
|
2018-02-10 03:48:25 +11:00
|
|
|
'force_transcode_pix': 'plex_force_transcode_pix'
|
2017-08-21 15:42:11 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
# settings: state-variable (state.py)
|
2017-09-03 21:23:18 +10:00
|
|
|
# Need to use getattr and setattr!
|
2017-08-21 15:42:11 +10:00
|
|
|
STATE_SETTINGS = {
|
2017-09-03 21:23:18 +10:00
|
|
|
'dbSyncIndicator': 'SYNC_DIALOG',
|
|
|
|
'remapSMB': 'REMAP_PATH',
|
|
|
|
'remapSMBmovieOrg': 'remapSMBmovieOrg',
|
|
|
|
'remapSMBmovieNew': 'remapSMBmovieNew',
|
|
|
|
'remapSMBtvOrg': 'remapSMBtvOrg',
|
|
|
|
'remapSMBtvNew': 'remapSMBtvNew',
|
|
|
|
'remapSMBmusicOrg': 'remapSMBmusicOrg',
|
|
|
|
'remapSMBmusicNew': 'remapSMBmusicNew',
|
|
|
|
'remapSMBphotoOrg': 'remapSMBphotoOrg',
|
|
|
|
'remapSMBphotoNew': 'remapSMBphotoNew',
|
|
|
|
'enableMusic': 'ENABLE_MUSIC',
|
2018-03-23 03:27:57 +11:00
|
|
|
'forceReloadSkinOnPlaybackStop': 'FORCE_RELOAD_SKIN',
|
2018-04-29 22:12:39 +10:00
|
|
|
'fetch_pms_item_number': 'FETCH_PMS_ITEM_NUMBER',
|
2018-05-03 16:20:55 +10:00
|
|
|
'imageSyncNotifications': 'IMAGE_SYNC_NOTIFICATIONS',
|
|
|
|
'enablePlaylistSync': 'SYNC_PLAYLISTS'
|
2017-08-21 15:42:11 +10:00
|
|
|
}
|
2018-01-22 04:31:49 +11:00
|
|
|
|
2016-08-31 00:57:47 +10:00
|
|
|
###############################################################################
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2018-04-09 16:13:54 +10:00
|
|
|
class KodiMonitor(xbmc.Monitor):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
PKC implementation of the Kodi Monitor class. Invoke only once.
|
|
|
|
"""
|
2018-01-07 01:19:12 +11:00
|
|
|
def __init__(self):
|
2018-04-09 16:13:54 +10:00
|
|
|
self.xbmcplayer = xbmc.Player()
|
2018-03-29 16:33:07 +11:00
|
|
|
self._already_slept = False
|
2018-04-09 16:13:54 +10:00
|
|
|
xbmc.Monitor.__init__(self)
|
2018-01-22 04:31:49 +11:00
|
|
|
for playerid in state.PLAYER_STATES:
|
2018-03-12 04:54:05 +11:00
|
|
|
state.PLAYER_STATES[playerid] = copy.deepcopy(state.PLAYSTATE)
|
|
|
|
state.OLD_PLAYER_STATES[playerid] = copy.deepcopy(state.PLAYSTATE)
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.info("Kodi monitor started.")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def onScanStarted(self, library):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
Will be called when Kodi starts scanning the library
|
|
|
|
"""
|
2018-02-04 22:06:39 +11:00
|
|
|
LOG.debug("Kodi library scan %s running.", library)
|
2016-02-20 06:03:06 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def onScanFinished(self, library):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
Will be called when Kodi finished scanning the library
|
|
|
|
"""
|
2018-02-04 22:06:39 +11:00
|
|
|
LOG.debug("Kodi library scan %s finished.", library)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-01-09 21:53:04 +11:00
|
|
|
def onSettingsChanged(self):
|
2016-10-23 02:15:10 +11:00
|
|
|
"""
|
|
|
|
Monitor the PKC settings for changes made by the user
|
|
|
|
"""
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.debug('PKC settings change detected')
|
2017-09-03 21:28:40 +10:00
|
|
|
changed = False
|
2016-12-21 02:13:19 +11:00
|
|
|
# Reset the window variables from the settings variables
|
2017-08-21 15:42:11 +10:00
|
|
|
for settings_value, window_value in WINDOW_SETTINGS.iteritems():
|
2016-10-24 02:10:34 +11:00
|
|
|
if window(window_value) != settings(settings_value):
|
2017-09-03 21:28:40 +10:00
|
|
|
changed = True
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.debug('PKC window settings changed: %s is now %s',
|
|
|
|
settings_value, settings(settings_value))
|
2016-10-24 02:10:34 +11:00
|
|
|
window(window_value, value=settings(settings_value))
|
2017-08-21 15:42:11 +10:00
|
|
|
# Reset the state variables in state.py
|
2017-09-03 21:23:18 +10:00
|
|
|
for settings_value, state_name in STATE_SETTINGS.iteritems():
|
2017-08-21 15:42:11 +10:00
|
|
|
new = settings(settings_value)
|
|
|
|
if new == 'true':
|
|
|
|
new = True
|
|
|
|
elif new == 'false':
|
|
|
|
new = False
|
2017-09-03 21:23:18 +10:00
|
|
|
if getattr(state, state_name) != new:
|
2017-09-03 21:28:40 +10:00
|
|
|
changed = True
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.debug('PKC state settings %s changed from %s to %s',
|
|
|
|
settings_value, getattr(state, state_name), new)
|
2017-09-03 21:23:18 +10:00
|
|
|
setattr(state, state_name, new)
|
2018-02-10 03:48:25 +11:00
|
|
|
if state_name == 'FETCH_PMS_ITEM_NUMBER':
|
|
|
|
LOG.info('Requesting playlist/nodes refresh')
|
|
|
|
plex_command('RUN_LIB_SCAN', 'views')
|
2017-08-22 16:16:21 +10:00
|
|
|
# Special cases, overwrite all internal settings
|
2018-02-01 17:44:12 +11:00
|
|
|
set_replace_paths()
|
2018-03-10 22:58:11 +11:00
|
|
|
state.BACKGROUND_SYNC_DISABLED = settings(
|
|
|
|
'enableBackgroundSync') == 'false'
|
2017-12-15 03:39:50 +11:00
|
|
|
state.FULL_SYNC_INTERVALL = int(settings('fullSyncInterval')) * 60
|
2017-08-22 16:16:21 +10:00
|
|
|
state.BACKGROUNDSYNC_SAFTYMARGIN = int(
|
|
|
|
settings('backgroundsync_saftyMargin'))
|
|
|
|
state.SYNC_THREAD_NUMBER = int(settings('syncThreadNumber'))
|
2018-01-28 23:23:47 +11:00
|
|
|
state.SSL_CERT_PATH = settings('sslcert') \
|
|
|
|
if settings('sslcert') != 'None' else None
|
2017-08-22 16:16:21 +10:00
|
|
|
# Never set through the user
|
|
|
|
# state.KODI_PLEX_TIME_OFFSET = float(settings('kodiplextimeoffset'))
|
2017-09-03 21:28:40 +10:00
|
|
|
if changed is True:
|
|
|
|
# Assume that the user changed the settings so that we can now find
|
|
|
|
# the path to all media files
|
|
|
|
state.STOP_SYNC = False
|
|
|
|
state.PATH_VERIFIED = False
|
2016-01-09 21:53:04 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
def onNotification(self, sender, method, data):
|
2017-12-15 03:39:50 +11:00
|
|
|
"""
|
|
|
|
Called when a bunch of different stuff happens on the Kodi side
|
|
|
|
"""
|
2015-12-25 07:07:00 +11:00
|
|
|
if data:
|
2017-01-09 01:03:41 +11:00
|
|
|
data = loads(data, 'utf-8')
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.debug("Method: %s Data: %s", method, data)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if method == "Player.OnPlay":
|
2018-04-18 04:18:25 +10:00
|
|
|
state.SUSPEND_SYNC = True
|
2016-03-17 03:02:22 +11:00
|
|
|
self.PlayBackStart(data)
|
2016-04-08 21:51:29 +10:00
|
|
|
elif method == "Player.OnStop":
|
|
|
|
# Should refresh our video nodes, e.g. on deck
|
2016-04-12 17:05:52 +10:00
|
|
|
# xbmc.executebuiltin('ReloadSkin()')
|
2018-04-09 16:13:54 +10:00
|
|
|
if data.get('end'):
|
|
|
|
if state.PKC_CAUSED_STOP is True:
|
|
|
|
state.PKC_CAUSED_STOP = False
|
|
|
|
LOG.debug('PKC caused this playback stop - ignoring')
|
|
|
|
else:
|
|
|
|
_playback_cleanup(ended=True)
|
|
|
|
else:
|
|
|
|
_playback_cleanup()
|
2018-04-18 05:01:51 +10:00
|
|
|
state.PKC_CAUSED_STOP_DONE = True
|
2018-04-18 04:18:25 +10:00
|
|
|
state.SUSPEND_SYNC = False
|
2017-12-21 19:28:06 +11:00
|
|
|
elif method == 'Playlist.OnAdd':
|
|
|
|
self._playlist_onadd(data)
|
|
|
|
elif method == 'Playlist.OnRemove':
|
|
|
|
self._playlist_onremove(data)
|
|
|
|
elif method == 'Playlist.OnClear':
|
|
|
|
self._playlist_onclear(data)
|
2015-12-25 07:07:00 +11:00
|
|
|
elif method == "VideoLibrary.OnUpdate":
|
|
|
|
# Manually marking as watched/unwatched
|
|
|
|
playcount = data.get('playcount')
|
|
|
|
item = data.get('item')
|
2018-01-26 03:15:38 +11:00
|
|
|
if playcount is None or item is None:
|
|
|
|
return
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
|
|
|
kodiid = item['id']
|
2016-04-26 21:53:19 +10:00
|
|
|
item_type = item['type']
|
2015-12-25 07:07:00 +11:00
|
|
|
except (KeyError, TypeError):
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.info("Item is invalid for playstate update.")
|
2018-01-26 03:15:38 +11:00
|
|
|
return
|
|
|
|
# Send notification to the server.
|
|
|
|
with plexdb.Get_Plex_DB() as plexcur:
|
|
|
|
plex_dbitem = plexcur.getItem_byKodiId(kodiid, item_type)
|
|
|
|
try:
|
|
|
|
itemid = plex_dbitem[0]
|
|
|
|
except TypeError:
|
|
|
|
LOG.error("Could not find itemid in plex database for a "
|
|
|
|
"video library update")
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2018-04-09 15:21:47 +10:00
|
|
|
# notify the server
|
|
|
|
if playcount > 0:
|
|
|
|
scrobble(itemid, 'watched')
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2018-04-09 15:21:47 +10:00
|
|
|
scrobble(itemid, 'unwatched')
|
2015-12-25 07:07:00 +11:00
|
|
|
elif method == "VideoLibrary.OnRemove":
|
2016-02-17 15:13:10 +11:00
|
|
|
pass
|
2016-08-31 00:57:47 +10:00
|
|
|
elif method == "System.OnSleep":
|
|
|
|
# Connection is going to sleep
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.info("Marking the server as offline. SystemOnSleep activated.")
|
2016-08-31 00:57:47 +10:00
|
|
|
window('plex_online', value="sleep")
|
2015-12-25 07:07:00 +11:00
|
|
|
elif method == "System.OnWake":
|
|
|
|
# Allow network to wake up
|
2018-04-09 16:13:54 +10:00
|
|
|
xbmc.sleep(10000)
|
2016-08-31 00:57:47 +10:00
|
|
|
window('plex_online', value="false")
|
2016-05-15 20:33:14 +10:00
|
|
|
elif method == "GUI.OnScreensaverDeactivated":
|
2016-08-31 00:57:47 +10:00
|
|
|
if settings('dbSyncScreensaver') == "true":
|
2018-04-09 16:13:54 +10:00
|
|
|
xbmc.sleep(5000)
|
2017-08-22 02:53:38 +10:00
|
|
|
plex_command('RUN_LIB_SCAN', 'full')
|
2017-05-17 18:09:57 +10:00
|
|
|
elif method == "System.OnQuit":
|
2017-12-15 03:39:50 +11:00
|
|
|
LOG.info('Kodi OnQuit detected - shutting down')
|
2017-05-17 18:09:57 +10:00
|
|
|
state.STOP_PKC = True
|
|
|
|
|
2017-12-21 19:28:06 +11:00
|
|
|
@LOCKER.lockthis
|
|
|
|
def _playlist_onadd(self, data):
|
|
|
|
"""
|
|
|
|
Called if an item is added to a Kodi playlist. Example data dict:
|
|
|
|
{
|
|
|
|
u'item': {
|
|
|
|
u'type': u'movie',
|
|
|
|
u'id': 2},
|
|
|
|
u'playlistid': 1,
|
|
|
|
u'position': 0
|
|
|
|
}
|
|
|
|
Will NOT be called if playback initiated by Kodi widgets
|
|
|
|
"""
|
2018-03-28 17:04:03 +11:00
|
|
|
if 'id' not in data['item']:
|
|
|
|
return
|
2018-03-07 04:23:56 +11:00
|
|
|
old = state.OLD_PLAYER_STATES[data['playlistid']]
|
|
|
|
if (not state.DIRECT_PATHS and data['position'] == 0 and
|
|
|
|
not PQ.PLAYQUEUES[data['playlistid']].items and
|
|
|
|
data['item']['type'] == old['kodi_type'] and
|
|
|
|
data['item']['id'] == old['kodi_id']):
|
2018-01-26 19:47:58 +11:00
|
|
|
# Hack we need for RESUMABLE items because Kodi lost the path of the
|
|
|
|
# last played item that is now being replayed (see playback.py's
|
2018-03-07 04:23:56 +11:00
|
|
|
# Player().play()) Also see playqueue.py _compare_playqueues()
|
2018-01-26 19:47:58 +11:00
|
|
|
LOG.info('Detected re-start of playback of last item')
|
|
|
|
kwargs = {
|
|
|
|
'plex_id': old['plex_id'],
|
|
|
|
'plex_type': old['plex_type'],
|
|
|
|
'path': old['file'],
|
|
|
|
'resolve': False
|
|
|
|
}
|
|
|
|
thread = Thread(target=playback_triage, kwargs=kwargs)
|
|
|
|
thread.start()
|
|
|
|
return
|
2017-12-21 19:28:06 +11:00
|
|
|
|
|
|
|
def _playlist_onremove(self, data):
|
|
|
|
"""
|
|
|
|
Called if an item is removed from a Kodi playlist. Example data dict:
|
|
|
|
{
|
|
|
|
u'playlistid': 1,
|
|
|
|
u'position': 0
|
|
|
|
}
|
|
|
|
"""
|
2018-02-04 22:06:39 +11:00
|
|
|
pass
|
2017-12-21 19:28:06 +11:00
|
|
|
|
2018-03-07 04:23:56 +11:00
|
|
|
@LOCKER.lockthis
|
2017-12-21 19:28:06 +11:00
|
|
|
def _playlist_onclear(self, data):
|
|
|
|
"""
|
|
|
|
Called if a Kodi playlist is cleared. Example data dict:
|
|
|
|
{
|
|
|
|
u'playlistid': 1,
|
|
|
|
}
|
|
|
|
"""
|
2018-03-07 04:23:56 +11:00
|
|
|
playqueue = PQ.PLAYQUEUES[data['playlistid']]
|
|
|
|
if not playqueue.is_pkc_clear():
|
2018-04-15 21:16:58 +10:00
|
|
|
playqueue.pkc_edit = True
|
2018-03-07 04:23:56 +11:00
|
|
|
playqueue.clear(kodi=False)
|
|
|
|
else:
|
|
|
|
LOG.debug('Detected PKC clear - ignoring')
|
2017-12-21 19:28:06 +11:00
|
|
|
|
2018-03-29 16:33:07 +11:00
|
|
|
def _get_ids(self, kodi_id, kodi_type, path):
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
2018-03-29 16:33:07 +11:00
|
|
|
Returns the tuple (plex_id, plex_type) or (None, None)
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
2017-12-14 06:14:27 +11:00
|
|
|
# No Kodi id returned by Kodi, even if there is one. Ex: Widgets
|
2018-03-29 16:33:07 +11:00
|
|
|
plex_id = None
|
|
|
|
plex_type = None
|
2017-12-14 06:14:27 +11:00
|
|
|
# If using direct paths and starting playback from a widget
|
2018-03-29 16:33:07 +11:00
|
|
|
if not kodi_id and kodi_type and path:
|
2018-04-28 17:12:29 +10:00
|
|
|
kodi_id, _ = kodiid_from_filename(path, kodi_type)
|
2018-03-29 16:33:07 +11:00
|
|
|
if kodi_id:
|
2018-03-14 17:41:53 +11:00
|
|
|
with plexdb.Get_Plex_DB() as plex_db:
|
|
|
|
plex_dbitem = plex_db.getItem_byKodiId(kodi_id, kodi_type)
|
|
|
|
try:
|
|
|
|
plex_id = plex_dbitem[0]
|
|
|
|
plex_type = plex_dbitem[2]
|
|
|
|
except TypeError:
|
|
|
|
# No plex id, hence item not in the library. E.g. clips
|
|
|
|
pass
|
2018-03-29 16:33:07 +11:00
|
|
|
return plex_id, plex_type
|
2018-01-11 06:14:05 +11:00
|
|
|
|
2018-02-04 02:40:24 +11:00
|
|
|
@staticmethod
|
|
|
|
def _add_remaining_items_to_playlist(playqueue):
|
|
|
|
"""
|
|
|
|
Adds all but the very first item of the Kodi playlist to the Plex
|
|
|
|
playqueue
|
|
|
|
"""
|
|
|
|
items = js.playlist_get_items(playqueue.playlistid)
|
|
|
|
if not items:
|
|
|
|
LOG.error('Could not retrieve Kodi playlist items')
|
|
|
|
return
|
|
|
|
# Remove first item
|
|
|
|
items.pop(0)
|
|
|
|
try:
|
|
|
|
for i, item in enumerate(items):
|
2018-05-02 23:34:21 +10:00
|
|
|
PL.add_item_to_plex_playqueue(playqueue, i + 1, kodi_item=item)
|
2018-02-04 02:40:24 +11:00
|
|
|
except PL.PlaylistError:
|
|
|
|
LOG.info('Could not build Plex playlist for: %s', items)
|
|
|
|
|
2018-03-29 16:33:07 +11:00
|
|
|
def _json_item(self, playerid):
|
|
|
|
"""
|
|
|
|
Uses JSON RPC to get the playing item's info and returns the tuple
|
|
|
|
kodi_id, kodi_type, path
|
|
|
|
or None each time if not found.
|
|
|
|
"""
|
|
|
|
if not self._already_slept:
|
|
|
|
# SLEEP before calling this for the first time just after playback
|
|
|
|
# start as Kodi updates this info very late!! Might get previous
|
|
|
|
# element otherwise
|
|
|
|
self._already_slept = True
|
2018-04-09 16:13:54 +10:00
|
|
|
xbmc.sleep(1000)
|
2018-03-29 16:33:07 +11:00
|
|
|
json_item = js.get_item(playerid)
|
|
|
|
LOG.debug('Kodi playing item properties: %s', json_item)
|
|
|
|
return (json_item.get('id'),
|
|
|
|
json_item.get('type'),
|
|
|
|
json_item.get('file'))
|
|
|
|
|
2018-01-11 06:14:05 +11:00
|
|
|
@LOCKER.lockthis
|
|
|
|
def PlayBackStart(self, data):
|
|
|
|
"""
|
|
|
|
Called whenever playback is started. Example data:
|
|
|
|
{
|
|
|
|
u'item': {u'type': u'movie', u'title': u''},
|
|
|
|
u'player': {u'playerid': 1, u'speed': 1}
|
|
|
|
}
|
|
|
|
Unfortunately when using Widgets, Kodi doesn't tell us shit
|
|
|
|
"""
|
2018-03-29 16:33:07 +11:00
|
|
|
self._already_slept = False
|
2018-01-11 06:14:05 +11:00
|
|
|
# Get the type of media we're playing
|
|
|
|
try:
|
|
|
|
playerid = data['player']['playerid']
|
|
|
|
except (TypeError, KeyError):
|
|
|
|
LOG.info('Aborting playback report - item invalid for updates %s',
|
|
|
|
data)
|
|
|
|
return
|
2018-01-23 17:59:53 +11:00
|
|
|
if playerid == -1:
|
|
|
|
# Kodi might return -1 for "last player"
|
|
|
|
try:
|
|
|
|
playerid = js.get_player_ids()[0]
|
|
|
|
except IndexError:
|
|
|
|
LOG.error('Could not retreive active player - aborting')
|
|
|
|
return
|
2018-01-07 01:19:12 +11:00
|
|
|
playqueue = PQ.PLAYQUEUES[playerid]
|
2018-01-11 06:14:05 +11:00
|
|
|
info = js.get_player_props(playerid)
|
|
|
|
pos = info['position'] if info['position'] != -1 else 0
|
2018-02-04 01:57:37 +11:00
|
|
|
LOG.debug('Detected position %s for %s', pos, playqueue)
|
2018-02-04 02:16:53 +11:00
|
|
|
status = state.PLAYER_STATES[playerid]
|
2018-03-29 16:33:07 +11:00
|
|
|
kodi_id = data.get('id')
|
|
|
|
kodi_type = data.get('type')
|
|
|
|
path = data.get('file')
|
2017-12-21 19:28:06 +11:00
|
|
|
try:
|
2018-01-11 06:14:05 +11:00
|
|
|
item = playqueue.items[pos]
|
2017-12-21 19:28:06 +11:00
|
|
|
except IndexError:
|
2018-03-15 18:24:56 +11:00
|
|
|
# PKC playqueue not yet initialized
|
2018-03-29 16:33:07 +11:00
|
|
|
LOG.debug('Position %s not in PKC playqueue yet', pos)
|
2018-03-15 18:24:56 +11:00
|
|
|
initialize = True
|
|
|
|
else:
|
2018-03-29 16:33:07 +11:00
|
|
|
if not kodi_id:
|
|
|
|
kodi_id, kodi_type, path = self._json_item(playerid)
|
|
|
|
if kodi_id and item.kodi_id:
|
|
|
|
if item.kodi_id != kodi_id or item.kodi_type != kodi_type:
|
|
|
|
LOG.debug('Detected different Kodi id')
|
2018-03-15 18:24:56 +11:00
|
|
|
initialize = True
|
|
|
|
else:
|
|
|
|
initialize = False
|
|
|
|
else:
|
|
|
|
# E.g. clips set-up previously with no Kodi DB entry
|
2018-03-29 16:33:07 +11:00
|
|
|
if not path:
|
|
|
|
kodi_id, kodi_type, path = self._json_item(playerid)
|
|
|
|
if item.file != path:
|
|
|
|
LOG.debug('Detected different path')
|
2018-03-15 18:24:56 +11:00
|
|
|
initialize = True
|
2018-02-04 00:59:43 +11:00
|
|
|
else:
|
2018-03-15 18:24:56 +11:00
|
|
|
initialize = False
|
|
|
|
if initialize:
|
|
|
|
LOG.debug('Need to initialize Plex and PKC playqueue')
|
2018-03-29 16:33:07 +11:00
|
|
|
if not kodi_id or not kodi_type:
|
|
|
|
kodi_id, kodi_type, path = self._json_item(playerid)
|
|
|
|
plex_id, plex_type = self._get_ids(kodi_id, kodi_type, path)
|
2018-03-15 18:24:56 +11:00
|
|
|
if not plex_id:
|
|
|
|
LOG.debug('No Plex id obtained - aborting playback report')
|
2018-04-01 02:51:03 +10:00
|
|
|
state.PLAYER_STATES[playerid] = copy.deepcopy(state.PLAYSTATE)
|
2018-03-15 18:24:56 +11:00
|
|
|
return
|
2018-05-02 02:08:31 +10:00
|
|
|
item = PL.init_plex_playqueue(playqueue, plex_id=plex_id)
|
2018-01-11 06:14:05 +11:00
|
|
|
# Set the Plex container key (e.g. using the Plex playqueue)
|
|
|
|
container_key = None
|
|
|
|
if info['playlistid'] != -1:
|
|
|
|
# -1 is Kodi's answer if there is no playlist
|
|
|
|
container_key = PQ.PLAYQUEUES[playerid].id
|
|
|
|
if container_key is not None:
|
|
|
|
container_key = '/playQueues/%s' % container_key
|
|
|
|
elif plex_id is not None:
|
|
|
|
container_key = '/library/metadata/%s' % plex_id
|
2018-02-07 23:32:10 +11:00
|
|
|
else:
|
2018-03-15 18:24:56 +11:00
|
|
|
LOG.debug('No need to initialize playqueues')
|
2018-02-07 23:32:10 +11:00
|
|
|
kodi_id = item.kodi_id
|
|
|
|
kodi_type = item.kodi_type
|
|
|
|
plex_id = item.plex_id
|
|
|
|
plex_type = item.plex_type
|
|
|
|
if playqueue.id:
|
|
|
|
container_key = '/playQueues/%s' % playqueue.id
|
|
|
|
else:
|
|
|
|
container_key = '/library/metadata/%s' % plex_id
|
2018-04-03 01:09:44 +10:00
|
|
|
# Remember that this player has been active
|
|
|
|
state.ACTIVE_PLAYERS.append(playerid)
|
2018-01-22 04:31:49 +11:00
|
|
|
status.update(info)
|
2018-03-29 16:33:07 +11:00
|
|
|
LOG.debug('Set the Plex container_key to: %s', container_key)
|
2018-02-04 02:16:53 +11:00
|
|
|
status['container_key'] = container_key
|
2018-01-22 04:31:49 +11:00
|
|
|
status['file'] = path
|
|
|
|
status['kodi_id'] = kodi_id
|
|
|
|
status['kodi_type'] = kodi_type
|
|
|
|
status['plex_id'] = plex_id
|
|
|
|
status['plex_type'] = plex_type
|
|
|
|
status['playmethod'] = item.playmethod
|
|
|
|
status['playcount'] = item.playcount
|
2018-02-04 02:20:10 +11:00
|
|
|
LOG.debug('Set the player state: %s', status)
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2018-01-22 21:20:37 +11:00
|
|
|
|
|
|
|
@thread_methods
|
|
|
|
class SpecialMonitor(Thread):
|
|
|
|
"""
|
|
|
|
Detect the resume dialog for widgets.
|
|
|
|
Could also be used to detect external players (see Emby implementation)
|
|
|
|
"""
|
|
|
|
def run(self):
|
|
|
|
LOG.info("----====# Starting Special Monitor #====----")
|
2018-01-28 22:52:31 +11:00
|
|
|
# "Start from beginning", "Play from beginning"
|
2018-04-09 16:13:54 +10:00
|
|
|
strings = (try_encode(xbmc.getLocalizedString(12021)),
|
|
|
|
try_encode(xbmc.getLocalizedString(12023)))
|
2018-02-12 00:57:39 +11:00
|
|
|
while not self.stopped():
|
2018-04-09 16:13:54 +10:00
|
|
|
if xbmc.getCondVisibility('Window.IsVisible(DialogContextMenu.xml)'):
|
|
|
|
if xbmc.getInfoLabel('Control.GetLabel(1002)') in strings:
|
2018-03-20 21:48:17 +11:00
|
|
|
# Remember that the item IS indeed resumable
|
|
|
|
control = int(Window(10106).getFocusId())
|
2018-04-04 01:07:37 +10:00
|
|
|
state.RESUME_PLAYBACK = True if control == 1001 else False
|
2018-01-28 22:52:31 +11:00
|
|
|
else:
|
2018-03-20 21:48:17 +11:00
|
|
|
# Different context menu is displayed
|
2018-01-28 22:52:31 +11:00
|
|
|
state.RESUME_PLAYBACK = False
|
2018-04-28 17:12:29 +10:00
|
|
|
if xbmc.getCondVisibility('Window.IsVisible(MyVideoNav.xml)'):
|
|
|
|
path = xbmc.getInfoLabel('container.folderpath')
|
|
|
|
if (isinstance(path, str) and
|
|
|
|
path.startswith('special://profile/playlists')):
|
|
|
|
pass
|
|
|
|
# TODO: start polling PMS for playlist changes
|
|
|
|
# Optionally: poll PMS continuously with custom intervall
|
2018-04-09 16:13:54 +10:00
|
|
|
xbmc.sleep(200)
|
2018-01-22 21:20:37 +11:00
|
|
|
LOG.info("#====---- Special Monitor Stopped ----====#")
|
2018-04-09 16:13:54 +10:00
|
|
|
|
|
|
|
|
|
|
|
@LOCKER.lockthis
|
|
|
|
def _playback_cleanup(ended=False):
|
|
|
|
"""
|
|
|
|
PKC cleanup after playback ends/is stopped. Pass ended=True if Kodi
|
|
|
|
completely finished playing an item (because we will get and use wrong
|
|
|
|
timing data otherwise)
|
|
|
|
"""
|
|
|
|
LOG.debug('playback_cleanup called. Active players: %s',
|
|
|
|
state.ACTIVE_PLAYERS)
|
|
|
|
# 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
|
|
|
|
for playerid in state.ACTIVE_PLAYERS:
|
|
|
|
status = state.PLAYER_STATES[playerid]
|
|
|
|
# Remember the last played item later
|
|
|
|
state.OLD_PLAYER_STATES[playerid] = copy.deepcopy(status)
|
|
|
|
# Stop transcoding
|
|
|
|
if status['playmethod'] == 'Transcode':
|
|
|
|
LOG.debug('Tell the PMS to stop transcoding')
|
|
|
|
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, ended)
|
|
|
|
# Reset the player's status
|
|
|
|
state.PLAYER_STATES[playerid] = copy.deepcopy(state.PLAYSTATE)
|
|
|
|
# As all playback has halted, reset the players that have been active
|
|
|
|
state.ACTIVE_PLAYERS = []
|
|
|
|
LOG.debug('Finished PKC playback cleanup')
|
|
|
|
|
|
|
|
|
|
|
|
def _record_playstate(status, ended):
|
|
|
|
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
|
|
|
|
totaltime = float(kodi_time_to_millis(status['totaltime'])) / 1000
|
|
|
|
if ended:
|
|
|
|
progress = 0.99
|
|
|
|
time = v.IGNORE_SECONDS_AT_START + 1
|
|
|
|
else:
|
|
|
|
time = float(kodi_time_to_millis(status['time'])) / 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']
|
|
|
|
last_played = unix_date_to_kodi(unix_timestamp())
|
|
|
|
if playcount is None:
|
|
|
|
LOG.debug('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 = None
|
|
|
|
last_played = None
|
|
|
|
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,
|
|
|
|
last_played)
|
|
|
|
# Hack to force "in progress" widget to appear if it wasn't visible before
|
|
|
|
if (state.FORCE_RELOAD_SKIN and
|
|
|
|
xbmc.getCondVisibility('Window.IsVisible(Home.xml)')):
|
|
|
|
LOG.debug('Refreshing skin to update widgets')
|
|
|
|
xbmc.executebuiltin('ReloadSkin()')
|
|
|
|
thread = Thread(target=_clean_file_table)
|
|
|
|
thread.setDaemon(True)
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
def _clean_file_table():
|
|
|
|
"""
|
|
|
|
If we associate a playing video e.g. pointing to plugin://... to an existing
|
|
|
|
Kodi library item, Kodi will add an additional entry for this (additional)
|
|
|
|
path plugin:// in the file table. This leads to all sorts of wierd behavior.
|
|
|
|
This function tries for at most 5 seconds to clean the file table.
|
|
|
|
"""
|
|
|
|
LOG.debug('Start cleaning Kodi files table')
|
|
|
|
i = 0
|
|
|
|
while i < 100 and not state.STOP_PKC:
|
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
|
|
|
files = kodi_db.obsolete_file_ids()
|
|
|
|
if files:
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
xbmc.sleep(50)
|
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
|
|
|
for file_id in files:
|
|
|
|
LOG.debug('Removing obsolete Kodi file_id %s', file_id)
|
|
|
|
kodi_db.remove_file(file_id[0], remove_orphans=False)
|
|
|
|
LOG.debug('Done cleaning up Kodi file table')
|