Example JSON client in Python?
#1
Question 
I'd like to get started with using the JSON API but I don't know where to start. Has anyone got simple example code (Python would be nice) to show me how to send XBMC an instruction using JSON?

Thanks,

JR
Reply
#2
The nice part is that there is already a call from python to do XBMC's JSON RPC calls that is part of the xbmc module.

Code:
xbmc.executeJSONRPC()

Building the JSON call can be a little tricky, but gets easier as you use them.

My script uses the following:

Code:
json_album = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "AudioLibrary.GetAlbums", "id": 1}')

to retrieve all the albums and album id's from the music library. It's a very simple call with without any parameters. All I do after this is parse the return which is stored in json_album.

The thread http://forum.xbmc.org/showthread.php?tid=68263 has page link to http://pastebin.com/m4f937676 which is an older list of the commands available. You can obtain a newer list(valid list to the version of XBMC you are using) by adding to your script:

Code:
print xbmcexecute.JSONRPC('{"jsonrpc": "2.0", "method": "JSONRPC.Introspect", "id": 1}')

then look in the xbmc.log and you will see a list print out similar to this:

Code:
{
                                               "id" : 1,
                                               "jsonrpc" : "2.0",
                                               "result" : {
                                                  "commands" : [
                                                     {
                                                        "command" : "JSONRPC.Introspect",
                                                        "description" : "Enumerates all actions and descriptions. Parameter example {\"getdescriptions\": true, \"getpermissions\": true, \"filterbytransport\": true }. All parameters optional",
                                                        "executable" : true,
                                                        "permission" : "ReadData"
                                                     },
                                                     {
                                                        "command" : "JSONRPC.Version",
                                                        "description" : "Retrieve the jsonrpc protocol version",
                                                        "executable" : true,
                                                        "permission" : "ReadData"
                                                     },
                                                     {
                                                        "command" : "JSONRPC.Permission",
                                                        "description" : "Retrieve the clients permissions",
                                                        "executable" : true,
                                                        "permission" : "ReadData"
                                                     },

it will be a long list(copy and paste to notepad and save a copy.)

I can help further, just not sure yet what you want to be able to do from your addon...
Reply
#3
Great response, I wish I had a resource like that when I had started. I'll throw in that an easy way to hit the server and test different JSON calls is with the Simple REST Client for Google Chrome. https://chrome.google.com/extensions/det...cjmb?hl=en

Set the url to http://localhost:8080/jsonrpc, method to POST, and data to the JSON you want. I've found this a quick way to test queries.
Image
Reply
#4
Well I just learned something new today... Thanks for the extra knowledge(about the google chrome plugin. ) Saves me some time and testing.
Reply
#5
Thanks both.

@giftie: presumably the executeJSONRPC command is XBMC specific, which isn't a lot of use as all it can do is get XBMC to remote control itself :-) What I was really after was a Python script I can run from a different computer. I can see that xbmc.executeJSONRPC is very useful for testing the JSON commands, but I'm also interested in how the protocol works at the sockets level, which brings me to ...

@TheDuffMan: thanks for the idea. I didn't realise the JSON stuff was just a HTTP POST: I expected something more complicated! There are lots of utilities for sending POST requests so I'll have a play. One thing you don't mention: what is the name of the POST parameter? Presumably I can just do:

wwwpost localhostConfusedomeport /jsonrpc paramname=lots+of+JSON+code

(where wwwpost is a command line HTTP POST applet) but what is the parameter name? Also what port should I use? Is it the same port that I've configured the web server to listen on? My XBMC isn't listening on port 8080.

Thanks again,

JR
Reply
#6
Aha, I've managed to figure out how it works from various comments in this forum. If anyone is interested, this Python code works:

Code:
_MYXBMCADDR = "192.168.128.128"
_MYXBMCPORT = 81

import socket
import select

#create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#now connect to rattle
s.connect((_MYXBMCADDR, _MYXBMCPORT))

# Send an Introspect command
s.send("POST /jsonrpc HTTP/1.1\x0D\x0A")
s.send("\x0D\x0A")
s.send("{\"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Introspect\", \"id\": 1}")

# Print the results
while True:
    print s.recv(0x4000)

    if len(select.select([s], [], [], 0)[0]) == 0:
        break;

# Finished
s.shutdown(socket.SHUT_RDWR)
s.close()

To use the raw interface it's even simpler:

Code:
_MYXBMCADDR = "192.168.128.128"
_MYXBMCPORT = 9090

import socket
import select

#create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#now connect to rattle
s.connect((_MYXBMCADDR, _MYXBMCPORT))

# Send an Introspect command
s.send("{\"jsonrpc\": \"2.0\", \"method\": \"JSONRPC.Introspect\", \"id\": 1}")

# Print the results
while True:
    print s.recv(0x4000)

    if len(select.select([s], [], [], 0)[0]) == 0:
        break;

# Finished
s.shutdown(socket.SHUT_RDWR)
s.close()

JR
Reply
#7
There are a few JSON RPC librarys out there for python , I would reccomend using one of those instead of messing about with sockets yourself.
Reply
#8
Just asking for clarification, there is currently no python library for using xbmc's json-rpc over http? If one wants to use the json-rpc api over http they have to using a python json-rpc and write the code from scratch?
Reply
#9
FYI: if using the rest client recommended above the header must be set to:
Content-Type: application/json

thank you for the tips Smile
Reply
#10
(2010-12-31, 09:05)konetzed Wrote: Just asking for clarification, there is currently no python library for using xbmc's json-rpc over http? If one wants to use the json-rpc api over http they have to using a python json-rpc and write the code from scratch?

you can use the urllib library.

This is how I use it:

Code:
result = simplejson.load(urllib2.urlopen(url))

where the url is the http formatted json string query
Reply

Logout Mark Read Team Forum Stats Members Help
Example JSON client in Python?0