forked from primaeval/plugin.program.simple.favourites
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.py
66 lines (50 loc) · 1.54 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
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
PY3 = sys.version_info.major >= 3
if PY3:
from builtins import str, object
import json
#from xbmcswift2 import xbmc
import xbmc
class RPCType(type):
def __getattr__(cls, category):
return Category(category)
if PY3:
class RPC(object, metaclass=RPCType):
pass
else:
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)
if PY3:
response = json.loads(xbmc.executeJSONRPC(xbmc_request))
else:
raw = xbmc.executeJSONRPC(xbmc_request)
clean = unicode(raw, 'utf-8', errors='ignore')
response = json.loads(clean)
if "error" in response:
raise RPCError(response["error"])
return response.get('result', response)