2016-03-31 15:39:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import json
|
2016-07-25 03:39:40 +00:00
|
|
|
import logging
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
|
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
|
|
|
import embydb_functions as embydb
|
|
|
|
import playbackutils as pbutils
|
2016-07-25 03:39:40 +00:00
|
|
|
from utils import window, settings, kodiSQL
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class KodiMonitor(xbmc.Monitor):
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self.clientInfo = clientinfo.ClientInfo()
|
|
|
|
self.addonName = self.clientInfo.getAddonName()
|
|
|
|
self.doUtils = downloadutils.DownloadUtils()
|
|
|
|
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("Kodi monitor started.")
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def onScanStarted(self, library):
|
2016-07-25 03:39:40 +00:00
|
|
|
log.debug("Kodi library scan %s running." % library)
|
2016-03-31 15:39:00 +00:00
|
|
|
if library == "video":
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_kodiScan', value="true")
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
def onScanFinished(self, library):
|
2016-07-25 03:39:40 +00:00
|
|
|
log.debug("Kodi library scan %s finished." % library)
|
2016-03-31 15:39:00 +00:00
|
|
|
if library == "video":
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_kodiScan', clear=True)
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
def onSettingsChanged(self):
|
|
|
|
# Monitor emby settings
|
|
|
|
# Review reset setting at a later time, need to be adjusted to account for initial setup
|
|
|
|
# changes.
|
|
|
|
'''currentPath = utils.settings('useDirectPaths')
|
|
|
|
if utils.window('emby_pluginpath') != currentPath:
|
|
|
|
# Plugin path value changed. Offer to reset
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Changed to playback mode detected", 1)
|
2016-03-31 15:39:00 +00:00
|
|
|
utils.window('emby_pluginpath', value=currentPath)
|
|
|
|
resp = xbmcgui.Dialog().yesno(
|
|
|
|
heading="Playback mode change detected",
|
|
|
|
line1=(
|
|
|
|
"Detected the playback mode has changed. The database "
|
|
|
|
"needs to be recreated for the change to be applied. "
|
|
|
|
"Proceed?"))
|
|
|
|
if resp:
|
|
|
|
utils.reset()'''
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
currentLog = settings('logLevel')
|
|
|
|
if window('emby_logLevel') != currentLog:
|
2016-03-31 15:39:00 +00:00
|
|
|
# The log level changed, set new prop
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("New log level: %s" % currentLog)
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_logLevel', value=currentLog)
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
def onNotification(self, sender, method, data):
|
|
|
|
|
|
|
|
doUtils = self.doUtils
|
|
|
|
if method not in ("Playlist.OnAdd"):
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("Method: %s Data: %s" % (method, data))
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
if data:
|
|
|
|
data = json.loads(data,'utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
if method == "Player.OnPlay":
|
|
|
|
# Set up report progress for emby playback
|
|
|
|
item = data.get('item')
|
|
|
|
try:
|
|
|
|
kodiid = item['id']
|
|
|
|
item_type = item['type']
|
|
|
|
except (KeyError, TypeError):
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("Item is invalid for playstate update.")
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
2016-06-18 03:03:28 +00:00
|
|
|
if ((settings('useDirectPaths') == "1" and not item_type == "song") or
|
|
|
|
(item_type == "song" and settings('enableMusic') == "true")):
|
2016-03-31 15:39:00 +00:00
|
|
|
# Set up properties for player
|
2016-06-18 03:03:28 +00:00
|
|
|
embyconn = kodiSQL('emby')
|
2016-03-31 15:39:00 +00:00
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
emby_dbitem = emby_db.getItem_byKodiId(kodiid, item_type)
|
|
|
|
try:
|
|
|
|
itemid = emby_dbitem[0]
|
|
|
|
except TypeError:
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("No kodiId returned.")
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
|
|
|
url = "{server}/emby/Users/{UserId}/Items/%s?format=json" % itemid
|
|
|
|
result = doUtils.downloadUrl(url)
|
2016-07-25 03:39:40 +00:00
|
|
|
log.debug("Item: %s" % result)
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
playurl = None
|
|
|
|
count = 0
|
|
|
|
while not playurl and count < 2:
|
|
|
|
try:
|
|
|
|
playurl = xbmc.Player().getPlayingFile()
|
|
|
|
except RuntimeError:
|
|
|
|
count += 1
|
|
|
|
xbmc.sleep(200)
|
|
|
|
else:
|
|
|
|
listItem = xbmcgui.ListItem()
|
|
|
|
playback = pbutils.PlaybackUtils(result)
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
if item_type == "song" and settings('streamMusic') == "true":
|
|
|
|
window('emby_%s.playmethod' % playurl, value="DirectStream")
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_%s.playmethod' % playurl, value="DirectPlay")
|
2016-03-31 15:39:00 +00:00
|
|
|
# Set properties for player.py
|
|
|
|
playback.setProperties(playurl, listItem)
|
|
|
|
finally:
|
|
|
|
embycursor.close()
|
|
|
|
|
|
|
|
|
|
|
|
elif method == "VideoLibrary.OnUpdate":
|
|
|
|
# Manually marking as watched/unwatched
|
|
|
|
playcount = data.get('playcount')
|
|
|
|
item = data.get('item')
|
|
|
|
try:
|
|
|
|
kodiid = item['id']
|
|
|
|
item_type = item['type']
|
|
|
|
except (KeyError, TypeError):
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("Item is invalid for playstate update.")
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
|
|
|
# Send notification to the server.
|
2016-06-18 03:03:28 +00:00
|
|
|
embyconn = kodiSQL('emby')
|
2016-03-31 15:39:00 +00:00
|
|
|
embycursor = embyconn.cursor()
|
|
|
|
emby_db = embydb.Embydb_Functions(embycursor)
|
|
|
|
emby_dbitem = emby_db.getItem_byKodiId(kodiid, item_type)
|
|
|
|
try:
|
|
|
|
itemid = emby_dbitem[0]
|
|
|
|
except TypeError:
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("Could not find itemid in emby database.")
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
|
|
|
# Stop from manually marking as watched unwatched, with actual playback.
|
2016-06-18 03:03:28 +00:00
|
|
|
if window('emby_skipWatched%s' % itemid) == "true":
|
2016-03-31 15:39:00 +00:00
|
|
|
# property is set in player.py
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_skipWatched%s' % itemid, clear=True)
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
|
|
|
# notify the server
|
|
|
|
url = "{server}/emby/Users/{UserId}/PlayedItems/%s?format=json" % itemid
|
|
|
|
if playcount != 0:
|
2016-04-04 21:21:05 +00:00
|
|
|
doUtils.downloadUrl(url, action_type="POST")
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("Mark as watched for itemid: %s" % itemid)
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
2016-04-04 21:21:05 +00:00
|
|
|
doUtils.downloadUrl(url, action_type="DELETE")
|
2016-07-25 03:39:40 +00:00
|
|
|
log.info("Mark as unwatched for itemid: %s" % itemid)
|
2016-03-31 15:39:00 +00:00
|
|
|
finally:
|
|
|
|
embycursor.close()
|
|
|
|
|
|
|
|
|
|
|
|
elif method == "VideoLibrary.OnRemove":
|
|
|
|
# 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:
|
|
|
|
kodiid = data['id']
|
|
|
|
type = data['type']
|
|
|
|
except (KeyError, TypeError):
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Item is invalid for emby deletion.", 1)
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
|
|
|
# Send the delete action to the server.
|
|
|
|
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-06-18 03:03:28 +00:00
|
|
|
log("Could not find itemid in emby database.", 1)
|
2016-03-31 15:39:00 +00:00
|
|
|
else:
|
|
|
|
if utils.settings('skipContextMenu') != "true":
|
|
|
|
resp = xbmcgui.Dialog().yesno(
|
|
|
|
heading="Confirm delete",
|
|
|
|
line1="Delete file on Emby Server?")
|
|
|
|
if not resp:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("User skipped deletion.", 1)
|
2016-03-31 15:39:00 +00:00
|
|
|
embycursor.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
url = "{server}/emby/Items/%s?format=json" % itemid
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Deleting request: %s" % itemid)
|
2016-04-04 21:21:05 +00:00
|
|
|
doUtils.downloadUrl(url, action_type="DELETE")
|
2016-03-31 15:39:00 +00:00
|
|
|
finally:
|
|
|
|
embycursor.close()'''
|
|
|
|
|
2016-07-25 03:39:40 +00:00
|
|
|
elif method == "System.OnSleep":
|
|
|
|
# Connection is going to sleep
|
|
|
|
log.info("Marking the server as offline. System.OnSleep activated.")
|
|
|
|
window('emby_online', value="sleep")
|
2016-03-31 15:39:00 +00:00
|
|
|
|
|
|
|
elif method == "System.OnWake":
|
|
|
|
# Allow network to wake up
|
|
|
|
xbmc.sleep(10000)
|
2016-07-25 03:39:40 +00:00
|
|
|
window('emby_online', value="false")
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_onWake', value="true")
|
2016-03-31 15:39:00 +00:00
|
|
|
|
2016-04-29 05:48:09 +00:00
|
|
|
|
|
|
|
elif method == "GUI.OnScreensaverDeactivated":
|
2016-06-18 03:03:28 +00:00
|
|
|
if settings('dbSyncScreensaver') == "true":
|
2016-05-11 05:35:35 +00:00
|
|
|
xbmc.sleep(5000);
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_onWake', value="true")
|
2016-04-29 05:48:09 +00:00
|
|
|
|
|
|
|
|
2016-03-31 15:39:00 +00:00
|
|
|
elif method == "Playlist.OnClear":
|
2016-02-04 01:06:12 +00:00
|
|
|
pass
|