2015-12-25 07:07:00 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-09-03 20:46:41 +10:00
|
|
|
from logging import getLogger
|
2017-01-09 01:03:41 +11:00
|
|
|
from json import loads
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-01-09 01:03:41 +11:00
|
|
|
from xbmc import Monitor, Player, sleep
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2017-09-03 20:46:41 +10:00
|
|
|
from downloadutils import DownloadUtils
|
2017-01-05 06:57:16 +11:00
|
|
|
import plexdb_functions as plexdb
|
2017-08-22 02:53:38 +10:00
|
|
|
from utils import window, settings, CatchExceptions, tryDecode, tryEncode, \
|
|
|
|
plex_command
|
2017-01-25 02:04:42 +11:00
|
|
|
from PlexFunctions import scrobble
|
2016-12-29 21:22:02 +11:00
|
|
|
from kodidb_functions import get_kodiid_from_filename
|
2017-01-09 01:03:41 +11:00
|
|
|
from PlexAPI import API
|
2017-05-17 18:09:57 +10:00
|
|
|
import state
|
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-09-03 20:46:41 +10: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',
|
|
|
|
'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!
|
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',
|
|
|
|
'enableBackgroundSync': 'BACKGROUND_SYNC'
|
2017-08-21 15:42:11 +10: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):
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-12-28 03:33:52 +11:00
|
|
|
def __init__(self, callback):
|
|
|
|
self.mgr = callback
|
2017-09-03 20:46:41 +10:00
|
|
|
self.doUtils = DownloadUtils().downloadUrl
|
2017-01-09 01:03:41 +11:00
|
|
|
self.xbmcplayer = Player()
|
2016-12-28 03:33:52 +11:00
|
|
|
self.playqueue = self.mgr.playqueue
|
2017-01-09 01:03:41 +11:00
|
|
|
Monitor.__init__(self)
|
2016-08-31 00:57:47 +10:00
|
|
|
log.info("Kodi monitor started.")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
def onScanStarted(self, library):
|
2016-08-31 00:57:47 +10:00
|
|
|
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):
|
2016-08-31 00:57:47 +10:00
|
|
|
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
|
|
|
|
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-08-19 23:23:57 +10: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-09-01 20:19:27 +10:00
|
|
|
log.debug('PKC window settings changed: %s is now %s'
|
2016-10-24 02:10:34 +11:00
|
|
|
% (settings_value, settings(settings_value)))
|
|
|
|
window(window_value, value=settings(settings_value))
|
2017-01-21 00:41:28 +11:00
|
|
|
if settings_value == 'fetch_pms_item_number':
|
|
|
|
log.info('Requesting playlist/nodes refresh')
|
2017-08-22 02:53:38 +10:00
|
|
|
plex_command('RUN_LIB_SCAN', 'views')
|
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-09-01 20:31:58 +10:00
|
|
|
log.debug('PKC state settings %s changed from %s to %s'
|
2017-09-03 21:23:18 +10:00
|
|
|
% (settings_value, getattr(state, state_name), new))
|
|
|
|
setattr(state, state_name, new)
|
2017-08-22 16:16:21 +10:00
|
|
|
# Special cases, overwrite all internal settings
|
|
|
|
state.FULL_SYNC_INTERVALL = int(settings('fullSyncInterval'))*60
|
|
|
|
state.BACKGROUNDSYNC_SAFTYMARGIN = int(
|
|
|
|
settings('backgroundsync_saftyMargin'))
|
|
|
|
state.SYNC_THREAD_NUMBER = int(settings('syncThreadNumber'))
|
|
|
|
# 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
|
|
|
|
2016-08-31 00:57:47 +10:00
|
|
|
@CatchExceptions(warnuser=False)
|
2015-12-25 07:07:00 +11:00
|
|
|
def onNotification(self, sender, method, data):
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
if data:
|
2017-01-09 01:03:41 +11:00
|
|
|
data = loads(data, 'utf-8')
|
2016-08-31 00:57:47 +10:00
|
|
|
log.debug("Method: %s Data: %s" % (method, data))
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
if method == "Player.OnPlay":
|
2016-03-17 03:02:22 +11:00
|
|
|
self.PlayBackStart(data)
|
2015-12-25 07:07:00 +11:00
|
|
|
|
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()')
|
|
|
|
pass
|
2016-04-08 21:51:29 +10:00
|
|
|
|
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')
|
2017-07-17 20:30:12 +10:00
|
|
|
|
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):
|
2016-08-31 00:57:47 +10:00
|
|
|
log.info("Item is invalid for playstate update.")
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
# Send notification to the server.
|
2017-01-05 06:57:16 +11:00
|
|
|
with plexdb.Get_Plex_DB() as plexcur:
|
|
|
|
plex_dbitem = plexcur.getItem_byKodiId(kodiid, item_type)
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
2017-01-05 06:57:16 +11:00
|
|
|
itemid = plex_dbitem[0]
|
2015-12-25 07:07:00 +11:00
|
|
|
except TypeError:
|
2017-01-05 06:57:16 +11:00
|
|
|
log.error("Could not find itemid in plex database for a "
|
2016-08-31 00:57:47 +10:00
|
|
|
"video library update")
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2017-05-02 03:51:10 +10:00
|
|
|
# Stop from manually marking as watched unwatched, with
|
|
|
|
# actual playback.
|
2017-01-09 01:03:41 +11:00
|
|
|
if window('plex_skipWatched%s' % itemid) == "true":
|
2015-12-25 07:07:00 +11:00
|
|
|
# property is set in player.py
|
2017-01-09 01:03:41 +11:00
|
|
|
window('plex_skipWatched%s' % itemid, clear=True)
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
# notify the server
|
2017-07-17 20:30:12 +10:00
|
|
|
if playcount > 0:
|
2016-03-12 00:42:14 +11:00
|
|
|
scrobble(itemid, 'watched')
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
2016-03-12 00:42:14 +11: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
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-08-31 00:57:47 +10:00
|
|
|
elif method == "System.OnSleep":
|
|
|
|
# Connection is going to sleep
|
|
|
|
log.info("Marking the server as offline. SystemOnSleep activated.")
|
|
|
|
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")
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-05-15 20:33:14 +10:00
|
|
|
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')
|
2016-05-15 20:33:14 +10:00
|
|
|
|
2017-05-17 18:09:57 +10:00
|
|
|
elif method == "System.OnQuit":
|
|
|
|
log.info('Kodi OnQuit detected - shutting down')
|
|
|
|
state.STOP_PKC = True
|
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
def PlayBackStart(self, data):
|
|
|
|
"""
|
|
|
|
Called whenever a playback is started
|
|
|
|
"""
|
2016-04-18 20:09:01 +10:00
|
|
|
# Get currently playing file - can take a while. Will be utf-8!
|
|
|
|
try:
|
|
|
|
currentFile = self.xbmcplayer.getPlayingFile()
|
|
|
|
except:
|
|
|
|
currentFile = None
|
|
|
|
count = 0
|
|
|
|
while currentFile is None:
|
2017-01-09 01:03:41 +11:00
|
|
|
sleep(100)
|
2016-04-18 20:09:01 +10:00
|
|
|
try:
|
|
|
|
currentFile = self.xbmcplayer.getPlayingFile()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if count == 50:
|
2016-08-31 00:57:47 +10:00
|
|
|
log.info("No current File, cancel OnPlayBackStart...")
|
2016-04-18 20:09:01 +10:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
count += 1
|
2016-06-26 00:02:40 +10:00
|
|
|
# Just to be on the safe side
|
2016-08-31 00:57:47 +10:00
|
|
|
currentFile = tryDecode(currentFile)
|
|
|
|
log.debug("Currently playing file is: %s" % currentFile)
|
2016-04-18 20:09:01 +10:00
|
|
|
|
2016-06-25 05:28:30 +10:00
|
|
|
# Get the type of media we're playing
|
2016-03-17 03:02:22 +11:00
|
|
|
try:
|
2016-06-25 05:28:30 +10:00
|
|
|
typus = data['item']['type']
|
|
|
|
except (TypeError, KeyError):
|
2016-08-31 00:57:47 +10:00
|
|
|
log.info("Item is invalid for PMS playstate update.")
|
2016-03-17 03:02:22 +11:00
|
|
|
return
|
2016-08-31 00:57:47 +10:00
|
|
|
log.debug("Playing itemtype is (or appears to be): %s" % typus)
|
2016-06-25 05:28:30 +10:00
|
|
|
|
|
|
|
# Try to get a Kodi ID
|
2016-06-26 00:02:40 +10:00
|
|
|
# If PKC was used - native paths, not direct paths
|
2017-05-02 03:51:10 +10:00
|
|
|
plex_id = window('plex_%s.itemid' % tryEncode(currentFile))
|
2016-06-26 00:02:40 +10:00
|
|
|
# Get rid of the '' if the window property was not set
|
2017-05-02 03:51:10 +10:00
|
|
|
plex_id = None if not plex_id else plex_id
|
2016-06-26 00:02:40 +10:00
|
|
|
kodiid = None
|
2017-05-02 03:51:10 +10:00
|
|
|
if plex_id is None:
|
2016-08-31 00:57:47 +10:00
|
|
|
log.debug('Did not get Plex id from window properties')
|
2016-06-25 20:25:01 +10:00
|
|
|
try:
|
2016-06-26 00:02:40 +10:00
|
|
|
kodiid = data['item']['id']
|
2016-06-25 20:25:01 +10:00
|
|
|
except (TypeError, KeyError):
|
2016-08-31 00:57:47 +10:00
|
|
|
log.debug('Did not get a Kodi id from Kodi, darn')
|
2016-06-26 00:02:40 +10:00
|
|
|
# For direct paths, if we're not streaming something
|
|
|
|
# When using Widgets, Kodi doesn't tell us shit so we need this hack
|
2017-05-02 03:51:10 +10:00
|
|
|
if (kodiid is None and plex_id is None and typus != 'song'
|
2016-06-26 00:02:40 +10:00
|
|
|
and not currentFile.startswith('http')):
|
2016-12-29 21:22:02 +11:00
|
|
|
(kodiid, typus) = get_kodiid_from_filename(currentFile)
|
|
|
|
if kodiid is None:
|
|
|
|
return
|
2016-06-25 05:28:30 +10:00
|
|
|
|
2017-05-02 03:51:10 +10:00
|
|
|
if plex_id is None:
|
2016-06-26 00:02:40 +10:00
|
|
|
# Get Plex' item id
|
2017-01-05 06:57:16 +11:00
|
|
|
with plexdb.Get_Plex_DB() as plexcursor:
|
|
|
|
plex_dbitem = plexcursor.getItem_byKodiId(kodiid, typus)
|
2016-06-26 00:02:40 +10:00
|
|
|
try:
|
2017-05-02 03:51:10 +10:00
|
|
|
plex_id = plex_dbitem[0]
|
2016-06-26 00:02:40 +10:00
|
|
|
except TypeError:
|
2016-08-31 00:57:47 +10:00
|
|
|
log.info("No Plex id returned for kodiid %s. Aborting playback"
|
|
|
|
" report" % kodiid)
|
2016-06-25 20:25:01 +10:00
|
|
|
return
|
2016-08-31 00:57:47 +10:00
|
|
|
log.debug("Found Plex id %s for Kodi id %s for type %s"
|
2017-05-02 03:51:10 +10:00
|
|
|
% (plex_id, kodiid, typus))
|
|
|
|
|
|
|
|
# Switch subtitle tracks if applicable
|
|
|
|
subtitle = window('plex_%s.subtitle' % tryEncode(currentFile))
|
|
|
|
if window(tryEncode('plex_%s.playmethod' % currentFile)) \
|
|
|
|
== 'Transcode' and subtitle:
|
|
|
|
if window('plex_%s.subtitle' % currentFile) == 'None':
|
|
|
|
self.xbmcplayer.showSubtitles(False)
|
|
|
|
else:
|
|
|
|
self.xbmcplayer.setSubtitleStream(int(subtitle))
|
2016-03-17 03:02:22 +11:00
|
|
|
|
|
|
|
# Set some stuff if Kodi initiated playback
|
2016-08-31 00:57:47 +10:00
|
|
|
if ((settings('useDirectPaths') == "1" and not typus == "song")
|
2016-06-25 05:28:30 +10:00
|
|
|
or
|
2016-08-31 00:57:47 +10:00
|
|
|
(typus == "song" and settings('enableMusic') == "true")):
|
2017-05-02 03:51:10 +10:00
|
|
|
if self.StartDirectPath(plex_id,
|
2016-06-26 00:02:40 +10:00
|
|
|
typus,
|
2016-08-31 00:57:47 +10:00
|
|
|
tryEncode(currentFile)) is False:
|
|
|
|
log.error('Could not initiate monitoring; aborting')
|
2016-03-17 03:02:22 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
# Save currentFile for cleanup later and to be able to access refs
|
2016-06-26 00:02:40 +10:00
|
|
|
window('plex_lastPlayedFiled', value=currentFile)
|
2017-05-02 03:51:10 +10:00
|
|
|
window('plex_currently_playing_itemid', value=plex_id)
|
|
|
|
window("plex_%s.itemid" % tryEncode(currentFile), value=plex_id)
|
2016-08-31 00:57:47 +10:00
|
|
|
log.info('Finish playback startup')
|
2016-03-17 03:02:22 +11:00
|
|
|
|
2017-05-02 03:51:10 +10:00
|
|
|
def StartDirectPath(self, plex_id, type, currentFile):
|
2016-03-17 03:02:22 +11:00
|
|
|
"""
|
|
|
|
Set some additional stuff if playback was initiated by Kodi, not PKC
|
|
|
|
"""
|
2017-05-02 03:51:10 +10:00
|
|
|
xml = self.doUtils('{server}/library/metadata/%s' % plex_id)
|
2016-03-17 03:02:22 +11:00
|
|
|
try:
|
2017-01-09 01:03:41 +11:00
|
|
|
xml[0].attrib
|
2016-03-17 03:02:22 +11:00
|
|
|
except:
|
2017-05-02 03:51:10 +10:00
|
|
|
log.error('Did not receive a valid XML for plex_id %s.' % plex_id)
|
2016-03-17 03:02:22 +11:00
|
|
|
return False
|
|
|
|
# Setup stuff, because playback was started by Kodi, not PKC
|
2017-01-09 01:03:41 +11:00
|
|
|
api = API(xml[0])
|
|
|
|
listitem = api.CreateListItemFromPlexItem()
|
|
|
|
api.set_playback_win_props(currentFile, listitem)
|
2016-08-31 00:57:47 +10:00
|
|
|
if type == "song" and settings('streamMusic') == "true":
|
2017-01-09 01:03:41 +11:00
|
|
|
window('plex_%s.playmethod' % currentFile, value="DirectStream")
|
2016-03-17 03:02:22 +11:00
|
|
|
else:
|
2017-01-09 01:03:41 +11:00
|
|
|
window('plex_%s.playmethod' % currentFile, value="DirectPlay")
|
2016-08-31 00:57:47 +10:00
|
|
|
log.debug('Window properties set for direct paths!')
|