Created
May 14, 2015 12:10
-
-
Save HappyCodingRobot/974f6a1321f3f05dd26f to your computer and use it in GitHub Desktop.
Simple XBMC/Kodi controller via http POST request and JSONRPC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Hi,
I found your program extemly usefull and many thanks.
I have just a small remarque regarding Basic authentication
I google for it for a verly long time.
So it's possible to make it working that way
def send_request(req):
req_par = requ_obj.copy()
req_par.update(cmd[req])
params = json.dumps(req_par)
**# we need to base 64 encode it
creds = b64encode("user:password")
headers = {'Authorization': 'Basic %s' % creds}**
con.request("POST", "/jsonrpc", params, headers)
Best regards
and many thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I changed the set commands to use shownotification like:
"pop": {"method":"GUI.ShowNotification", "params": {"displaytime":10000,"title":"SabNZBd bericht", "message": "test geslaagd", "image":"http://www.newsservers.net/wp-content/uploads/2016/11/9GVQBuy.png" }}
But cant figure out how to give the params in a cmd line
I tryed popup.py pop 10000,'title name','message text'
but stil get the predefined message from above line of code.
any help what i am doing wrong ?