-
Notifications
You must be signed in to change notification settings - Fork 3
/
updateapi.py
executable file
·139 lines (110 loc) · 3.33 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
import sys
import httplib2
def linebreak(indent):
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 introspect_http(host, port):
'''Get api definition'''
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 print_file(filename, output):
'''Write result'''
file_handle = open(filename, "w")
file_handle.write(output)
def format_data(data):
'''format resulting json'''
bracecount = 0
methodlevel = -1
methodstring = ""
indent = 0
outputjson = ""
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(indent)
if char == "}" or char == "]":
bracecount -= 1
indent -= 1
prefix += linebreak(indent)
if char == ",":
# if(lastchar=="}"):
suffix += linebreak(indent)
if bracecount == methodlevel + 1:
suffix += linebreak(indent)
# 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
return outputjson
def main():
"""Our main program"""
# Config here:
host = "localhost"
port = 8080
outputfile = "doc/kodiapi.json"
config_file = '.fetchapi_config.py'
nextishost = 0
nextisport = 0
nextisof = 0
execfile(config_file)
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)
data = introspect_http(host, port)
outputjson = format_data(data)
print_file(outputfile, outputjson)
if __name__ == "__main__":
main()