-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc.py
executable file
·53 lines (39 loc) · 1.3 KB
/
rpc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
#from xbmcswift2 import xbmc
import xbmc
class RPCType(type):
def __getattr__(cls, category):
return Category(category)
class RPC(object):
__metaclass__ = RPCType
class Category(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name.title().replace("_", "")
def __getattr__(self, method):
return Method(self, method)
class Method(object):
def __init__(self, category, name):
self.category = category
self.name = name
def __str__(self):
return self.name.title().replace("_", "")
def __call__(self, **kwargs):
method = "%s.%s" % (str(self.category), str(self))
query = {"method": method, "params": kwargs}
return json_query(query)
class RPCError(Exception):
pass
def json_query(query):
if not "jsonrpc" in query:
query["jsonrpc"] = "2.0"
if not "id" in query:
query["id"] = 1
xbmc_request = json.dumps(query)
raw = xbmc.executeJSONRPC(xbmc_request)
clean = unicode(raw, 'utf-8', errors='ignore')
response = json.loads(clean)
if "error" in response:
pass
return response.get('result', response)