-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
202 lines (170 loc) · 7.4 KB
/
parser.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import json
import re
import time
from datetime import datetime
from urllib.parse import urljoin, urlparse
import requests
from bs4 import BeautifulSoup
BASE_URL = 'https://docs.microsoft.com/en-us/rest/api/power-bi/'
def parse_page(soup, url):
query, body = [], {}
uri_parameters_table = soup.select_one('#uri-parameters + table')
if uri_parameters_table:
trs = uri_parameters_table.select('tr')
name_index, in_index, required_index, type_index, description_index = 0, 1, 2, 3, 4
for tr in trs:
if ths := tr.select('th'):
columns = [th.text.strip() for th in ths]
print("URI Parameters:", columns)
# Name In Required Type Description
name_index = columns.index('Name')
in_index = columns.index('In')
required_index = columns.index('Required')
type_index = columns.index('Type')
description_index = columns.index('Description')
elif tds := tr.select('td'):
row = [re.sub(r'( )+', ' ', td.text.strip().replace('\r', ' ').replace('\t', ' ').replace('\n', ' ')) for td in tds]
if row[in_index] == 'path':
continue
else: # query
name = row[name_index]
required = row[required_index]
data_type = row[type_index]
description = row[description_index]
optional = '(Optional) ' if not required else ''
query.append({
'key': name,
'value': None,
'description': f"{data_type} {optional}{description}",
'disabled': True if not required else False
})
else:
print(f"ELSE ?!?!?!?!")
raise
items = [f"{item['key']}={item['value']}" for item in query]
if items:
url += '?' + '&'.join(items)
request_body_table = soup.select_one('#request-body + table')
if request_body_table:
body['mode'] = 'raw'
body['raw'] = ''
body_raw_dict = {}
trs = request_body_table.select('tr')
name_index, required_index, type_index, description_index = 0, 1, 2, 3
for tr in trs:
if ths := tr.select('th'):
columns = [th.text.strip() for th in ths]
print("Request Body:", columns)
name_index = columns.index('Name')
type_index = columns.index('Type')
description_index = columns.index('Description')
try:
required_index = columns.index('Required')
except ValueError:
required_index = -1
pass
elif tds := tr.select('td'):
row = [td.text.strip() for td in tds]
body_raw_dict[row[name_index]] = ''
else:
print(f"ELSE ?!?!?!?!")
raise
body['raw'] = json.dumps(body_raw_dict, indent='\t')
return query, body, url
def main():
# ------------------------------------------------------------------------
# 0. Postman Collection v2.1 뼈대
# ------------------------------------------------------------------------
postman_collection = {
"info": {
"name": "Power BI REST API v1",
"description": "Power BI REST API provides service endpoints for embedding, administration, and user resources.\r\n\r\nhttps://docs.microsoft.com/en-us/rest/api/power-bi/",
"version": {
"major": 0,
"minor": 1,
"patch": 0,
"identifier": f"beta-{datetime.now().strftime('%y%m%d')}",
"meta": "Jangwon Seo"
},
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [],
"protocolProfileBehavior": {}
}
# ------------------------------------------------------------------------
# 1. 제일 큰 구조 (REST Operation groups → Folder)
# ------------------------------------------------------------------------
time.sleep(1)
r = requests.get(BASE_URL)
# soup = BeautifulSoup(r.content.decode(), 'lxml')
soup = BeautifulSoup(r.content.decode(), 'html.parser')
table = soup.select_one('table')
tbody = table.select_one('tbody')
trs = tbody.select('tr')
lis = []
for tr in trs:
title = tr.select_one('td:nth-child(1) > a').text.strip()
link = tr.select_one('td:nth-child(1) > a').get('href')
description = tr.select_one('td:nth-child(2)').text.strip()
lis.append({
'name': title,
'item': [],
'description': description + '\r\n\r\n' + link,
'protocolProfileBehavior': {},
'_link': BASE_URL + link
})
# ------------------------------------------------------------------------
# 2. REST Operation groups 각각의 Operations → Request
# ------------------------------------------------------------------------
for i, li in enumerate(lis):
print(f"[{i+1:2}/{len(lis)}] {li['name']}")
time.sleep(1)
r = requests.get(li['_link'])
soup = BeautifulSoup(r.content.decode(), 'html.parser')
table = soup.select_one('table')
trs = table.select('tr')
operations = []
for tr in trs:
a = tr.select_one('td:nth-child(1) > a')
name = a.text.strip()
full_href = urljoin(BASE_URL, a.get('href'))
description_title = f"<a href='{full_href}'>{name}</a>"
description_html = tr.select_one('td:nth-child(2)').decode_contents().strip()
time.sleep(1)
inner_r = requests.get(full_href)
inner_soup = BeautifulSoup(inner_r.content.decode(), 'html.parser')
method, url = inner_soup.select_one('pre > code').text.split(' ')
parsed_url = urlparse(url)
paths = list(filter(None, parsed_url.path.split('/')))
query, body, url_with_query = parse_page(inner_soup, url)
headers = []
if body:
headers.append({
"key": "Content-Type",
"type": "text",
"value": "application/json"
})
operations.append({
'name': name,
"request": {
"method": method,
"header": headers,
"body": body,
"url": {
"raw": url,
"protocol": parsed_url.scheme, # "https",
"host": parsed_url.netloc.split('.'), # ["api", "powerbi", "com"],
"path": paths, # ["v1.0", "myorg", "groups"],
"query": query
},
"description": description_title + '<br>' + description_html
},
"response": [],
'_link': full_href
})
lis[i]['item'] = operations
postman_collection['item'] = lis
with open(f"postman_collection {datetime.now().strftime('%Y-%m-%d %H;%M;%S')}.json", 'w') as f:
json.dump(postman_collection, f)
if __name__ == '__main__':
main()