-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
index.js
83 lines (70 loc) 路 2.86 KB
/
index.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
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 * as path from 'node:path';
export default async (ctx) => {
const { id = 'news', category = 'china' } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 30;
const rootUrl = `http://${id}.m4.cn`;
const currentUrl = new URL(category ? `/${category.replace(/\/$/, '')}/` : '/', rootUrl).href;
const { data: response } = await got(currentUrl);
const $ = load(response);
let items = $('div.articleitem0 div.aheader0')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);
const a = item.find('a').first();
return {
title: a.text(),
link: a.prop('href'),
description: art(path.join(__dirname, 'templates/description.art'), {
images: [
{
src: item.parent().find('div.aimg0 a img').prop('src'),
alt: a.text(),
},
],
}),
category: item
.find('a.aclass')
.toArray()
.map((c) => $(c).text().replaceAll('[]', '').trim()),
pubDate: timezone(parseDate(item.find('span.atime').text()), +8),
};
});
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const { data: detailResponse } = await got(item.link);
const content = load(detailResponse);
item.title = content('h1').first().text();
item.description = art(path.join(__dirname, 'templates/description.art'), {
intro: content('div.aintro1, p.cont-summary').text(),
description: content('div.content0, div.cont-detail').html(),
});
item.category = content('span.dd0 a, a[rel="category"]')
.toArray()
.map((c) => content(c).text())
.slice(1);
item.pubDate = timezone(parseDate(content('span.atime1, span.post-time').text()), +8);
return item;
})
)
);
const image = $('a.logo0_b img').prop('src');
ctx.set('data', {
item: items,
title: $('title').text(),
link: currentUrl,
description: $('meta[name="description"]').prop('content'),
language: 'zh',
image,
subtitle: $('meta[name="keywords"]').prop('content'),
author: $('meta[name="author"]').prop('content'),
allowEmpty: true,
});
};