forked from xingrz/mk-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
116 lines (95 loc) · 2.99 KB
/
update.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
const { parse } = require('yaml');
const { join } = require('path');
const { existsSync, readFileSync, writeFileSync } = require('fs');
const rp = require('request-promise');
const cheerio = require('cheerio');
const moment = require('moment');
const smartisan = parse(readFileSync(join(__dirname, '_data/smartisan.yml'), 'utf8'));
const meizu = parse(readFileSync(join(__dirname, '_data/meizu.yml'), 'utf8'));
const names = parse(readFileSync(join(__dirname, '_data/device_names.yml'), 'utf8'));
const devices = [
...smartisan,
...meizu,
];
const now = moment().utcOffset(8);
const base = now.format('YYYYMM');
const yaml = join(__dirname, `_data/stats/${base}.yml`);
const { BOT_TOKEN } = process.env;
const records = existsSync(yaml) && parse(readFileSync(yaml, 'utf8')) || [];
rp.get('https://stats.mokeedev.com')
.then(html => cheerio.load(html))
.then(parseDoc);
async function parseDoc($) {
const current = { timestamp: now.unix() };
$('tr').each((i, tr) => {
const tds = $(tr).find('> td');
if (tds.length != 2) return;
const td0 = $(tds.get(0));
const td1 = $(tds.get(1));
const name = td0.text().toLowerCase();
if (!devices.includes(name)) return;
current[name] = parseInt(td1.text().replace(/,/g,''));
});
let last = tail(records);
if (last) {
const timeOfLast = moment(last.timestamp * 1000).utcOffset(8);
if (timeOfLast.isSame(now, 'day')) {
records.pop();
last = tail(records);
}
}
records.push(current);
const lines = [];
for (const r of records) {
lines.push(`- timestamp: ${r.timestamp}`);
for (const d of devices) {
if (typeof r[d] != 'undefined') {
lines.push(` ${d}: ${r[d]}`);
}
}
}
lines.push('');
writeFileSync(yaml, lines.join('\n'));
await publish(smartisan, last, current, '@smartisandev');
console.log('');
console.log('');
await publish(meizu, last, current, '@meizudev');
}
function tail(array) {
if (array.length > 0) {
return array[array.length - 1];
} else {
return null;
}
}
async function publish(devices, last, current, chat_id) {
const message = [];
message.push(`*安装量统计*`);
message.push('');
for (const d of devices) {
if (typeof current[d] != 'undefined') {
if (last && last[d] < current[d]) {
message.push(`${names[d]} …… *${current[d]}* (+${current[d] - last[d]})`);
} else if (last && last[d] > current[d]) {
message.push(`${names[d]} …… *${current[d]}* (-${last[d] - current[d]})`);
} else {
message.push(`${names[d]} …… *${current[d]}*`);
}
}
}
message.push('');
message.push(`${now.format('YYYY/MM/DD HH:mm')}`);
message.push('[查看详情](https://stats.xingrz.me/)');
message.push('');
message.push('#stats');
console.log(message.join('\n'));
if (BOT_TOKEN) {
await rp.post(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
json: {
chat_id,
text: message.join('\n'),
parse_mode: 'Markdown',
}
});
}
}