-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatexbmc.py
executable file
·68 lines (53 loc) · 1.7 KB
/
updatexbmc.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
67
68
#!/usr/bin/env python
settings = {
'hostname': '127.0.0.1',
'port': '8080',
'username': 'htpc',
'password': 'htpc'
}
http_address = 'http://%s:%s/jsonrpc' % (settings['hostname'], settings['port'])
username = settings['username']
password = settings['password']
try:
import json
except ImportError:
import simplejson as json
import urllib2, base64
class XBMCJSON:
def __init__(self, server):
self.server = server
self.version = '2.0'
def __call__(self, **kwargs):
method = '.'.join(map(str, self.n))
self.n = []
return XBMCJSON.__dict__['Request'](self, method, kwargs)
def __getattr__(self,name):
if not self.__dict__.has_key('n'):
self.n=[]
self.n.append(name)
return self
def Request(self, method, kwargs):
data = [{}]
data[0]['method'] = method
data[0]['params'] = kwargs
data[0]['jsonrpc'] = self.version
data[0]['id'] = 1
data = json.JSONEncoder().encode(data)
content_length = len(data)
content = {
'Content-Type': 'application/json',
'Content-Length': content_length,
}
request = urllib2.Request(self.server, data, content)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
f = urllib2.urlopen(request)
response = f.read()
f.close()
response = json.JSONDecoder().decode(response)
try:
return response[0]['result']
except:
return response[0]['error']
xbmc = XBMCJSON(http_address)
xbmc.AudioLibrary.Scan()