generated from digshare-scripts/digshare-script-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
62 lines (49 loc) · 1.32 KB
/
script.ts
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
import {script} from '@digshare/script';
import * as Cheerio from 'cheerio';
import fetch from 'node-fetch';
const USER_URL = 'https://xueqiu.com/u/3690450104';
interface Payload {}
interface Storage {
pushed: string[];
}
export default script<Payload, Storage>(async (_payload, {storage}) => {
let html = await fetch(USER_URL, {
headers: {
'User-Agent': 'Googlebot',
},
}).then(response => response.text());
let $ = Cheerio.load(html);
let pushedSet = new Set(storage.getItem('pushed'));
let user = $('h1').text();
let statuses = $('.list > .content')
.toArray()
.map(content => {
let href = $('a', content).attr('href');
let status = $('p:last-child', content).text();
if (typeof href !== 'string' || !/^\/\d+\/\d+$/.test(href)) {
return undefined;
}
return {
href,
status,
};
})
.filter(
(status): status is NonNullable<typeof status> =>
!!status && !pushedSet.has(status.href),
);
if (statuses.length === 0) {
console.info('没有发布新状态。');
return undefined;
}
storage.setItem(
'pushed',
[...pushedSet, ...statuses.map(status => status.href)].slice(-100),
);
return {
content: `\
${user} 发布了新的状态:
${statuses[0].status}`,
links: [USER_URL],
};
});