forked from digitaldoggo/mycroft-skill-kodi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
136 lines (121 loc) · 5.16 KB
/
helpers.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os.path
from netifaces import interfaces, ifaddresses, AF_INET
import httplib2
import json
import socket
import jsonhelpers
headers = {"Content-type": "application/json"}
configFile = 'mycroft/configuration/kodi-config.json'
# testing
#configFile = 'kodi-config.json'
def make_request(conn, method, json_params):
config = auto_discover()
if config == -1:
return -1
try:
res, c = conn.request('http://' + config['HOST'] + ':' + str(config['PORT']) + '/jsonrpc?' + method, 'POST', json.dumps(json_params), headers)
if hasattr(res, 'status'):
status = res['status']
else:
return -1
if status == '200':
if 'c' in locals():
result = jsonhelpers.json_loads_byteified(c)
if result.has_key('error'):
# TODO: better handle errors returned from kodi
return -1
elif 'result' in locals():
return result
return -1
# TODO: better handle errors caught from request attempt
except socket.error, err:
return -1
except httplib2.ServerNotFoundError:
return -1
def get_player_id(conn):
method = 'Player.GetActivePlayers'
json_params = {
'jsonrpc':'2.0',
'method':method,
'id':1
}
result = make_request(conn, method, json_params)
if result == -1:
return -1
elif result['result'] != [] and result['result'][0].has_key('playerid'):
return result['result'][0]['playerid']
else:
return 0
def auto_discover():
# TODO: use async requests
conn = httplib2.Http(timeout=.1)
method = 'XBMC.GetInfoLabels'
json_params = {
'jsonrpc':'2.0',
'method':method,
'id':1,
'params': [['Network.IPAddress','System.FriendlyName']]
}
if os.path.isfile(configFile):
with open(configFile) as data_file:
config = jsonhelpers.json_load_byteified(data_file)
if ('config' in locals() and
config.has_key('HOST') and
config.has_key('PORT')):
try:
res, c = conn.request('http://' + config['HOST'] + ':' + str(config['PORT']) + '/jsonrpc?' + method, 'POST', json.dumps(json_params), headers)
if ('res' in locals() and
res.has_key('status') and
res['status'] == '200'):
print('Using config in ' + configFile)
return config
else:
print("Reconfiguring")
except:
print("Reconfiguring")
for ifaceName in interfaces():
addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr': 'No IP addr'}])]
if ifaceName != "lo":
for xxx in range(1, 255):
try:
ipPrefix = addresses[0][:addresses[0].rfind('.') + 1]
res, c = conn.request('http://' + ipPrefix + str(xxx) + ':8080/jsonrpc?' + method, 'POST', json.dumps(json_params), headers)
if hasattr(res, 'status'):
status = res['status']
else:
print("Response received, but no status.")
return -1
if status == '200':
if 'c' in locals():
result = jsonhelpers.json_loads_byteified(c)
if result.has_key('error'):
print("Error received from Kodi")
return -1
elif 'result' in locals():
print("Kodi found at 10.10.1." + str(xxx))
if 'config' in globals():
config['NAME'] = result['result']['System.FriendlyName']
config['HOST'] = result['result']['Network.IPAddress']
config['PORT'] = 8080
else:
f = open(configFile, 'w+')
config = {
'NAME': result['result']['System.FriendlyName'],
'HOST': result['result']['Network.IPAddress'],
'PORT': 8080
}
f.writelines([
'{',
'\n\t"NAME":"' + result['result']['System.FriendlyName'] + '",',
'\n\t"HOST":"' + result['result']['Network.IPAddress'] + '",',
'\n\t"PORT":' + '8080',
'\n}'
])
return config
print("Response received, but there was an issue. Status: " + status)
return -1
except socket.error, err:
pass
except:
pass
return -1