/
index.ts
95 lines (80 loc) · 2.86 KB
/
index.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
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
path: '/:category?',
categories: ['new-media'],
example: '/tangshufang',
parameters: { category: '分类,见下表,默认为首页' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['tangshufang.com/:category', 'tangshufang.com/'],
},
],
name: '分类',
maintainers: ['nczitzk'],
handler,
description: `| 首页 | 老唐实盘 | 书房拾遗 | 理念 & 估值 | 经典陪读 | 财务套利 |
| ---- | -------- | -------- | ----------- | -------- | -------- |
| | shipan | wenda | linian | peidu | taoli |
| 企业分析 | 白酒企业 | 腾讯控股 | 分众传媒 | 海康威视 | 其他企业 |
| -------- | -------- | -------- | -------- | -------- | -------- |
| qiye | baijiu | tengxun | fenzhong | haikang | qita |
| 核心五篇 | 读者投稿 | 读书随笔 | 财报浅析 | 出行游记 | 巴芒连载 |
| -------- | -------- | -------- | -------- | -------- | -------- |
| hexin | tougao | suibi | caibao | youji | bamang |`,
};
async function handler(ctx) {
const category = ctx.req.param('category') ?? '';
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 10;
const rootUrl = 'https://www.tangshufang.com';
const currentUrl = `${rootUrl}${category ? `/${category}` : ''}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = load(response.data);
let items = $('article')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);
const a = item.find('h2 a');
return {
title: a.text(),
link: a.attr('href'),
pubDate: parseDate(item.find('time').text()),
};
});
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
item.description = content('.wxsyncmain').html();
item.category = content('a[rel="category tag"]')
.toArray()
.map((a) => content(a).text());
return item;
})
)
);
return {
title: $('title').text(),
link: currentUrl,
item: items,
};
}