2015-03-23 15:54:16 +11:00
|
|
|
#################################################################################################
|
|
|
|
# WebSocket Client thread
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
|
|
|
|
import json
|
|
|
|
import threading
|
|
|
|
import urllib
|
|
|
|
import socket
|
|
|
|
import websocket
|
2015-04-11 22:56:07 +10:00
|
|
|
|
|
|
|
import KodiMonitor
|
2015-04-29 08:23:26 +10:00
|
|
|
import Utils as utils
|
|
|
|
|
2015-03-23 15:54:16 +11:00
|
|
|
from ClientInformation import ClientInformation
|
|
|
|
from DownloadUtils import DownloadUtils
|
|
|
|
from PlaybackUtils import PlaybackUtils
|
2015-03-26 16:15:45 +11:00
|
|
|
from LibrarySync import LibrarySync
|
2015-05-07 19:45:24 +10:00
|
|
|
from WriteKodiVideoDB import WriteKodiVideoDB
|
2015-05-02 19:56:31 +10:00
|
|
|
from ReadEmbyDB import ReadEmbyDB
|
2015-05-17 01:58:48 +10:00
|
|
|
from ReadKodiDB import ReadKodiDB
|
2015-03-23 15:54:16 +11:00
|
|
|
|
2015-05-22 11:41:11 +10:00
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
|
|
|
|
2015-03-23 15:54:16 +11:00
|
|
|
_MODE_BASICPLAY=12
|
|
|
|
|
|
|
|
class WebSocketThread(threading.Thread):
|
|
|
|
|
2015-04-29 08:23:26 +10:00
|
|
|
_shared_state = {}
|
|
|
|
|
2015-05-14 14:48:35 +10:00
|
|
|
doUtils = DownloadUtils()
|
2015-04-29 08:23:26 +10:00
|
|
|
clientInfo = ClientInformation()
|
|
|
|
KodiMonitor = KodiMonitor.Kodi_Monitor()
|
2015-05-17 15:37:53 +10:00
|
|
|
WINDOW = xbmcgui.Window(10000)
|
|
|
|
|
2015-04-29 08:23:26 +10:00
|
|
|
addonName = clientInfo.getAddonName()
|
|
|
|
|
2015-03-23 15:54:16 +11:00
|
|
|
client = None
|
|
|
|
keepRunning = True
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
2015-04-11 22:56:07 +10:00
|
|
|
|
2015-04-29 08:23:26 +10:00
|
|
|
self.__dict__ = self._shared_state
|
2015-03-23 15:54:16 +11:00
|
|
|
threading.Thread.__init__(self, *args)
|
|
|
|
|
2015-04-29 08:23:26 +10:00
|
|
|
def logMsg(self, msg, lvl=1):
|
2015-03-23 15:54:16 +11:00
|
|
|
|
2015-04-29 08:23:26 +10:00
|
|
|
self.className = self.__class__.__name__
|
|
|
|
utils.logMsg("%s %s" % (self.addonName, self.className), msg, int(lvl))
|
|
|
|
|
|
|
|
def sendProgressUpdate(self, data):
|
|
|
|
self.logMsg("sendProgressUpdate", 1)
|
|
|
|
if self.client:
|
2015-03-23 15:54:16 +11:00
|
|
|
try:
|
2015-04-29 08:23:26 +10:00
|
|
|
# Send progress update
|
|
|
|
messageData = {
|
|
|
|
'MessageType': "ReportPlaybackProgress",
|
|
|
|
'Data': data
|
|
|
|
}
|
2015-03-23 15:54:16 +11:00
|
|
|
messageString = json.dumps(messageData)
|
|
|
|
self.client.send(messageString)
|
2015-04-29 08:23:26 +10:00
|
|
|
self.logMsg("Message data: %s" % messageString, 2)
|
2015-03-23 15:54:16 +11:00
|
|
|
except Exception, e:
|
2015-04-29 08:23:26 +10:00
|
|
|
self.logMsg("Exception: %s" % e, 1)
|
2015-03-23 15:54:16 +11:00
|
|
|
|
|
|
|
def stopClient(self):
|
|
|
|
# stopping the client is tricky, first set keep_running to false and then trigger one
|
|
|
|
# more message by requesting one SessionsStart message, this causes the
|
|
|
|
# client to receive the message and then exit
|
|
|
|
if(self.client != None):
|
|
|
|
self.logMsg("Stopping Client")
|
|
|
|
self.keepRunning = False
|
|
|
|
self.client.keep_running = False
|
|
|
|
self.client.close()
|
|
|
|
self.logMsg("Stopping Client : KeepRunning set to False")
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
self.keepRunning = False
|
|
|
|
self.client.keep_running = False
|
|
|
|
self.logMsg("Stopping Client")
|
|
|
|
self.logMsg("Calling Ping")
|
|
|
|
self.client.sock.ping()
|
|
|
|
|
|
|
|
self.logMsg("Calling Socket Shutdown()")
|
|
|
|
self.client.sock.sock.shutdown(socket.SHUT_RDWR)
|
|
|
|
self.logMsg("Calling Socket Close()")
|
|
|
|
self.client.sock.sock.close()
|
|
|
|
self.logMsg("Stopping Client Done")
|
|
|
|
self.logMsg("Calling Ping")
|
|
|
|
self.client.sock.ping()
|
|
|
|
|
|
|
|
except Exception, e:
|
|
|
|
self.logMsg("Exception : " + str(e), level=0)
|
|
|
|
'''
|
|
|
|
else:
|
|
|
|
self.logMsg("Stopping Client NO Object ERROR")
|
|
|
|
|
|
|
|
def on_message(self, ws, message):
|
2015-03-26 16:15:45 +11:00
|
|
|
self.logMsg("Message : " + str(message), 0)
|
2015-03-23 15:54:16 +11:00
|
|
|
result = json.loads(message)
|
|
|
|
|
|
|
|
messageType = result.get("MessageType")
|
|
|
|
data = result.get("Data")
|
2015-05-16 16:31:08 +10:00
|
|
|
WINDOW = xbmcgui.Window( 10000 )
|
|
|
|
|
2015-05-17 15:37:53 +10:00
|
|
|
if(messageType != None and messageType == "Play" and data != None):
|
2015-03-23 15:54:16 +11:00
|
|
|
itemIds = data.get("ItemIds")
|
|
|
|
playCommand = data.get("PlayCommand")
|
|
|
|
|
|
|
|
if(playCommand != None and playCommand == "PlayNow"):
|
|
|
|
|
|
|
|
xbmc.executebuiltin("Dialog.Close(all,true)")
|
|
|
|
startPositionTicks = data.get("StartPositionTicks")
|
|
|
|
PlaybackUtils().PLAYAllItems(itemIds, startPositionTicks)
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(Playlist: Added " + str(len(itemIds)) + " items to Playlist,)")
|
|
|
|
|
|
|
|
elif(playCommand != None and playCommand == "PlayNext"):
|
|
|
|
|
|
|
|
playlist = PlaybackUtils().AddToPlaylist(itemIds)
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(Playlist: Added " + str(len(itemIds)) + " items to Playlist,)")
|
|
|
|
if(xbmc.Player().isPlaying() == False):
|
|
|
|
xbmc.Player().play(playlist)
|
|
|
|
|
|
|
|
elif(messageType != None and messageType == "Playstate"):
|
|
|
|
command = data.get("Command")
|
|
|
|
if(command != None and command == "Stop"):
|
|
|
|
self.logMsg("Playback Stopped")
|
|
|
|
xbmc.executebuiltin('xbmc.activatewindow(10000)')
|
|
|
|
xbmc.Player().stop()
|
|
|
|
elif(command != None and command == "Pause"):
|
|
|
|
self.logMsg("Playback Paused")
|
|
|
|
xbmc.Player().pause()
|
|
|
|
elif(command != None and command == "Unpause"):
|
|
|
|
self.logMsg("Playback UnPaused")
|
|
|
|
xbmc.Player().pause()
|
|
|
|
elif(command != None and command == "NextTrack"):
|
|
|
|
self.logMsg("Playback NextTrack")
|
|
|
|
xbmc.Player().playnext()
|
|
|
|
elif(command != None and command == "PreviousTrack"):
|
|
|
|
self.logMsg("Playback PreviousTrack")
|
|
|
|
xbmc.Player().playprevious()
|
|
|
|
elif(command != None and command == "Seek"):
|
|
|
|
seekPositionTicks = data.get("SeekPositionTicks")
|
|
|
|
self.logMsg("Playback Seek : " + str(seekPositionTicks))
|
|
|
|
seekTime = (seekPositionTicks / 1000) / 10000
|
|
|
|
xbmc.Player().seekTime(seekTime)
|
2015-03-26 16:15:45 +11:00
|
|
|
|
|
|
|
elif(messageType != None and messageType == "UserDataChanged"):
|
|
|
|
# for now just do a full playcount sync
|
2015-04-03 10:41:39 +11:00
|
|
|
self.logMsg("Message : Doing UserDataChanged", 0)
|
|
|
|
userDataList = data.get("UserDataList")
|
|
|
|
self.logMsg("Message : Doing UserDataChanged : UserDataList : " + str(userDataList), 0)
|
|
|
|
if(userDataList != None):
|
2015-05-09 22:26:33 +10:00
|
|
|
self.user_data_update(userDataList)
|
2015-03-23 15:54:16 +11:00
|
|
|
|
2015-04-03 13:12:09 +11:00
|
|
|
elif(messageType != None and messageType == "LibraryChanged"):
|
|
|
|
foldersAddedTo = data.get("FoldersAddedTo")
|
|
|
|
foldersRemovedFrom = data.get("FoldersRemovedFrom")
|
|
|
|
|
|
|
|
# doing items removed
|
|
|
|
itemsRemoved = data.get("ItemsRemoved")
|
|
|
|
itemsAdded = data.get("ItemsAdded")
|
|
|
|
itemsUpdated = data.get("ItemsUpdated")
|
|
|
|
itemsToUpdate = itemsAdded + itemsUpdated
|
2015-04-19 03:10:06 +10:00
|
|
|
self.logMsg("Message : WebSocket LibraryChanged : Items Added : " + str(itemsAdded), 0)
|
|
|
|
self.logMsg("Message : WebSocket LibraryChanged : Items Updated : " + str(itemsUpdated), 0)
|
|
|
|
self.logMsg("Message : WebSocket LibraryChanged : Items Removed : " + str(itemsRemoved), 0)
|
|
|
|
|
2015-05-13 05:27:27 +10:00
|
|
|
self.remove_items(itemsRemoved)
|
|
|
|
self.update_items(itemsToUpdate)
|
2015-04-19 03:10:06 +10:00
|
|
|
|
2015-05-09 17:17:58 +10:00
|
|
|
elif messageType == "GeneralCommand":
|
|
|
|
|
2015-05-16 16:31:08 +10:00
|
|
|
command = data.get("Name")
|
|
|
|
arguments = data.get("Arguments")
|
|
|
|
|
|
|
|
commandsPlayback = [
|
|
|
|
'Mute','Unmute','SetVolume',
|
|
|
|
'SetAudioStreamIndex'
|
|
|
|
]
|
|
|
|
|
|
|
|
if command in commandsPlayback:
|
|
|
|
# These commands need to be reported back
|
|
|
|
if command == "Mute":
|
|
|
|
xbmc.executebuiltin('Mute')
|
|
|
|
elif command == "Unmute":
|
|
|
|
xbmc.executebuiltin('Mute')
|
|
|
|
elif command == "SetVolume":
|
|
|
|
volume = arguments[u'Volume']
|
|
|
|
xbmc.executebuiltin('SetVolume(%s[,showvolumebar])' % volume)
|
|
|
|
# Report playback
|
|
|
|
WINDOW.setProperty('commandUpdate', 'true')
|
|
|
|
|
|
|
|
else:
|
|
|
|
# GUI commands
|
|
|
|
if command == "ToggleFullscreen":
|
|
|
|
xbmc.executebuiltin('Action(FullScreen)')
|
|
|
|
elif command == "ToggleOsdMenu":
|
|
|
|
xbmc.executebuiltin('Action(OSD)')
|
|
|
|
elif command == "MoveUp":
|
|
|
|
xbmc.executebuiltin('Action(Up)')
|
|
|
|
elif command == "MoveDown":
|
|
|
|
xbmc.executebuiltin('Action(Down)')
|
|
|
|
elif command == "MoveLeft":
|
|
|
|
xbmc.executebuiltin('Action(Left)')
|
|
|
|
elif command == "MoveRight":
|
|
|
|
xbmc.executebuiltin('Action(Right)')
|
|
|
|
elif command == "Select":
|
|
|
|
xbmc.executebuiltin('Action(Select)')
|
|
|
|
elif command == "Back":
|
|
|
|
xbmc.executebuiltin('Action(back)')
|
|
|
|
elif command == "ToggleContextMenu":
|
|
|
|
xbmc.executebuiltin('Action(ContextMenu)')
|
|
|
|
elif command == "GoHome":
|
|
|
|
xbmc.executebuiltin('ActivateWindow(Home)')
|
|
|
|
elif command == "PageUp":
|
|
|
|
xbmc.executebuiltin('Action(PageUp)')
|
|
|
|
elif command == "NextLetter":
|
|
|
|
xbmc.executebuiltin('Action(NextLetter)')
|
|
|
|
elif command == "GoToSearch":
|
|
|
|
xbmc.executebuiltin('VideoLibrary.Search')
|
|
|
|
elif command == "GoToSettings":
|
|
|
|
xbmc.executebuiltin('ActivateWindow(Settings)')
|
|
|
|
elif command == "PageDown":
|
|
|
|
xbmc.executebuiltin('Action(PageDown)')
|
|
|
|
elif command == "PreviousLetter":
|
|
|
|
xbmc.executebuiltin('Action(PrevLetter)')
|
|
|
|
elif command == "TakeScreenshot":
|
|
|
|
xbmc.executebuiltin('TakeScreenshot')
|
|
|
|
elif command == "ToggleMute":
|
|
|
|
xbmc.executebuiltin('Mute')
|
|
|
|
elif command == "VolumeUp":
|
|
|
|
xbmc.executebuiltin('Action(VolumeUp)')
|
|
|
|
elif command == "VolumeDown":
|
|
|
|
xbmc.executebuiltin('Action(VolumeDown)')
|
|
|
|
elif command == "DisplayMessage":
|
|
|
|
header = arguments[u'Header']
|
|
|
|
text = arguments[u'Text']
|
2015-05-19 08:21:07 +10:00
|
|
|
xbmcgui.Dialog().notification(header, text, time=4000)
|
2015-05-16 16:31:08 +10:00
|
|
|
elif command == "SendString":
|
|
|
|
string = arguments[u'String']
|
2015-05-16 17:11:41 +10:00
|
|
|
text = '{"jsonrpc": "2.0", "method": "Input.SendText", "params": { "text": "%s", "done": false }, "id": 0}' % string
|
|
|
|
result = xbmc.executeJSONRPC(text)
|
2015-05-16 16:31:08 +10:00
|
|
|
else:
|
|
|
|
self.logMsg("Unknown command.", 1)
|
2015-05-09 17:17:58 +10:00
|
|
|
|
2015-04-19 03:10:06 +10:00
|
|
|
def remove_items(self, itemsRemoved):
|
2015-05-08 08:23:49 +10:00
|
|
|
|
|
|
|
#Process video library
|
|
|
|
connection = utils.KodiSQL("video")
|
2015-05-03 06:46:12 +10:00
|
|
|
cursor = connection.cursor()
|
2015-04-19 03:10:06 +10:00
|
|
|
for item in itemsRemoved:
|
2015-05-17 01:58:48 +10:00
|
|
|
type=ReadKodiDB().getTypeByEmbyId(item, connection, cursor)
|
|
|
|
self.logMsg("Type: " + str(type))
|
2015-05-08 08:23:49 +10:00
|
|
|
self.logMsg("Message : Doing LibraryChanged : Items Removed : Calling deleteItemFromKodiLibrary: " + item, 0)
|
2015-05-17 01:58:48 +10:00
|
|
|
if type == "episode":
|
|
|
|
showId=ReadKodiDB().getShowIdByEmbyId(item, connection, cursor) # Get the TV Show ID
|
|
|
|
self.logMsg("ShowID: " + str(showId),0)
|
2015-05-07 19:45:24 +10:00
|
|
|
WriteKodiVideoDB().deleteItemFromKodiLibrary(item, connection, cursor)
|
2015-05-17 01:58:48 +10:00
|
|
|
if type == "episode":
|
|
|
|
showTotalCount = ReadKodiDB().getShowTotalCount(showId, connection, cursor) # Check if there are no episodes left
|
|
|
|
self.logMsg("ShowTotalCount: " + str(showTotalCount),0)
|
|
|
|
if showTotalCount == 0 or showTotalCount == None: # Delete show if no episodes are left
|
|
|
|
embyId=ReadKodiDB().getEmbyIdByKodiId(showId, "tvshow", connection, cursor)
|
|
|
|
self.logMsg("Message : Doing LibraryChanged : Deleting show:" + embyId, 0)
|
|
|
|
WriteKodiVideoDB().deleteItemFromKodiLibrary(embyId, connection, cursor)
|
2015-05-17 02:04:09 +10:00
|
|
|
connection.commit()
|
2015-05-03 06:46:12 +10:00
|
|
|
cursor.close()
|
2015-05-08 08:23:49 +10:00
|
|
|
|
|
|
|
#Process music library
|
|
|
|
addon = xbmcaddon.Addon(id='plugin.video.emby')
|
|
|
|
if addon.getSetting("enableMusicSync") == "true":
|
|
|
|
connection = utils.KodiSQL("music")
|
|
|
|
cursor = connection.cursor()
|
|
|
|
for item in itemsRemoved:
|
|
|
|
self.logMsg("Message : Doing LibraryChanged : Items Removed : Calling deleteItemFromKodiLibrary (musiclibrary): " + item, 0)
|
|
|
|
WriteKodiMusicDB().deleteItemFromKodiLibrary(item, connection, cursor)
|
|
|
|
connection.commit()
|
|
|
|
cursor.close()
|
2015-04-19 03:10:06 +10:00
|
|
|
|
|
|
|
def update_items(self, itemsToUpdate):
|
|
|
|
# doing adds and updates
|
|
|
|
if(len(itemsToUpdate) > 0):
|
|
|
|
self.logMsg("Message : Doing LibraryChanged : Processing Added and Updated : " + str(itemsToUpdate), 0)
|
2015-05-02 19:56:31 +10:00
|
|
|
LibrarySync().IncrementalSync(itemsToUpdate)
|
2015-04-19 03:10:06 +10:00
|
|
|
|
2015-04-22 23:24:54 +10:00
|
|
|
def user_data_update(self, userDataList):
|
2015-05-02 19:56:31 +10:00
|
|
|
itemsToUpdate = list()
|
2015-04-19 03:10:06 +10:00
|
|
|
for userData in userDataList:
|
|
|
|
itemId = userData.get("ItemId")
|
|
|
|
if(itemId != None):
|
2015-05-02 19:56:31 +10:00
|
|
|
itemsToUpdate.append(itemId)
|
|
|
|
if(len(itemsToUpdate) > 0):
|
|
|
|
self.logMsg("Message : Doing UserDataChanged : Processing Updated : " + str(itemsToUpdate), 0)
|
|
|
|
LibrarySync().IncrementalSync(itemsToUpdate)
|
2015-04-19 03:10:06 +10:00
|
|
|
|
2015-03-23 15:54:16 +11:00
|
|
|
def on_error(self, ws, error):
|
2015-05-03 15:27:43 +10:00
|
|
|
if "10061" in str(error):
|
|
|
|
# Server is offline
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.logMsg("Error: %s" % error, 1)
|
2015-03-23 15:54:16 +11:00
|
|
|
#raise
|
|
|
|
|
|
|
|
def on_close(self, ws):
|
2015-05-03 15:27:43 +10:00
|
|
|
self.logMsg("Closed", 2)
|
2015-03-23 15:54:16 +11:00
|
|
|
|
|
|
|
def on_open(self, ws):
|
2015-04-29 08:23:26 +10:00
|
|
|
pass
|
2015-03-23 15:54:16 +11:00
|
|
|
|
|
|
|
def run(self):
|
2015-04-29 08:23:26 +10:00
|
|
|
|
2015-04-14 04:56:36 +10:00
|
|
|
WINDOW = xbmcgui.Window(10000)
|
2015-04-29 08:23:26 +10:00
|
|
|
logLevel = int(WINDOW.getProperty('logLevel'))
|
2015-04-14 04:56:36 +10:00
|
|
|
username = WINDOW.getProperty('currUser')
|
|
|
|
server = WINDOW.getProperty('server%s' % username)
|
2015-04-29 08:23:26 +10:00
|
|
|
token = WINDOW.getProperty('accessToken%s' % username)
|
|
|
|
deviceId = ClientInformation().getMachineId()
|
|
|
|
|
|
|
|
if (logLevel == 2):
|
2015-03-23 15:54:16 +11:00
|
|
|
websocket.enableTrace(True)
|
2015-04-29 08:23:26 +10:00
|
|
|
|
|
|
|
# Get the appropriate prefix for websocket
|
2015-04-14 04:56:36 +10:00
|
|
|
if "https" in server:
|
2015-04-29 08:23:26 +10:00
|
|
|
server = server.replace('https', 'wss')
|
2015-04-14 04:56:36 +10:00
|
|
|
else:
|
2015-04-29 08:23:26 +10:00
|
|
|
server = server.replace('http', 'ws')
|
|
|
|
|
|
|
|
websocketUrl = "%s?api_key=%s&deviceId=%s" % (server, token, deviceId)
|
|
|
|
self.logMsg("websocket URL: %s" % websocketUrl)
|
|
|
|
|
|
|
|
self.client = websocket.WebSocketApp(websocketUrl,
|
2015-03-23 15:54:16 +11:00
|
|
|
on_message = self.on_message,
|
|
|
|
on_error = self.on_error,
|
|
|
|
on_close = self.on_close)
|
|
|
|
|
|
|
|
self.client.on_open = self.on_open
|
|
|
|
|
2015-04-11 22:56:07 +10:00
|
|
|
while not self.KodiMonitor.abortRequested():
|
2015-05-03 15:27:43 +10:00
|
|
|
|
2015-05-14 14:48:35 +10:00
|
|
|
if WINDOW.getProperty("Server_online") == "true":
|
|
|
|
# Server came back online, repost capabilities
|
|
|
|
self.doUtils.postCapabilities(deviceId)
|
|
|
|
self.client.run_forever()
|
2015-05-03 15:27:43 +10:00
|
|
|
|
|
|
|
if (self.keepRunning):
|
|
|
|
# Server is not online
|
|
|
|
if WINDOW.getProperty("Server_online") == "true":
|
|
|
|
self.logMsg("Server is unreachable.", 1)
|
|
|
|
WINDOW.setProperty("Server_online", "false")
|
2015-05-14 15:11:15 +10:00
|
|
|
xbmcgui.Dialog().notification("Error connecting", "%s Server is unreachable." % self.addonName)
|
2015-05-03 15:27:43 +10:00
|
|
|
|
2015-04-12 13:01:44 +10:00
|
|
|
if self.KodiMonitor.waitForAbort(5):
|
2015-04-11 22:56:07 +10:00
|
|
|
break
|
2015-05-03 15:27:43 +10:00
|
|
|
|
2015-03-23 15:54:16 +11:00
|
|
|
self.logMsg("Thread Exited")
|
2015-05-09 22:26:33 +10:00
|
|
|
|