Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple XBMC/Kodi controller via http POST request and JSONRPC
#!/usr/bin/python
'''
Very simple XBMC/Kodi controller via http POST request and JSONRPC
(testing the principle..)
'''
import httplib
import sys, argparse
import json
PI = '192.168.162.33'
PORT = 80
version = 'v0.5.1'
headers = {"Content-type": "application/json", "Accept": "text/plain"}
#headers = {"Content-type": "application/json; charset=UTF-8"}
requ_obj = {"jsonrpc": "2.0", "id": 1, "method": "", "params":{}}
con = httplib.HTTPConnection(PI, PORT, timeout=5)
#req_vol = {"jsonrpc": "2.0", "method": "Application.GetProperties", "params": {"properties": ["volume"]}, "id": 1}
#req_home = {"jsonrpc": "2.0", "method": "Input.home", "id": 1}
cmd = { "get_vol": {"method": "Application.GetProperties", "params": {"properties": ["volume"]}},
"set_vol": {"method": "Application.SetVolume", "params": {"volume":100}},
"mute": {"method": "Application.SetMute", "params":{"mute":"toggle"}},
"pause": {"method": "Player.PlayPause", "params": { "playerid": 1 }},
"stop": {"method": "Player.Stop", "params": { "playerid": 1 }},
"pos": {"method": "Player.GetProperties", "params": {"properties": ["percentage"], "playerid": 1}},
"home": {"method": "Input.home"},
"back": {"method":"Input.Back"},
"reboot": {"method":"System.Reboot"},
"off": {"method":"System.Shutdown"}
}
descr = {"get_vol": "returns actual volume",
"set_vol": "set volume (0..100)",
"mute": "toggle mute",
"pause": "pause player",
"stop": "stop playing",
"pos": "show actual position in %",
"home": "menu: returns to home screen",
"back": "menu: one step back",
"reboot": "reboot raspberry pi",
"off": "shutdown raspberry pi"
}
def send_request(req):
req_par = requ_obj.copy()
req_par.update(cmd[req])
params = json.dumps(req_par)
con.request("POST", "/jsonrpc", params, headers)
resp = con.getresponse()
if resp.status==200:
ret = json.loads(resp.read())
#print(ret)
if ret['jsonrpc']==u'2.0':
if 'return' in ret:
print(ret['return'])
if 'result' in ret:
print(ret['result'])
if 'error' in ret:
print('!error:')
print(ret['error'])
else:
print('!ERROR: no jsonrpc object.')
print(ret)
else:
print(resp.status, resp.reason)
def print_help():
print(' RPi/kodi cmd tool %s' %version)
print('# Not a valid command or missing command!')
print(' use:\n %s [cmd] <par>\n' % sys.argv[0])
print('valid commands:')
for c in cmd.keys():
print(' %s - %s' % (c,descr[c]) )
print('\noptional parameter:\n e.g. %s set_vol 80\n' % sys.argv[0])
def main():
'''Where everything starts'''
if (len(sys.argv)<=1) or (sys.argv[1].lower() not in cmd.keys()):
print_help()
sys.exit(1)
try:
# only to check if connected, request works without
con.connect()
except Exception, e:
print('Failed to connect:')
print(e)
sys.exit(2)
if (len(sys.argv)>2) and sys.argv[2].isdigit():
if sys.argv[1]=='set_vol':
val = int(sys.argv[2])
if val>100:
val=100;
cmd['set_vol']['params']['volume']=val
send_request(sys.argv[1])
con.close()
if __name__ == '__main__':
#sys.argv.extend(['get_vol','72'])
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.