Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #!/bin/env/python
  2. import json
  3. import urllib2
  4. import base64
  5. import time
  6. import xbmc
  7.  
  8. # Setings
  9. # The IP address for the XBMC instance you want to talk to
  10. ip = 'localhost'
  11. # The port number XBMC's web interface is listening on
  12. port = '8080'
  13. # The username on XBMC's web interface (just comment or delete this line if you don't use authentication
  14. username = 'xbmc'
  15. # Same as the username.
  16. password = 'xbmc'
  17.  
  18. # Here you specify the method and parameters you want to pass to the XBMC JSON API
  19. # For a LOT of info on the kinds of things you can do with the interface go here:
  20. # http://wiki.xbmc.org/index.php?title=JSON-RPC_API/v6
  21. # Here's an example of just sending an on-screen notification. This should help you understand the syntax
  22. # method = 'GUI.ShowNotification'
  23. # parameters = {"title":"Hello There!", "message":"This is a notification!", "displaytime":3000}
  24. # This is what I am actually doing with this script, running the Artwork Downloader.
  25. # Note: I am using the "silent" mode to avoid having a pop-up dialog box that would need to be closed.
  26. # Also note: this stuff is very syntax-specific. Boolean and Int values must not be quoted. Strings must be doublequoted.
  27. method = 'Settings.SetSettingValue'
  28. parameters = {"setting":"videoscreen.resolution","value":65}
  29. #parameters = {"setting":"videoscreen.resolution","value":16}
  30.  
  31. # This is a single, reusable method that makes a call to XBMC and gives you back the response
  32.  
  33. def getJsonRemote(host,port,username,password,method,parameters):
  34. # First we build the URL we're going to talk to
  35. url = 'http://%s:%s/jsonrpc' %(host, port)
  36. # Next we'll build out the Data to be sent
  37. values ={}
  38. values["jsonrpc"] = "2.0"
  39. values["method"] = method
  40. # This fork handles instances where no parameters are specified
  41. if parameters:
  42. values["params"] = parameters
  43. values["id"] = "1"
  44. headers = {"Content-Type":"application/json",}
  45. # Format the data
  46. data = json.dumps(values)
  47. # Now we're just about ready to actually initiate the connection
  48. req = urllib2.Request(url, data, headers)
  49. # This fork kicks in only if both a username & password are provided
  50. if username and password:
  51. # This properly formats the provided username & password and adds them to the request header
  52. base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
  53. req.add_header("Authorization", "Basic %s" % base64string)
  54. # Now we're ready to talk to XBMC
  55. # I wrapped this up in a try: statement to allow for graceful error handling
  56. try:
  57. response = urllib2.urlopen(req)
  58. response = response.read()
  59. response = json.loads(response)
  60. # A lot of the XBMC responses include the value "result", which lets you know how your call went
  61. # This logic fork grabs the value of "result" if one is present, and then returns that.
  62. # Note, if no "result" is included in the response from XBMC, the JSON response is returned instead.
  63. # You can then print out the whole thing, or pull info you want for further processing or additional calls.
  64. if 'result' in response:
  65. response = response['result']
  66. # This error handling is specifically to catch HTTP errors and connection errors
  67. except urllib2.URLError as e:
  68. # In the event of an error, I am making the output begin with "ERROR " first, to allow for easy scripting.
  69. # You will get a couple different kinds of error messages in here, so I needed a consistent error condition to check for.
  70. response = 'ERROR '+str(e.reason)
  71. return response
  72.  
  73. # Here's an example of using the above method and variable values to make XBMC run the add-on
  74. time.sleep(10)
  75. results=getJsonRemote(ip,port,username,password,method,parameters)
  76. print results
  77. time.sleep(5)
  78. results=xbmc.executebuiltin("SendClick(yesnodialog,11)")
  79. #xbmc.executebuiltin("Action(Left,yesnodialog)")
  80. #xbmc.executebuiltin("Action(Enter,yesnodialog)")
  81. #results=getJsonRemote(ip,port,username,password,'Input.Left','')
  82. #results=getJsonRemote(ip,port,username,password,'Input.Select','')
  83. # I just print the results out
  84. print results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement