/
timeline.ts
123 lines (107 loc) · 4.04 KB
/
timeline.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
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
117
118
119
120
121
122
123
import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
export const route: Route = {
path: '/timeline/:category?',
categories: ['finance'],
example: '/jinse/timeline',
parameters: { category: '分类,见下表,默认为头条' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '首页',
maintainers: ['nczitzk'],
handler,
description: `| 头条 | 独家 | 铭文 | 产业 | 项目 |
| ------ | ---- | ------- | ---------- | ---- |
| 政策 | AI | Web 3.0 | 以太坊 2.0 | DeFi |
| Layer2 | NFT | DAO | 百科 | |`,
};
async function handler(ctx) {
const { category = '头条' } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 50;
const rootUrl = 'https://www.jinse.cn';
const rootApiUrl = 'https://api.jinse.cn';
const apiUrl = new URL('noah/v3/timelines', rootApiUrl).href;
const currentUrl = rootUrl;
const { data: response } = await got(apiUrl, {
searchParams: {
catelogue_key: category === '头条' ? 'www' : category,
limit,
information_id: 0,
flag: 'up',
},
});
let items = response.data.list.slice(0, limit).map((item) => {
item = item.object_1 ?? item.object_2;
return {
title: item.title,
link: item.jump_url,
description: art(path.join(__dirname, 'templates/description.art'), {
images: item.cover
? [
{
src: item.cover.replace(/_[^\W_]+(\.\w+)$/, '_true$1'),
alt: item.title,
},
]
: undefined,
intro: item.summary,
description: item.content,
}),
author: item.author.nickname,
guid: `jinse${/\/lives\//.test(item.jump_url) ? '-lives' : ''}-${item.id}`,
pubDate: parseDate(item.published_at, 'X'),
upvotes: item.up_counts ?? 0,
downvotes: item.down_counts ?? 0,
comments: item.comment_count ?? 0,
};
});
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
if (/\/lives\//.test(item.link)) {
return item;
}
const { data: detailResponse } = await got(item.link);
const content = load(detailResponse);
item.description += art(path.join(__dirname, 'templates/description.art'), {
description: content('section.js-article-content').html() || content('div.js-article').html(),
});
item.category = content('section.js-article-tag_state_1 a span')
.toArray()
.map((c) => content(c).text());
return item;
})
)
);
const { data: currentResponse } = await got(currentUrl);
const $ = load(currentResponse);
const author = $('meta[name="author"]').prop('content');
const image = $('a.js-logoBox img').prop('src');
const icon = new URL($('link[rel="favicon"]').prop('href'), rootUrl).href;
return {
item: items,
title: `${author} - ${category}`,
link: currentUrl,
description: $('meta[name="description"]').prop('content'),
language: $('html').prop('lang'),
image,
icon,
logo: icon,
subtitle: $('meta[name="keywords"]').prop('content'),
author,
allowEmpty: true,
};
}