This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs_spider.py
211 lines (181 loc) · 7.84 KB
/
funcs_spider.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
import json
import os
import re
import sys
import logging
from collections import OrderedDict
import scrapy
from scrapy.cmdline import execute
from pyquery import PyQuery as pq
class FunctionParser(object):
def __init__(self, prefix, syntax):
self.isFunc = '(' in syntax
syntax = re.sub(r"(?P<param>\w+)1\s*,?\s*((?P=param)2)?\s*,?\s*((\.+)|(\u2026))\s*,?\s*(?P=param)[nN]", r"\g<param>1_\g<param>N", syntax) # Ellipsis
if self.isFunc or (syntax.count("=") == 1):
syntax = re.sub(r".*=\s+(?=[\w.]+\s*(\(|$))", "", syntax) # delete return
syntax = syntax.replace(",", " ").replace("(", " ").replace(")", "")
self.identifier = syntax.split()
self.prefix = prefix
def getFname(self):
if len(self.identifier):
return self.identifier[0]
return self.prefix
def genSyntax(self):
return self._genFunction() if self.isFunc else self._genCommand()
def _genFunction(self):
return "{}({})".format(self.getFname(), self.genParams())
def _genCommand(self):
return "{} {}".format(self.getFname(), self.genParams()).strip()
def genPlainParams(self):
sep = r"\, " if self.isFunc else r" "
return sep.join(self.identifier[1:])
def genParams(self):
sep = ", " if self.isFunc else r" "
return sep.join([param if self._isConst(param) else "${" + param + "}" for param in self.identifier[1:]])
def _isConst(self, s):
return "'" in s or '"' in s or "=" in s or s.startswith("-")
class FunctionsSpider(scrapy.Spider):
name = "funcs"
version = "R2020a"
headers = {
"User-Agent:": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3879.0 Safari/537.36 Edg/78.0.249.1e"
}
baseurl = "https://www.mathworks.com/help/search/reflist/doccenter/en/{}".format(version)
acceptAllCategorys = True
acceptCategorys = [
"matlab", # MATLAB
]
prefix_filter = re.compile('^[0-9a-zA-Z._]+$')
def start_requests(self):
urls = [
'{}?type=function'.format(self.baseurl),
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parseCategorys, headers=self.headers)
def parseCategorys(self, response):
lst = json.loads(response.body)
for category in lst["siblingCategories"]:
product = category["helpdir"].split('/')[-2]
if (not self.acceptAllCategorys) and (product not in self.acceptCategorys):
continue
url = "{}?type=function&product={}".format(self.baseurl, product)
yield scrapy.Request(url=url, callback=self.parseFuncs, headers=self.headers, meta=dict(product=product))
def nest(self, data):
if "child-categories" in data:
urls = []
for child in data["child-categories"]:
urls += self.nest(child)
return urls
elif "leaf-items" in data:
urls = []
for child in data["leaf-items"]:
urls.append(self.nest(child))
return urls
else:
assert("path" in data)
url = "https://www.mathworks.com" + data["path"]
return url
def parseFuncs(self, response):
lst = json.loads(response.body)
for url in self.nest(lst["category"]):
yield scrapy.Request(url=url, callback=self.parseFunc, headers=self.headers, meta=response.meta)
def parseFunc(self, response):
if response.url.startswith('https://www.mathworks.com/login?uri='):
return
lang = response.xpath('//div[@class="ref_sect"]/h2[text()="Languages"]/..').get()
if lang:
self.log("unsupported lang {}, {}".format(pq(lang).text(), response.url), logging.ERROR)
return
desc = response.xpath('//div[@class="ref_sect"]/h2[text()="Syntax"]/.. | //div[@class="ref_sect"]/h3[text()="Syntax"]/..').get()
if not desc:
self.log("error processing " + response.url, level=logging.ERROR)
return
desc_pq = pq(desc)
desc_pq('h2').remove()
desc_pq('h3').remove()
desc:str = desc_pq.text()
title = response.xpath('//span[@class="refname"]/text() | //h1[@itemprop="title"]/text()').get()
syntaxs = desc.strip().split('\n')
for syntax in syntaxs:
prefix = syntax.strip().split('(')[0]
prefix = prefix.strip().split('=')[-1]
prefix = prefix.strip().split(' ')[0]
prefix = prefix.strip()
if self.prefix_filter.match(prefix) and (prefix.lower() in title.lower() or prefix.lower() in response.url.lower()):
break
else:
self.log("invalid prefix '{}', {}".format(prefix, response.url), level=logging.ERROR)
return
name = response.meta.get('product') + "/" + prefix
elements = response.css('.description_element').getall() or response.xpath('//h2[text()="Description"]/..//span[@itemprop="syntax"]/..').getall()
if not elements:
return {
name: {
"prefix": prefix,
"body": [
prefix
],
"description": desc + " ref: " + response.url
}
}
paramOpt = []
plains = []
opts = None
for idx, element in enumerate(elements):
element_pq = pq(element)
element_pq('.syntax_example').remove()
syntax = element_pq('span[itemprop="syntax"]').text()
syntaxParser = FunctionParser(prefix, syntax)
syntax = syntaxParser.genSyntax()
if '___' not in syntax:
plain = syntaxParser.genPlainParams()
if len(syntaxParser.identifier) > 1 and plain not in plains:
paramOpt.append(syntaxParser)
plains.append(plain)
elif len(paramOpt) == 0:
self.log("___ replace fail on {}".format(response.url), logging.ERROR)
elif len(paramOpt) == 1:
syntax = syntax.replace('${___}', paramOpt[0].genParams(), 1)
else:
if opts is None:
params = []
for syn in paramOpt:
param = syn.genPlainParams()
if param not in params:
params.append(param)
opts = ','.join(params)
syntax = syntax.replace("${___}", "$0${}1|{}|{}".format('{', opts, "}"), 1)
syntax = syntax.replace("${...}", "${etc}")
yield {
name + ("_{}".format(idx) if idx else ''): {
"prefix": prefix,
"body": [
syntax
],
"description": element_pq.text()
}
}
def close(self, reason):
self.log("closed by {}".format(reason))
content = None
with open('funcs.json', 'r') as f:
content = f.read()
data = json.loads(content)
funcs = {}
for func in data:
for key in func:
funcs[key] = func[key]
with open('patch.json', 'r') as f:
funcs.update(json.loads(f.read()))
od = OrderedDict(sorted(funcs.items()))
with open('snippets.json', 'w') as f:
f.write(json.dumps(od, indent=4))
if __name__ == "__main__":
output_json = "funcs.json"
log_file = "log.txt"
if os.path.exists(output_json):
os.remove(output_json)
if os.path.exists(log_file):
os.remove(log_file)
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
execute(['scrapy', 'runspider', 'funcs_spider.py', '-o', output_json, '-L', "ERROR", '--logfile', log_file])