-
Notifications
You must be signed in to change notification settings - Fork 8
/
getList.js
61 lines (57 loc) · 2.02 KB
/
getList.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
const spider = require('@ksky521/spider');
const Queue = require('./lib/Queue');
const getMidFromUrl = require('./lib/getMidFromUrl');
const getMdArticle = require('./lib/getMdArticle');
const fs = require('fs-extra');
function self(url, jsonFilePath, options) {
return new Promise((resolve, reject) => {
if (!url) {
reject('url is empty');
}
spider.Spider(
url,
(err, data) => {
if (!err) {
let items = data.items;
if (!Array.isArray(items)) {
if (items.mid) {
items = [items];
} else {
reject('getList Error: ' + JSON.stringify(data));
}
}
const queue = new Queue(getMdArticle, 2);
items.forEach(({url, mid, title}, i) => {
queue.add([mid, url, options]);
});
queue.run().then(data => {
data = data.filter(item => {
return item && item.content;
});
fs.writeJSONSync(jsonFilePath, data);
resolve(data);
});
} else {
reject(err);
}
// content = toMd(content, { gfm: true });
// console.log(title, content);
},
{
items: {
selector: '#js_content a',
handler: (dom, $) => {
let url = dom.attr('href');
return {
mid: getMidFromUrl(url),
url,
title: dom.text().trim()
};
}
}
}
);
});
}
// const url = 'https://mp.weixin.qq.com/s/CIPosICgva9haqstMDIHag';
module.exports = self;