/
wechat.ts
90 lines (87 loc) · 3.01 KB
/
wechat.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
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
import { Route } from '@/types';
import got from '@/utils/got';
import { finishArticleItem } from '@/utils/wechat-mp';
import { load } from 'cheerio';
import utils from './utils';
import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
export const route: Route = {
path: '/wechat/:wxid',
categories: ['social-media'],
example: '/newrank/wechat/chijiread',
parameters: { wxid: '微信号,若微信号与新榜信息不一致,以新榜为准' },
features: {
requireConfig: [
{
name: 'NEWRANK_COOKIE',
description: '',
},
],
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '微信公众号',
maintainers: ['lessmoe'],
handler,
};
async function handler(ctx) {
if (!config.newrank || !config.newrank.cookie) {
throw new ConfigNotFoundError('newrank RSS is disabled due to the lack of <a href="https://docs.rsshub.app/deploy/config#route-specific-configurations">relevant config</a>');
}
const uid = ctx.req.param('wxid');
const nonce = utils.random_nonce(9);
const { data: summaryHTML } = await got({
method: 'get',
url: `https://www.newrank.cn/new/readDetial?account=${uid}`,
headers: {
Connection: 'keep-alive',
Cookie: config.newrank.cookie,
},
});
const summary$ = load(summaryHTML);
const mainsrc = summary$('script')
.toArray()
.find((item) => (item.attribs.src || '').startsWith('/new/static/js/main.')).attribs.src;
const { data: mainScript } = await got({
method: 'get',
url: `https://www.newrank.cn${mainsrc}`,
});
const N_TOKEN_match = mainScript.match(/"N-Token":"([^"]+)/);
if (!N_TOKEN_match) {
throw new Error('Cannot find n-token');
}
const N_TOKEN = N_TOKEN_match[1];
const response = await got({
method: 'post',
url: 'https://gw.newrank.cn/api/wechat/xdnphb/detail/v1/rank/article/lists',
headers: {
Connection: 'keep-alive',
Cookie: config.newrank.cookie,
'n-token': N_TOKEN,
},
form: {
account: uid,
nonce,
xyz: utils.decrypt_wechat_detail_xyz(uid, nonce),
},
});
const name = response.data.value.user.name;
const realTimeArticles = utils.flatten(response.data.value.realTimeArticles);
const articles = utils.flatten(response.data.value.articles);
const newArticles = [...realTimeArticles, ...articles];
const items = newArticles.map((item) => ({
title: item.title,
description: '',
link: item.url,
pubDate: item.publicTime,
}));
await Promise.all(items.map((item) => finishArticleItem(item)));
return {
title: name + ' - 微信公众号',
link: `https://www.newrank.cn/new/readDetial?account=${uid}`,
item: items,
};
}