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))
     if not requests.post(self.host, self.port, "/:/timeline", msg, getPlexHeaders(), self.protocol):
         subMgr.removeSubscriber(self.uuid)
 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))
     if not requests.post(self.host, self.port, "/:/timeline", msg,
                          getPlexHeaders(), self.protocol):
         subMgr.removeSubscriber(self.uuid)
def jsonrpc(action, arguments = {}):
    """ put some JSON together for the JSON-RPC APIv6 """
    if action.lower() == "sendkey":
        request=json.dumps({ "jsonrpc" : "2.0" , "method" : "Input.SendText", "params" : { "text" : self.arguments[0], "done" : False }} )
    elif action.lower() == "ping":
        request=json.dumps({ "jsonrpc" : "2.0",
                             "id" : 1 ,
                             "method"  : "JSONRPC.Ping" })
    elif action.lower() == "playmedia":
        fullurl=arguments[0]
        resume=arguments[1]
        request=json.dumps({ "id"      : 1,
                             "jsonrpc" : "2.0",
                             "method"  : "Player.Open",
                             "params"  : { "item"  :  {"file":"plugin://plugin.video.plexbmc/?mode=5&force="+resume+"&url="+fullurl } } })
    elif arguments:
        request=json.dumps({ "id" : 1,
                             "jsonrpc" : "2.0",
                             "method"  : action,
                             "params"  : arguments})
    else:
        request=json.dumps({ "id" : 1,
                             "jsonrpc" : "2.0",
                             "method"  : action})
    
    printDebug("Sending request to XBMC: %s" % request)
    jsonraw = requests.post(
        "127.0.0.1", 
        settings['port'], 
        "/jsonrpc", 
        request, 
        { 'Content-Type' : 'application/json',
          'Authorization' : 'Basic ' + string.strip(base64.encodestring(settings['user'] + ':' + settings['passwd'])) })
                
    """ parse the request """
    if not jsonraw:
        printDebug("Empty response from XBMC")
        return False
    else:
        printDebug("Response from XBMC: %s" % jsonraw)
        parsed=json.loads(jsonraw)
        
    if parsed.get('error', False):
        print "XBMC returned an error: %s" % parsed.get('error')                

    return parsed.get('result', False)
def jsonrpc(action, arguments = {}):
    """ put some JSON together for the JSON-RPC APIv6 """
    if action.lower() == "sendkey":
        request=json.dumps({ "jsonrpc" : "2.0" , "method" : "Input.SendText", "params" : { "text" : self.arguments[0], "done" : False }} )
    elif action.lower() == "ping":
        request=json.dumps({ "jsonrpc" : "2.0",
                             "id" : 1 ,
                             "method"  : "JSONRPC.Ping" })
    elif action.lower() == "playmedia":
        fullurl=arguments[0]
        resume=arguments[1]
        xbmc.Player().play("plugin://plugin.video.plexbmc/?mode=5&force="+resume+"&url="+fullurl)
        return True
    elif arguments:
        request=json.dumps({ "id" : 1,
                             "jsonrpc" : "2.0",
                             "method"  : action,
                             "params"  : arguments})
    else:
        request=json.dumps({ "id" : 1,
                             "jsonrpc" : "2.0",
                             "method"  : action})
    
    printDebug("Sending request to XBMC without network stack: %s" % request)
    result = parseJSONRPC(xbmc.executeJSONRPC(request))

    if not result and settings['webserver_enabled']:
        # xbmc.executeJSONRPC appears to fail on the login screen, but going
        # through the network stack works, so let's try the request again
        result = parseJSONRPC(requests.post(
            "127.0.0.1",
            settings['port'],
            "/jsonrpc",
            request,
            { 'Content-Type' : 'application/json',
              'Authorization' : 'Basic ' + string.strip(base64.encodestring(settings['user'] + ':' + settings['passwd'])) }))

    return result
def jsonrpc(action, arguments={}):
    """ put some JSON together for the JSON-RPC APIv6 """
    if action.lower() == "sendkey":
        request = json.dumps({
            "jsonrpc": "2.0",
            "method": "Input.SendText",
            "params": {
                "text": self.arguments[0],
                "done": False
            }
        })
    elif action.lower() == "ping":
        request = json.dumps({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "JSONRPC.Ping"
        })
    elif action.lower() == "playmedia":
        fullurl = arguments[0]
        resume = arguments[1]
        request = json.dumps({
            "id": 1,
            "jsonrpc": "2.0",
            "method": "Player.Open",
            "params": {
                "item": {
                    "file":
                    "plugin://plugin.video.plexbmc/?mode=5&force=" + resume +
                    "&url=" + fullurl
                }
            }
        })
    elif arguments:
        request = json.dumps({
            "id": 1,
            "jsonrpc": "2.0",
            "method": action,
            "params": arguments
        })
    else:
        request = json.dumps({"id": 1, "jsonrpc": "2.0", "method": action})

    printDebug("Sending request to XBMC: %s" % request)
    jsonraw = requests.post(
        "127.0.0.1", settings['port'], "/jsonrpc", request, {
            'Content-Type':
            'application/json',
            'Authorization':
            'Basic ' + string.strip(
                base64.encodestring(settings['user'] + ':' +
                                    settings['passwd']))
        })
    """ parse the request """
    if not jsonraw:
        printDebug("Empty response from XBMC")
        return False
    else:
        printDebug("Response from XBMC: %s" % jsonraw)
        parsed = json.loads(jsonraw)

    if parsed.get('error', False):
        print "XBMC returned an error: %s" % parsed.get('error')

    return parsed.get('result', False)