-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathswagger2csv.py
More file actions
45 lines (35 loc) · 1.06 KB
/
swagger2csv.py
File metadata and controls
45 lines (35 loc) · 1.06 KB
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
#!/usr/bin/python
import os
import json
import sys
import requests
from urlparse import urlparse
def parse_docs(url_file):
with open(url_file, 'r') as f:
urls = f.read()
f.close()
for url in urls.split('\n'):
if url == None or url == '':
continue
r = requests.get(url)
apidocs = r.json()
if len(apidocs['apis']) < 1:
print "This swagger doc has no APIs defined!"
sys.exit(1)
csv = {}
for api in apidocs['apis']:
domain = urlparse(url).hostname
if domain not in csv:
csv[domain] = []
csv[domain].append(api['path'])
with open('apis.csv', 'w') as f:
f.write("domain,endpoint\n")
for domain in csv:
for endpoint in csv[domain]:
f.write("{},{}\n".format(domain,endpoint))
f.close()
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: {} [file containing URLs]".format(sys.argv[0])
sys.exit(1)
parse_docs(sys.argv[1])