/
xhsxw.ts
110 lines (94 loc) · 3.06 KB
/
xhsxw.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
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 timezone from '@/utils/timezone';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
export const route: Route = {
path: ['/xhsxw', '/whxw'],
categories: ['new-media'],
example: '/news/xhsxw',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['news.cn/xhsxw.htm'],
},
],
name: '新华社新闻',
maintainers: ['nczitzk'],
handler,
url: 'news.cn/xhsxw.htm',
};
async function handler(ctx) {
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 100;
const rootUrl = 'http://www.news.cn';
const currentUrl = new URL('xhsxw.htm', rootUrl).href;
const { data: currentResponse } = await got(currentUrl);
const $ = load(currentResponse);
const id = $('ul.wz-list')
.prop('data')
.replace(/datasource:/, '');
const apiUrl = new URL(`ds_${id}.json`, rootUrl).href;
const {
data: { datasource: response },
} = await got(apiUrl);
let items = response.slice(0, limit).map((item) => ({
title: item.title,
link: new URL(item.publishUrl, rootUrl).href,
description: art(path.join(__dirname, 'templates/description.art'), {
images:
item.shareImages?.map((i) => ({
src: i.imageUrl,
alt: item.title,
})) ?? undefined,
intro: item.summary,
}),
author: item.author,
category: item.keywords.split(/-|,/),
guid: `news-${item.contentId}`,
pubDate: timezone(parseDate(item.publishTime), +8),
}));
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
try {
const { data: detailResponse } = await got(item.link);
const content = load(detailResponse);
item.description += art(path.join(__dirname, 'templates/description.art'), {
description: content('#detailContent').html(),
});
} catch {
// no-empty
}
return item;
})
)
);
const title = $('title').text();
const image = new URL('20141223_xhsxw_logo_v1.png', rootUrl).href;
const icon = new URL('favicon.ico', rootUrl).href;
return {
item: items,
title,
link: currentUrl,
description: title.split(/_/)[0],
language: 'zh',
image,
icon,
logo: icon,
author: title.split(/_/).pop(),
allowEmpty: true,
};
}