-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspider.js
78 lines (65 loc) · 2.01 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
'use strict';
const request = require('superagent');
const cheerio = require('cheerio');
const fs = require('fs');
const schedule = require('node-schedule');
const shell = require('shelljs');
// 抓取url地址
const URL = 'https://github.com/trending/javascript';
// 获取页面内容
function requestHtml() {
return request.get(URL).then(res => res.text);
}
// 提交采集的数据到git
function gitAddCommitPush(date) {
try {
if (shell.exec(`git add -A && git commit -m "add ${date}" && git push`).code === 0) {
console.log('提交成功');
}
} catch (error) {
console.log('提交失败');
}
}
// 获取页面数据
function getData(html) {
let $ = cheerio.load(html);
return Array.from($('.repo-list li').map(function () {
let link = $(this).find('h3 a').attr('href');
let desc = $(this).find('.py-1 p').text().trim();
return ({
link,
desc
});
}));
}
// 将数据写入文件
async function wirteFile(date, data, currentMonth) {
let writeStr = `### ${date} \n`;
data.map((item) => {
writeStr += `* [${(item.link).slice(1)}](https://github.com/${item.link}):${item.desc} \n`;
});
if (!fs.existsSync(currentMonth)) {
fs.mkdirSync(currentMonth);
}
fs.writeFileSync(`./${currentMonth}/${date}.md`, writeStr);
}
// 开始程序
async function start() {
// 格式化当前日期
let date = new Date();
let formatDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
let currentMonth = `${date.getFullYear()}-${date.getMonth() + 1}`;
console.log('开始爬取数据');
let html = await requestHtml();
let data = getData(html);
await wirteFile(formatDate, data, currentMonth);
console.log('开始提交数据');
await gitAddCommitPush(formatDate);
console.log('结束本次任务');
}
(function () {
console.log('程序开始运行');
schedule.scheduleJob('00 10 9 * * *', function () {
start();
})
}())