-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadApi.js
More file actions
86 lines (76 loc) · 2.19 KB
/
loadApi.js
File metadata and controls
86 lines (76 loc) · 2.19 KB
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
// Generated by CoffeeScript 1.10.0
(function() {
'use strict';
var async, cheerio, fs, loadApi, log, pathpath, superagent;
fs = require('fs');
async = require('async');
superagent = require('superagent');
cheerio = require('cheerio');
pathpath = require('path');
log = console.log;
loadApi = function(url) {
var get, getModules, save, saveFile;
get = function(url, autoReload, maxReloadTime, cb) {
return function(cb) {
var _get, count;
count = 0;
log("get from " + url);
_get = function(url) {
return superagent.get(url).end(function(err, res) {
if (err) {
log(err.code);
if (autoReload && count < maxReloadTime) {
count++;
log("reload(" + count + ") : " + url);
return _get(url);
} else {
return log('Auto reload time exceed');
}
} else {
if (cb != null) {
return cb(null, res);
} else {
return save(res);
}
}
});
};
_get(url);
};
};
getModules = function(res) {
var $, all, attr, getHref, i, len;
$ = cheerio.load(res.text);
all = $('[href]');
getHref = function(attr) {
var href;
href = attr.href;
if (href && !href.match(/^http:|^https:/)) {
return get(url + href, false, 0)();
}
};
for (i = 0, len = all.length; i < len; i++) {
attr = all[i];
getHref(attr.attribs);
}
};
save = function(res) {
var $;
$ = cheerio.load(res.text);
$('img').remove();
$('script').remove();
saveFile(res.req.path, $.html());
};
saveFile = function(path, htmlStr) {
fs.writeFile(pathpath.join('./', path), htmlStr, function(err) {
return log(err ? err : path + " save success");
});
};
return async.waterfall([get('https://nodejs.org/api/', true, 10), getModules], function(err, result) {
log(err);
return log(result);
});
};
loadApi('https://nodejs.org/api/');
}).call(this);
//# sourceMappingURL=loadApi.js.map