Guest User

XBMC autoupdate library

a guest
Mar 31st, 2015
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. settings = {
  2.     'hostname': '127.0.0.1',
  3.     'port': '1234',
  4.     'username': '',
  5.     'password': ''
  6. }
  7.  
  8.  
  9.  
  10. http_address = 'http://%s:%s/jsonrpc' % (settings['hostname'], settings['port'])
  11. username = settings['username']
  12. password = settings['password']
  13.  
  14. try:
  15.     import json
  16. except ImportError:
  17.     import simplejson as json
  18. import urllib2, base64
  19.  
  20. class XBMCJSON:
  21.  
  22.     def __init__(self, server):
  23.         self.server = server
  24.         self.version = '2.0'
  25.  
  26.     def __call__(self, **kwargs):
  27.         method = '.'.join(map(str, self.n))
  28.         self.n = []
  29.         return XBMCJSON.__dict__['Request'](self, method, kwargs)
  30.  
  31.     def __getattr__(self,name):
  32.         if not self.__dict__.has_key('n'):
  33.             self.n=[]
  34.         self.n.append(name)
  35.         return self
  36.  
  37.     def Request(self, method, kwargs):
  38.         data = [{}]
  39.         data[0]['method'] = method
  40.         data[0]['params'] = kwargs
  41.         data[0]['jsonrpc'] = self.version
  42.         data[0]['id'] = 1
  43.  
  44.         data = json.JSONEncoder().encode(data)
  45.         content_length = len(data)
  46.  
  47.         content = {
  48.             'Content-Type': 'application/json',
  49.             'Content-Length': content_length,
  50.         }
  51.    
  52.         request = urllib2.Request(self.server, data, content)
  53.         base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
  54.         request.add_header("Authorization", "Basic %s" % base64string)
  55.  
  56.         f = urllib2.urlopen(request)
  57.         response = f.read()
  58.         f.close()
  59.         response = json.JSONDecoder().decode(response)
  60.  
  61.         try:
  62.             return response[0]['result']
  63.         except:
  64.             return response[0]['error']
  65.  
  66.  
  67. xbmc = XBMCJSON(http_address)
  68. xbmc.VideoLibrary.Scan()
Advertisement
Add Comment
Please, Sign In to add comment