import re import threading import xbmcgui from xml.dom.minidom import parseString from functions import * from settings import settings from httppersist import requests import downloadutils class SubscriptionManager: def __init__(self): self.subscribers = {} self.info = {} self.lastkey = "" self.lastratingkey = "" self.volume = 0 self.guid = "" self.server = "" self.protocol = "http" self.port = "" self.playerprops = {} self.sentstopped = True self.download = downloadutils.DownloadUtils() def getVolume(self): self.volume = getVolume() def msg(self, players): msg = getXMLHeader() msg += ' 30: sub.cleanup() del self.subscribers[sub.uuid] def getPlayerProperties(self, playerid): info = {} try: # get info from the player props = jsonrpc("Player.GetProperties", {"playerid": playerid, "properties": ["time", "totaltime", "speed", "shuffled"]}) printDebug(jsonrpc("Player.GetItem", {"playerid": playerid, "properties": ["file", "showlink", "episode", "season"]})) info['time'] = timeToMillis(props['time']) info['duration'] = timeToMillis(props['totaltime']) info['state'] = ("paused", "playing")[int(props['speed'])] info['shuffle'] = ("0","1")[props.get('shuffled', False)] except: info['time'] = 0 info['duration'] = 0 info['state'] = "stopped" info['shuffle'] = False # get the volume from the application info['volume'] = self.volume info['guid'] = self.guid return info class Subscriber: def __init__(self, protocol, host, port, uuid, commandID): self.protocol = protocol or "http" self.host = host self.port = port or 32400 self.uuid = uuid or host self.commandID = int(commandID) or 0 self.navlocationsent = False self.age = 0 self.download = downloadutils.DownloadUtils() def __eq__(self, other): return self.uuid == other.uuid def tostr(self): return "uuid=%s,commandID=%i" % (self.uuid, self.commandID) def cleanup(self): requests.closeConnection(self.protocol, self.host, self.port) def send_update(self, msg, is_nav): self.age += 1 if not is_nav: self.navlocationsent = False elif self.navlocationsent: return True else: self.navlocationsent = True msg = re.sub(r"INSERTCOMMANDID", str(self.commandID), msg) printDebug("sending xml to subscriber %s: %s" % (self.tostr(), msg)) url = self.protocol + '://' + self.host + ':' + self.port \ + "/:/timeline" # Override some headers headerOptions = { 'Accept': '*/*' } response = self.download.downloadUrl( url, postBody=msg, type="POSTXML", headerOptions=headerOptions) # if not requests.post(self.host, self.port, "/:/timeline", msg, getPlexHeaders(), self.protocol): # subMgr.removeSubscriber(self.uuid) if response in [False, 401]: subMgr.removeSubscriber(self.uuid) subMgr = SubscriptionManager()