This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
spider.js
212 lines (172 loc) · 4.8 KB
/
spider.js
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
'use strict';
const CONCURRENCY = 4
const MIN_WAIT = 1 * 1000
var request = require('co-request'),
co = require('co'),
cheerio = require('cheerio'),
URL = require('url');
var Scraper = {
categories: {},
services: {},
_queueued: {},
enqueue: function (url) {
url = url.replace(/#.*/, '').replace(/\.html$/, '');
if (url in this._queueued) return;
this._queueued[url] = true;
console.error('<-- <' + url + '>');
this.queue.push(url);
},
queue: []
};
function categoryFromUrl (url) {
var m = /^https:\/\/developers\.google\.com\/apps-script\/reference\/([^/]+)/.exec(url);
if (!m) return null;
return m[1];
}
function* visit (url) {
var category = categoryFromUrl(url);
if (!category) {
return;
}
if (!Scraper.categories[category]) {
Scraper.categories[category] = {
decls: {}
};
}
console.error('==> ' + url);
var result = yield request({
url: url
});
var $ = cheerio.load(result.body);
var headingText = $('h1').text();
var m = headingText.match(/(Class|Enum|Interface) ([a-zA-Z0-9_.]+)/);
if (!m) {
var m = headingText.match(/^\s*(.+) Service/);
if (m) {
Scraper.categories[category].name = m[1];
} else {
console.error('!!! unexpected heading: ' + headingText);
}
return;
}
/// doc
var doc = $('.type.doc').children().filter(function () {
return $(this).css('display') !== 'none';
}).map(function () {
if ($(this).is('pre')) {
// indent code part
return '\n' + $(this).text().split(/\n/).map((line) => ' ' + line).join('\n') + '\n\n';
} else {
return $(this).text();
}
}).toArray().join('\n');
var decl = {
kind: m[1].toLowerCase(),
name: m[2],
doc: doc,
url: url,
properties: [],
methods: []
};
function resolveHref (link) {
var href = link.attr('href');
if (href) {
return URL.resolve(url, href);
}
}
/// props
$('table.members.property tr:not(:first-child)').each(function () {
var cells = $(this).find('td');
if (cells.length !== 3) return;
var name = cells.eq(0),
type = cells.eq(1),
doc = cells.eq(2);
var typeHref = resolveHref(type.find('a'));
if (typeHref) Scraper.enqueue(typeHref);
decl.properties.push({
name: name.text(),
type: { name: type.text(), category: categoryFromUrl(typeHref) },
doc: doc.text()
});
});
/// methods
$('table.members.function tr:not(:first-child)').each(function () {
var cells = $(this).find('td');
if (cells.length !== 3) return;
var name = cells.eq(0),
type = cells.eq(1),
doc = cells.eq(2);
var typeHref = resolveHref(type.find('a'));
if (typeHref) Scraper.enqueue(typeHref);
var method = {
name: name.text().replace(/\(.*$/, ''),
returnType: { name: type.text(), category: categoryFromUrl(typeHref) },
doc: doc.text(),
params: []
};
var detailId = name.find('a').attr('href').substring(1);
// $('#' + detailId) fails
$('*[id]').filter(function () {
return $(this).attr('id') === detailId;
})
.find('table.function.param tr:not(:first-child)').each(function () {
var cells = $(this).find('td');
if (cells.length !== 3) return;
var name = cells.eq(0),
type = cells.eq(1),
doc = cells.eq(2);
var typeHref = resolveHref(type.find('a'));
if (typeHref) Scraper.enqueue(typeHref);
method.params.push({
name: name.text(),
type: { name: type.text(), category: categoryFromUrl(typeHref) },
doc: doc.text()
});
})
decl.methods.push(method);
});
Scraper.categories[category].decls[decl.name] = decl;
return;
}
co(function* () {
var startURL = 'https://developers.google.com/apps-script/reference/';
var result = yield request(startURL);
var $ = cheerio.load(result.body);
var inServices = true;
$('.devsite-section-nav li li li').each(function () {
var name = $(this).text();
if (name === 'Classes') {
inServices = false;
return;
}
var url = $(this).find('a[href]').attr('href');
if (!url) return;
url = URL.resolve(startURL, url);
if (inServices) {
Scraper.services[url] = true;
}
Scraper.enqueue(url);
if (name === 'Overview') {
inServices = true;
}
});
while (Scraper.queue.length > 0) {
var urls = Scraper.queue.splice(0, CONCURRENCY);
yield Promise.all(
urls.map((url) => co(visit(url)))
.concat(new Promise((ok) => setTimeout(ok, MIN_WAIT)))
);
}
return;
}).then(function () {
console.log(
JSON.stringify({
categories: Scraper.categories,
services: Scraper.services
}, true, 2)
);
process.exit(0);
}).catch(function (err) {
console.error('ERR ' + err);
process.exit(1);
});