PlexKodiConnect/resources/lib/kodimonitor.py

432 lines
17 KiB
Python
Raw Normal View History

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
2015-12-25 07:07:00 +11:00
2018-01-22 21:20:37 +11:00
from xbmc import Monitor, Player, sleep, getCondVisibility, getInfoLabel, \
getLocalizedString
from xbmcgui import Window
2015-12-25 07:07:00 +11:00
import plexdb_functions as plexdb
2018-01-24 05:10:18 +11:00
from utils import window, settings, plex_command, thread_methods
from PlexFunctions import scrobble
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-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
import state
2017-12-11 05:01:22 +11: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
# settings: window-variable
WINDOW_SETTINGS = {
'plex_restricteduser': 'plex_restricteduser',
'force_transcode_pix': 'plex_force_transcode_pix',
'fetch_pms_item_number': 'fetch_pms_item_number'
}
# settings: state-variable (state.py)
2017-09-03 21:23:18 +10:00
# Need to use getattr and setattr!
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-01-29 03:56:33 +11:00
'enableBackgroundSync': 'BACKGROUND_SYNC'
}
2018-01-22 04:31:49 +11:00
2016-08-31 00:57:47 +10:00
###############################################################################
2015-12-25 07:07:00 +11:00
2017-01-09 01:03:41 +11:00
class KodiMonitor(Monitor):
2017-12-15 03:39:50 +11:00
"""
PKC implementation of the Kodi Monitor class. Invoke only once.
"""
def __init__(self):
2017-01-09 01:03:41 +11:00
self.xbmcplayer = Player()
Monitor.__init__(self)
2018-01-22 04:31:49 +11:00
for playerid in state.PLAYER_STATES:
state.PLAYER_STATES[playerid] = dict(state.PLAYSTATE)
2018-01-26 03:15:38 +11:00
state.OLD_PLAYER_STATES[playerid] = dict(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
"""
LOG.debug("Kodi library scan %s running." % library)
2015-12-25 07:07:00 +11:00
if library == "video":
2016-08-31 00:57:47 +10:00
window('plex_kodiScan', value="true")
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
"""
LOG.debug("Kodi library scan %s finished." % library)
2015-12-25 07:07:00 +11:00
if library == "video":
2016-08-31 00:57:47 +10:00
window('plex_kodiScan', clear=True)
2015-12-25 07:07:00 +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
for settings_value, window_value in WINDOW_SETTINGS.iteritems():
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))
window(window_value, value=settings(settings_value))
if settings_value == 'fetch_pms_item_number':
2017-12-15 03:39:50 +11:00
LOG.info('Requesting playlist/nodes refresh')
2017-08-22 02:53:38 +10:00
plex_command('RUN_LIB_SCAN', 'views')
# Reset the state variables in state.py
2017-09-03 21:23:18 +10:00
for settings_value, state_name in STATE_SETTINGS.iteritems():
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)
# Special cases, overwrite all internal settings
2017-12-15 03:39:50 +11:00
state.FULL_SYNC_INTERVALL = int(settings('fullSyncInterval')) * 60
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
# 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
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":
self.PlayBackStart(data)
2016-04-08 21:51:29 +10:00
elif method == "Player.OnStop":
# Should refresh our video nodes, e.g. on deck
# xbmc.executebuiltin('ReloadSkin()')
pass
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']
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-01-26 03:15:38 +11:00
# Stop from manually marking as watched unwatched, with
# actual playback.
if window('plex_skipWatched%s' % itemid) == "true":
# property is set in player.py
window('plex_skipWatched%s' % itemid, clear=True)
2015-12-25 07:07:00 +11:00
else:
2018-01-26 03:15:38 +11:00
# notify the server
if playcount > 0:
scrobble(itemid, 'watched')
2015-12-25 07:07:00 +11:00
else:
2018-01-26 03:15:38 +11:00
scrobble(itemid, 'unwatched')
2015-12-25 07:07:00 +11:00
elif method == "VideoLibrary.OnRemove":
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
2017-01-09 01:03:41 +11:00
sleep(10000)
2016-08-31 00:57:47 +10:00
window('plex_onWake', value="true")
window('plex_online', value="false")
elif method == "GUI.OnScreensaverDeactivated":
2016-08-31 00:57:47 +10:00
if settings('dbSyncScreensaver') == "true":
2017-01-09 01:03:41 +11:00
sleep(5000)
2017-08-22 02:53:38 +10:00
plex_command('RUN_LIB_SCAN', 'full')
elif method == "System.OnQuit":
2017-12-15 03:39:50 +11:00
LOG.info('Kodi OnQuit detected - shutting down')
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
"""
playqueue = PQ.PLAYQUEUES[data['playlistid']]
# Did PKC cause this add? Then lets not do anything
if playqueue.is_kodi_onadd() is False:
LOG.debug('PKC added this item to the playqueue - ignoring')
return
2018-01-26 03:15:38 +11:00
kodi_item = js.get_item(data['playlistid'])
if (state.RESUMABLE is True and not kodi_item['file'] and
data['position'] == 0 and
data['item'].get('title') is not None and
getCondVisibility('Window.IsVisible(MyVideoNav.xml)')):
# 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
# Player().play())
LOG.info('Detected re-start of playback of last item')
old = state.OLD_PLAYER_STATES[data['playlistid']]
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
2018-01-21 23:42:22 +11:00
# Have we initiated the playqueue already? If not, ignore this
if not playqueue.items:
LOG.debug('Playqueue not initiated - ignoring')
return
2017-12-21 19:28:06 +11:00
# Playlist has been updated; need to tell Plex about it
if playqueue.id is None:
PL.init_Plex_playlist(playqueue, kodi_item=data['item'])
else:
PL.add_item_to_PMS_playlist(playqueue,
data['position'],
kodi_item=data['item'])
# Make sure that we won't re-add this item
2018-01-26 03:15:38 +11:00
# playqueue.old_kodi_pl = kodi_playqueue
2017-12-21 19:28:06 +11:00
@LOCKER.lockthis
def _playlist_onremove(self, data):
"""
Called if an item is removed from a Kodi playlist. Example data dict:
{
u'playlistid': 1,
u'position': 0
}
"""
playqueue = PQ.PLAYQUEUES[data['playlistid']]
# Did PKC cause this add? Then lets not do anything
if playqueue.is_kodi_onremove() is False:
LOG.debug('PKC removed this item already from playqueue - ignoring')
return
2017-12-21 19:28:06 +11:00
# Check whether we even need to update our known playqueue
kodi_playqueue = js.playlist_get_items(data['playlistid'])
if playqueue.old_kodi_pl == kodi_playqueue:
# We already know the latest playqueue - nothing to do
return
PL.delete_playlist_item_from_PMS(playqueue, data['position'])
playqueue.old_kodi_pl = kodi_playqueue
@LOCKER.lockthis
def _playlist_onclear(self, data):
"""
Called if a Kodi playlist is cleared. Example data dict:
{
u'playlistid': 1,
}
"""
playqueue = PQ.PLAYQUEUES[data['playlistid']]
if playqueue.is_kodi_onclear() is False:
LOG.debug('PKC already cleared the playqueue - ignoring')
return
playqueue.clear()
2017-12-21 19:28:06 +11:00
2018-01-11 06:14:05 +11:00
def _get_ids(self, json_item):
"""
"""
2018-01-11 06:14:05 +11:00
kodi_id = json_item.get('id')
kodi_type = json_item.get('type')
path = json_item.get('file')
2017-12-14 06:14:27 +11:00
if not path and not kodi_id:
2017-12-15 03:39:50 +11:00
LOG.info('Aborting playback report - no Kodi id or file for %s',
2018-01-11 06:14:05 +11:00
json_item)
raise RuntimeError
2017-12-14 06:14:27 +11:00
# Plex id will NOT be set with direct paths
plex_id = state.PLEX_IDS.get(path)
2017-12-11 05:01:22 +11:00
try:
2017-12-14 06:14:27 +11:00
plex_type = v.PLEX_TYPE_FROM_KODI_TYPE[kodi_type]
except KeyError:
2017-12-11 05:01:22 +11:00
plex_type = None
2017-12-14 06:14:27 +11:00
# No Kodi id returned by Kodi, even if there is one. Ex: Widgets
if plex_id and not kodi_id:
with plexdb.Get_Plex_DB() as plex_db:
plex_dbitem = plex_db.getItem_byId(plex_id)
try:
kodi_id = plex_dbitem[0]
except TypeError:
kodi_id = None
# If using direct paths and starting playback from a widget
if not path.startswith('http'):
if not kodi_id:
kodi_id = kodiid_from_filename(path, kodi_type)
if not plex_id and kodi_id:
with plexdb.Get_Plex_DB() as plex_db:
plex_dbitem = plex_db.getItem_byKodiId(kodi_id, kodi_type)
try:
plex_id = plex_dbitem[0]
2017-12-14 06:14:27 +11:00
plex_type = plex_dbitem[2]
except TypeError:
# No plex id, hence item not in the library. E.g. clips
pass
2018-01-11 06:14:05 +11:00
return kodi_id, kodi_type, plex_id, plex_type
@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
"""
# Get the type of media we're playing
try:
kodi_type = data['item']['type']
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-22 04:31:49 +11:00
# Remember that this player has been active
state.ACTIVE_PLAYERS.append(playerid)
playqueue = PQ.PLAYQUEUES[playerid]
2018-01-11 06:14:05 +11:00
info = js.get_player_props(playerid)
json_item = js.get_item(playerid)
path = json_item.get('file')
pos = info['position'] if info['position'] != -1 else 0
LOG.info('Detected position %s for %s', pos, playqueue)
2017-12-21 19:28:06 +11:00
try:
2018-01-11 06:14:05 +11:00
item = playqueue.items[pos]
# See if playback.py already initiated playback
init_done = item.init_done
2017-12-21 19:28:06 +11:00
except IndexError:
2018-01-11 06:14:05 +11:00
init_done = False
if init_done is True:
kodi_id = item.kodi_id
kodi_type = item.kodi_type
plex_id = item.plex_id
plex_type = item.plex_type
else:
try:
kodi_id, kodi_type, plex_id, plex_type = self._get_ids(json_item)
except RuntimeError:
return
LOG.info('Need to initialize Plex and PKC playqueue')
2017-12-21 19:28:06 +11:00
if plex_id:
2018-01-30 17:51:14 +11:00
item = PL.init_Plex_playlist(playqueue, plex_id=plex_id)
2017-12-21 19:28:06 +11:00
else:
2018-01-30 17:51:14 +11:00
item = PL.init_Plex_playlist(playqueue,
kodi_item={'id': kodi_id,
'type': kodi_type,
'file': path})
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
state.PLAYER_STATES[playerid]['container_key'] = container_key
LOG.debug('Set the Plex container_key to: %s', container_key)
2018-01-22 04:31:49 +11:00
status = state.PLAYER_STATES[playerid]
status.update(info)
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-01-11 06:14:05 +11:00
LOG.debug('Set the player state: %s', state.PLAYER_STATES[playerid])
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 #====----")
player = Player()
# "Start from beginning", "Play from beginning"
strings = (getLocalizedString(12021), getLocalizedString(12023))
2018-01-22 21:20:37 +11:00
while not self.thread_stopped():
is_playing = player.isPlaying()
if (not is_playing and
getCondVisibility('Window.IsVisible(DialogContextMenu.xml)') and
getInfoLabel('Control.GetLabel(1002)') in strings):
# Remember that the item IS indeed resumable
state.RESUMABLE = True
2018-01-22 21:20:37 +11:00
control = int(Window(10106).getFocusId())
if control == 1002:
# Start from beginning
state.RESUME_PLAYBACK = False
elif control == 1001:
2018-01-22 21:20:37 +11:00
state.RESUME_PLAYBACK = True
else:
# User chose something else from the context menu
state.RESUME_PLAYBACK = False
sleep(100)
2018-01-22 21:20:37 +11:00
LOG.info("#====---- Special Monitor Stopped ----====#")