-
Notifications
You must be signed in to change notification settings - Fork 26
/
extractor_old.py
239 lines (180 loc) · 6.18 KB
/
extractor_old.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import requests
DEBUG = True
def debug(t):
if DEBUG:
print("\n# " + t)
def arg_to_s(arg):
return "{%s, [%s]%s}" % (arg[0], ", ".join(arg[1]), ", :optional" if arg[2] else "")
def build_method(name, args, returned):
typ = ":get" if name.startswith("get") else ":post"
if args is None:
args = []
args = [arg_to_s(x) for x in args]
return """method {}, "{}", [{}], {}""".format(typ, name, ", ".join(args), returned)
def parse_types(text):
res = []
for t in text.split(" or "):
t = t.strip()
name = ""
if t == "Integer":
name = ":integer"
elif t == "String":
name = ":string"
elif t == "Boolean":
name = ":boolean"
elif t == "Float":
name = ":float"
elif t == "True":
name = ":boolean"
elif t == "Float number":
name = ":float"
elif t == "InputFile":
name = ":file"
elif t.startswith("Array"):
rest_t = " of ".join(t.split(" of ")[1:])
more = parse_types(rest_t)
el = more[0] if len(more) > 0 else ":any"
name = "{:array, %s}" % el
elif " and " in t:
splitted = t.split(" and ")
left = parse_types(splitted[0])[0]
right = parse_types(" and ".join(splitted[1:]))[0]
name = "[%s, %s]" % (left, right)
elif t[0].isupper():
name = t
# debug("Class {} as any".format(t))
# return [":any"]
if name != "":
res.append(name)
else:
debug("ERROR PARSING TYPE: {}".format(t))
return res
def extract_table(table):
res = []
x = (len(table.findAll('tr')))
for row in table.findAll("tr")[1:x]:
col = row.findAll('td')
name = col[0].getText()
types = parse_types(col[1].getText())
opt = col[2].getText() == "Optional"
res.append((name, types, opt))
return res
def good_type(t):
t = t.strip(".").strip(",").strip()
if t == "ExGram.Model.Int":
return "integer"
if t == "ExGram.Model.String":
return "String.t()"
if t.lower() in ["exgram.model.true"]:
return "true"
return t
def extract_return_type(text):
if "Array of Update objects is returned" in text:
return "[ExGram.Model.Update]"
if "File object is returned" in text:
return "ExGram.Model.File"
if " is returned" in text:
return "ExGram.Model." + text.split(" is returned")[0].split()[-1]
if "returns an Array of GameHighScore" in text:
return "[ExGram.Model.GameHighScore]"
if "returns an Array of ChatMember" in text:
return "[ExGram.Model.ChatMember]"
if "Returns Array of BotCommand" in text:
return "[ExGram.Model.BotCommand]"
ts = ["Returns basic information about the bot in form of a ",
"returns the edited ",
"Returns exported invite link as ",
"Returns the new invite link as",
"Returns a ",
"returns a ",
"Returns ",
"returns "]
for x in ts:
if x in text:
return "ExGram.Model." + text.split(x)[1].split()[0]
return ":any"
# x is returned
def struct_t(name, types, opt):
t = ":any" if len(types) == 0 else types[0]
# t = t + ".t" if t[0].isupper() and t != "String" else t
extra = "" if not opt else ", :optional"
return "{:%s, %s%s}" % (name, t, extra)
def extract_model(h4):
name = h4.text
debug("Extracting type: " + name)
table = h4.find_next("table")
tabled = extract_table(table)
model_s = "model {}, [{}]"
ts = [struct_t(type_name, types, opt) for (type_name, types, opt) in tabled]
debug(str(ts))
return model_s.format(name, ", ".join(ts))
def generic_to_text(name, sub_types):
types_s = ", ".join(sub_types)
types_t = " | ".join(["{}.t()".format(x) for x in sub_types])
return """defmodule {} do
@type t :: {}
def subtypes() do
[{}]
end
end""".format(name, types_t, types_s)
def extract_generic(h4):
name = h4.text
debug("Extracting generic: " + name)
l = h4.find_next("ul")
sub_types = [li.text for li in l.findChildren("li", recursive=False)]
return generic_to_text(name, sub_types)
def main():
html = requests.get("https://core.telegram.org/bots/api").content
soup = BeautifulSoup(html, 'html.parser')
n = 0
# updates = soup.find(href="#getting-updates").parent.findAllNext("h4")
h4s = soup.find(href="#getting-updates").parent.findAllNext("h4")
# h4s = soup.find(href="#available-methods").parent.findAllNext("h4")
skip = []
generic_types = ["InlineQueryResult", "InputMessageContent", "PassportElementError"]
not_parameters = ["getMe", "deleteWebhook", "getWebhookInfo", "getMyCommands"]
models = []
methods = []
generics = []
for h4 in h4s:
name = h4.text
if name in skip:
debug("Skipping {}".format(name))
continue
if name in generic_types:
generics.append(extract_generic(h4))
continue
if name[0].isupper():
if len(name.split(" ")) == 1:
models.append(extract_model(h4))
continue
debug(name)
n += 1
returned = good_type(extract_return_type(h4.find_next("p").text))
debug("RETURNS: " + returned)
if name in not_parameters:
methods.append(build_method(name, [], returned))
continue
table = h4.find_next("table")
tabled = extract_table(table)
methods.append(build_method(name, tabled, returned))
debug("----------METHODS-----------\n")
print("# AUTO GENERATED")
print()
print("# Methods")
print()
print("\n\n".join(methods))
debug("{} methods\n".format(len(methods)))
debug("----------MODELS-----------\n")
print("# Models")
print()
print("\ndefmodule Model do")
print("\n\n ".join(models))
debug("{} models\n".format(len(models)))
print("\n\n ".join(generics))
debug("{} generics\n".format(len(generics)))
print("end")
if __name__ == "__main__":
main()