/
all.ts
105 lines (90 loc) · 3.18 KB
/
all.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
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, parseRelativeDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
export const route: Route = {
path: '/all/:id?',
categories: ['bbs'],
example: '/hupu/all/topic-daily',
parameters: { id: '编号,可在对应热帖版面 URL 中找到,默认为步行街每日话题' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['m.hupu.com/:category', 'm.hupu.com/'],
target: '/:category',
},
],
name: '热帖',
maintainers: ['nczitzk'],
handler,
description: `:::tip
更多热帖版面参见 [论坛](https://bbs.hupu.com)
:::`,
};
async function handler(ctx) {
const id = ctx.req.param('id') ?? 'topic-daily';
const rootUrl = 'https://bbs.hupu.com';
const currentUrl = `${rootUrl}/${id}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = load(response.data);
let items = $('div.t-info > a, a.p-title')
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: `https://m.hupu.com/bbs${item.attr('href')}`,
pubDate: timezone(parseDate(item.parent().parent().find('.post-time').text(), 'MM-DD HH:mm'), +8),
};
});
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
try {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
const videos = [];
content('.hupu-post-video').each(function () {
videos.push({
source: content(this).attr('src'),
poster: content(this).attr('poster'),
});
});
item.author = content('.bbs-user-wrapper-content-name-span').first().text();
item.pubDate = item.pubDate ?? timezone(parseRelativeDate(content('.second-line-user-info').first().text()), +8);
item.description = art(path.join(__dirname, 'templates/description.art'), {
videos,
description: content('.bbs-content').first().html(),
});
} catch {
// no-empty
}
return item;
})
)
);
return {
title: `虎扑社区 - ${$('.middle-title, .bbs-sl-web-intro-detail-title').text()}`,
link: currentUrl,
item: items,
};
}