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
|
|
|
|
2016-08-31 00:57:47 +10:00
|
|
|
import logging
|
2015-12-25 07:07:00 +11:00
|
|
|
import json
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
|
|
|
|
import downloadutils
|
|
|
|
import embydb_functions as embydb
|
2016-03-23 02:17:06 +11:00
|
|
|
import kodidb_functions as kodidb
|
2015-12-25 07:07:00 +11:00
|
|
|
import playbackutils as pbutils
|
2016-08-31 00:57:47 +10:00
|
|
|
from utils import window, settings, CatchExceptions, tryDecode, tryEncode
|
2016-12-04 01:43:23 +11:00
|
|
|
from PlexFunctions import scrobble, REMAP_TYPE_FROM_PLEXTYPE
|
2016-01-13 00:14:49 +11:00
|
|
|
|
2016-02-20 06:03:06 +11:00
|
|
|
###############################################################################
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-08-31 00:57:47 +10:00
|
|
|
log = logging.getLogger("PLEX."+__name__)
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
|
|
|
|
class KodiMonitor(xbmc.Monitor):
|
|
|
|
|
2016-08-07 23:33:36 +10:00
|
|
|
def __init__(self):
|
2015-12-25 07:07:00 +11:00
|
|
|
|
2016-03-17 03:02:22 +11:00
|
|
|
self.doUtils = downloadutils.DownloadUtils().downloadUrl
|
2016-08-07 23:33:36 +10:00
|
|
|
self.xbmcplayer = xbmc.Player()
|
2016-07-22 23:54:03 +10:00
|
|
|
xbmc.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
|
|
|
|
"""
|
2016-10-24 02:10:34 +11:00
|
|
|
# settings: window-variable
|
|
|
|
items = {
|
|
|
|
'logLevel': 'plex_logLevel',
|
|
|
|
'enableContext': 'plex_context',
|
2016-12-03 23:07:41 +11:00
|
|
|
'plex_restricteduser': 'plex_restricteduser',
|
2016-12-04 01:43:23 +11:00
|
|
|
'dbSyncIndicator': 'dbSyncIndicator',
|
|
|
|
'remapSMB': 'remapSMB',
|
|
|
|
'replaceSMB': 'replaceSMB',
|
2016-10-24 02:10:34 +11:00
|
|
|
}
|
2016-12-04 01:43:23 +11:00
|
|
|
# Path replacement
|
|
|
|
for typus in REMAP_TYPE_FROM_PLEXTYPE.values():
|
|
|
|
for arg in ('Org', 'New'):
|
|
|
|
key = 'remapSMB%s%s' % (typus, arg)
|
|
|
|
items[key] = key
|
|
|
|
# Reset the window variables from the settings variables
|
2016-10-24 02:10:34 +11:00
|
|
|
for settings_value, window_value in items.iteritems():
|
|
|
|
if window(window_value) != settings(settings_value):
|
|
|
|
log.debug('PKC settings changed: %s is now %s'
|
|
|
|
% (settings_value, settings(settings_value)))
|
|
|
|
window(window_value, value=settings(settings_value))
|
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:
|
2016-03-17 03:02:22 +11:00
|
|
|
data = json.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')
|
|
|
|
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.
|
2016-03-16 22:49:55 +11:00
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
2016-04-26 21:53:19 +10:00
|
|
|
emby_dbitem = emby_db.getItem_byKodiId(kodiid, item_type)
|
2015-12-25 07:07:00 +11:00
|
|
|
try:
|
|
|
|
itemid = emby_dbitem[0]
|
|
|
|
except TypeError:
|
2016-08-31 00:57:47 +10:00
|
|
|
log.error("Could not find itemid in emby database for a "
|
|
|
|
"video library update")
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
# Stop from manually marking as watched unwatched, with actual playback.
|
2016-08-31 00:57:47 +10:00
|
|
|
if window('emby_skipWatched%s' % itemid) == "true":
|
2015-12-25 07:07:00 +11:00
|
|
|
# property is set in player.py
|
2016-08-31 00:57:47 +10:00
|
|
|
window('emby_skipWatched%s' % itemid, clear=True)
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
# notify the server
|
|
|
|
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
|
|
|
# Removed function, because with plugin paths + clean library, it will wipe
|
|
|
|
# entire library if user has permissions. Instead, use the emby context menu available
|
|
|
|
# in Isengard and higher version
|
|
|
|
pass
|
|
|
|
'''try:
|
2015-12-25 07:07:00 +11:00
|
|
|
kodiid = data['id']
|
|
|
|
type = data['type']
|
|
|
|
except (KeyError, TypeError):
|
2016-09-05 00:57:06 +10:00
|
|
|
log.info("Item is invalid for emby deletion.")
|
2015-12-25 07:07:00 +11:00
|
|
|
else:
|
|
|
|
# Send the delete action to the server.
|
2015-12-28 20:54:46 +11:00
|
|
|
embyconn = utils.kodiSQL('emby')
|
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
emby_dbitem = emby_db.getItem_byKodiId(kodiid, type)
|
|
|
|
try:
|
|
|
|
itemid = emby_dbitem[0]
|
|
|
|
except TypeError:
|
2016-09-05 00:57:06 +10:00
|
|
|
log.info("Could not find itemid in emby database.")
|
2015-12-28 20:54:46 +11:00
|
|
|
else:
|
2016-08-31 00:57:47 +10:00
|
|
|
if settings('skipContextMenu') != "true":
|
2015-12-28 20:54:46 +11:00
|
|
|
resp = xbmcgui.Dialog().yesno(
|
|
|
|
heading="Confirm delete",
|
|
|
|
line1="Delete file on Emby Server?")
|
|
|
|
if not resp:
|
2016-09-05 00:57:06 +10:00
|
|
|
log.info("User skipped deletion.")
|
2015-12-28 20:54:46 +11:00
|
|
|
embycursor.close()
|
|
|
|
return
|
2016-02-13 09:30:20 +11:00
|
|
|
|
2015-12-28 20:54:46 +11:00
|
|
|
url = "{server}/emby/Items/%s?format=json" % itemid
|
2016-09-05 00:57:06 +10:00
|
|
|
log.info("Deleting request: %s" % itemid)
|
2016-04-26 21:53:19 +10:00
|
|
|
doUtils.downloadUrl(url, action_type="DELETE")
|
2015-12-28 20:54:46 +11:00
|
|
|
finally:
|
2016-02-17 15:13:10 +11:00
|
|
|
embycursor.close()'''
|
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
|
|
|
|
xbmc.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":
|
2016-05-16 00:02:19 +10:00
|
|
|
xbmc.sleep(5000)
|
2016-08-31 00:57:47 +10:00
|
|
|
window('plex_runLibScan', value="full")
|
2016-05-15 20:33:14 +10:00
|
|
|
|
2015-12-25 07:07:00 +11:00
|
|
|
elif method == "Playlist.OnClear":
|
2016-03-17 03:02:22 +11:00
|
|
|
pass
|
|
|
|
|
|
|
|
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:
|
|
|
|
xbmc.sleep(100)
|
|
|
|
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
|
2016-08-31 00:57:47 +10:00
|
|
|
plexid = window('emby_%s.itemid' % tryEncode(currentFile))
|
2016-06-26 00:02:40 +10:00
|
|
|
# Get rid of the '' if the window property was not set
|
|
|
|
plexid = None if not plexid else plexid
|
|
|
|
kodiid = None
|
|
|
|
if plexid 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
|
2016-06-26 19:53:26 +10:00
|
|
|
if (kodiid is None and plexid is None and typus != 'song'
|
2016-06-26 00:02:40 +10:00
|
|
|
and not currentFile.startswith('http')):
|
2016-06-25 20:25:01 +10:00
|
|
|
try:
|
2016-06-26 00:02:40 +10:00
|
|
|
filename = currentFile.rsplit('/', 1)[1]
|
|
|
|
path = currentFile.rsplit('/', 1)[0] + '/'
|
|
|
|
except IndexError:
|
|
|
|
filename = currentFile.rsplit('\\', 1)[1]
|
|
|
|
path = currentFile.rsplit('\\', 1)[0] + '\\'
|
2016-08-31 00:57:47 +10:00
|
|
|
log.debug('Trying to figure out playing item from filename: %s '
|
|
|
|
'and path: %s' % (filename, path))
|
2016-06-26 00:02:40 +10:00
|
|
|
with kodidb.GetKodiDB('video') as kodi_db:
|
|
|
|
try:
|
|
|
|
kodiid, typus = kodi_db.getIdFromFilename(filename, path)
|
|
|
|
except TypeError:
|
2016-08-31 00:57:47 +10:00
|
|
|
log.info('Abort playback report, could not id kodi item')
|
2016-06-26 00:02:40 +10:00
|
|
|
return
|
2016-06-25 05:28:30 +10:00
|
|
|
|
2016-06-26 00:02:40 +10:00
|
|
|
if plexid is None:
|
|
|
|
# Get Plex' item id
|
|
|
|
with embydb.GetEmbyDB() as emby_db:
|
|
|
|
emby_dbitem = emby_db.getItem_byKodiId(kodiid, typus)
|
|
|
|
try:
|
|
|
|
plexid = emby_dbitem[0]
|
|
|
|
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"
|
|
|
|
% (plexid, kodiid, typus))
|
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")):
|
2016-06-26 00:02:40 +10:00
|
|
|
if self.StartDirectPath(plexid,
|
|
|
|
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)
|
2016-09-18 04:48:37 +10:00
|
|
|
window('plex_currently_playing_itemid', value=plexid)
|
2016-08-31 00:57:47 +10:00
|
|
|
window("emby_%s.itemid" % tryEncode(currentFile), value=plexid)
|
|
|
|
log.info('Finish playback startup')
|
2016-03-17 03:02:22 +11:00
|
|
|
|
|
|
|
def StartDirectPath(self, plexid, type, currentFile):
|
|
|
|
"""
|
|
|
|
Set some additional stuff if playback was initiated by Kodi, not PKC
|
|
|
|
"""
|
|
|
|
result = self.doUtils('{server}/library/metadata/%s' % plexid)
|
|
|
|
try:
|
|
|
|
result[0].attrib
|
|
|
|
except:
|
2016-08-31 00:57:47 +10:00
|
|
|
log.error('Did not receive a valid XML for plexid %s.' % plexid)
|
2016-03-17 03:02:22 +11:00
|
|
|
return False
|
|
|
|
# Setup stuff, because playback was started by Kodi, not PKC
|
|
|
|
pbutils.PlaybackUtils(result[0]).setProperties(
|
|
|
|
currentFile, xbmcgui.ListItem())
|
2016-08-31 00:57:47 +10:00
|
|
|
if type == "song" and settings('streamMusic') == "true":
|
|
|
|
window('emby_%s.playmethod' % currentFile, value="DirectStream")
|
2016-03-17 03:02:22 +11:00
|
|
|
else:
|
2016-08-31 00:57:47 +10:00
|
|
|
window('emby_%s.playmethod' % currentFile, value="DirectPlay")
|
|
|
|
log.debug('Window properties set for direct paths!')
|