-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_swagger_TrelloAPI.py
executable file
·151 lines (130 loc) · 3.77 KB
/
generate_swagger_TrelloAPI.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
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
from bs4 import BeautifulSoup
from processMethod import processMethod
import urllib2
import json
import sys
# Constants
TRELLO_JSON_FILE="TrelloAPI.json"
API_DOC = "https://trello.com/docs/api"
# Methods
def processTrelloEntity(soup, swagger, numEntity):
entity = swagger['tags'][numEntity]['name']
'''
# print "Entity {} ".format(entity)
# if "board" in entity :
if "action" in entity :
# if "label" in entity :
# if "member" in entity :
print "Doing {}".format(entity)
else:
return
'''
index = soup.html.body.find(class_='section', id=entity)
for div in index.find_all("div", recursive=False) :
# print "Entity {} -- {}".format(entity, div['id'])
processMethod(div, swagger, entity)
return
def processTrelloAPIIndex(soup, swagger):
for link in soup.find_all(class_='toctree-l1') :
swagger['tags'].append(
{
"name": link.a.string
, "description": "{}/{}".format(API_DOC, link.a['href'])
})
return
def processTrelloToC(swagger):
idx = 0
for tag in swagger['tags'] :
if idx != 101 :
# print "{}".format(tag['description'])
processTrelloEntity(
BeautifulSoup(urllib2.urlopen(tag['description']).read())
, swagger
, idx
)
idx = idx + 1
return
# - - - - - - - - - - -
# Main routine (for testing)
def main():
swagger = {
"swagger": "2.0",
"info": {
"version": "1.0",
"title": "Trello API",
"description": "This document describes the REST API of Trello as "
+ "published by Trello.com."
+ "\n - <a href='https://trello.com/docs/index.html' target='_blank'>"
+ "Official Documentation</a>"
+ "\n - <a href='" + API_DOC + "' target='_blank'>"
+ "The HTML pages that were scraped in order to "
+ "generate this specification.</a>",
"termsOfService": "https://trello.com/legal",
"contact": {
"name": "Trello",
"url": "https://trello.com/home"
},
"license": {
"name": "Trello : Terms of Service",
"url": "https://trello.com/legal"
}
},
"host": "trello.com",
"tags": [
],
"securityDefinitions": {
"trello_auth": {
"type": "oauth2",
"authorizationUrl": "https:\/\/trello.com\/1\/appKey\/generate",
"flow": "implicit",
"scopes": {
"write:boards": "modify any of the boards in your account",
"read:boards": "read any of the boards in your account"
}
},
"api_key": {
"type": "apiKey",
"name": "api_key",
"in": "header"
}
},
"definitions": {
},
"paths": {
},
"basePath": "/1",
"schemes": [
"https"
],
}
soup = BeautifulSoup(urllib2.urlopen(API_DOC).read())
processTrelloAPIIndex(soup, swagger)
processTrelloToC(swagger)
print(">> - - - - - - - - - - ! - - - - - - - - - - -<<")
print "JSON {} written to {}".format(json.dumps(swagger['info']['title']), TRELLO_JSON_FILE)
print("<< - - - - - - - - - - - - - - - - - - - - - ->>")
# now write output to a file
TrelloAPI_json = open(TRELLO_JSON_FILE, "w")
# magic happens here to make it pretty-printed
prettySpec = json.dumps(swagger, indent=2, sort_keys=False)
# replace all "false" with false and all "true" with true
finalSpec = prettySpec.replace("\"false\"", "false").replace("\"true\"", "true")
TrelloAPI_json.write(prettySpec)
TrelloAPI_json.close()
if __name__ == "__main__": main()
# "List":{
# "type":"object",
# "properties":{
# "name":{
# "type":"string"
# },
# "pos":{
# "type":"string",
# "default":"top"
# }
# },
# "xml":{
# "name":"Order"
# }
# }