forked from a-dekker/kodimote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateapi.py
executable file
·125 lines (99 loc) · 2.46 KB
/
updateapi.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
#!/usr/bin/python
import sys
import httplib2
def linebreak():
retval="\n"
for i in range(1,indent):
retval+=" "
return retval
# Dirty hack to "source" config files
def execfile(file, globals=globals(), locals=locals()):
try:
with open(file, "r") as fh:
exec(fh.read()+"\n", globals, locals)
except:
# print("config file not found")
pass
def introspectHttp(host, port):
h = httplib2.Http()
res, content = h.request("http://" + host + ":" + str(port) + "/jsonrpc", "POST", "{\"id\":0,\"jsonrpc\":\"2.0\", \"method\":\"JSONRPC.Introspect\"}")#, headers)
#print(res)
#print(content)
retval=""
retval+=content.decode("utf-8")
return retval
def printFile(filename, output):
f = open(filename, "w")
f.write(output)
host="localhost"
port=8080
outputfile="doc/xbmcapi.json"
config_file = '.fetchapi_config.py'
execfile(config_file)
nextishost = 0
nextisport = 0
nextisof = 0
for arg in sys.argv:
# print("command line arg", arg)
if(nextishost == 1):
host = arg
nextishost = 0
if(nextisport == 1):
port = arg
nextisport = 0
if(nextisof == 1):
outputfile = arg
nextisof = 0
if(arg == "--help"):
print("usage:", sys.argv[0], "[-h <ip>] [-p <port>] [-o <outputfile>]")
exit(0)
if(arg == "-h"):
nextishost = 1
if(arg == "-p"):
nextisport = 1
if(arg == "-o"):
nextisof = 1
print("connecting to host:", host)
bracecount=0
outputjson=""
lastchar=""
indent=0
methodstring=""
methodlevel=-1
data=introspectHttp(host, port)
if not data=="":
for char in data:
prefix=""
suffix=""
# if(char=="\""):
# if(lastchar==","):
# prefix+=linebreak();
if(char=="{") or (char=="["):
bracecount+=1
indent+=1
suffix+=linebreak()
if(char=="}") or (char=="]"):
bracecount-=1
indent-=1
prefix+=linebreak()
if(char==","):
# if(lastchar=="}"):
suffix+=linebreak()
if bracecount==methodlevel+1:
suffix+=linebreak()
# reassemble the string
outputjson+=prefix
outputjson+=char
outputjson+=suffix
# do some other thecks
methodstring+=char
tmp="methods"
if(methodstring==tmp):
methodlevel=bracecount
methodstring=""
# print("methodlevel is", methodlevel)
if not tmp.startswith(methodstring):
methodstring=""
# print("got brace. total:", bracecount)
lastchar=char
printFile(outputfile, outputjson)