2015-02-11, 04:56
is this the best way to listen for update to the video database? and update a bunch of kodi instance that might be running? bit of kludge I think espcially going to settings and coming back to home. also this only does it if the user is on the home screen, if he isn't navigating back to home should trigger a refresh
any ideas are welcome.
any ideas are welcome.
Code:
#!/usr/bin/python
import websocket
import thread
import threading
import time
import json
from xbmcjson import XBMC, PLAYER_VIDEO
import logging
import logging.handlers
import sys
logger = logging.getLogger('MyLogger')
logger.setLevel(logging.INFO)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
logger.addHandler(handler)
logging.basicConfig()
hosts = ['host1:8080','host2:8080','host3:8080']
def xbmcnotify_thread(host):
try:
xbmc = XBMC("http://" + host + "/jsonrpc")
myjson = xbmc.Player.GetActivePlayers()
window = xbmc.GUI.GetProperties({"properties":["currentwindow"]})
if (not myjson['result']) and (window['result']['currentwindow']['id'] == 10000):
xbmc.GUI.ActivateWindow({"window":"video"})
xbmc.GUI.ActivateWindow({"window":"home"})
except urllib2.URLError as e :
if not e.reason.startswith('[Errno 113]'):
logger.critical("failed to notify kodi due to %s" % (e.reason))
finally:
xbmc = None
sys.exit(0)
def on_message(ws, message):
json_string = json.loads(message)
#print json_string['method']
if json_string['method'] == 'VideoLibrary.OnUpdate':
for host in hosts:
logging.debug("Updating %s" % host)
threading.Thread(target=xbmcnotify_thread,args=[host]).start()
def on_error(ws, error):
print error
def on_close(ws):
logging.info("Stopped watching KODI")
def on_open(ws):
def run(*args):
logging.info("Looking for VideoLibrary.onUpdate on local KODI")
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(False)
ws = websocket.WebSocketApp("ws://127.0.0.1:9090/jsonrpc",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()