/
nice.ts
105 lines (92 loc) · 3.08 KB
/
nice.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 got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import MarkdownIt from 'markdown-it';
const md = MarkdownIt({
html: true,
});
export const route: Route = {
path: '/nice',
categories: ['blog'],
example: '/chuanliu/nice',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['chuanliu.org/nice'],
},
],
name: '严选',
maintainers: ['nczitzk'],
handler,
url: 'chuanliu.org/nice',
};
async function handler(ctx) {
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 100;
const rootUrl = 'https://chuanliu.org';
const apiRootUrl = 'https://s.chuanliu.org';
const currentUrl = new URL('nice/', rootUrl).href;
const apiUrl = new URL('api/dis/api/v1/memo', apiRootUrl).href;
const { data: response } = await got(apiUrl, {
searchParams: {
creatorId: 1,
offset: 0,
limit,
},
});
const items = response.slice(0, limit).map((item) => {
const contents = item.content.split(/\n/);
const category = contents?.[0].replace(/^#/, '') ?? undefined;
const title = contents?.[1] ?? undefined;
const link = contents?.[2] ?? undefined;
const author = contents?.[3] ?? undefined;
const isStar = (contents?.[5] && contents[5] === 'star') ?? false;
if (isStar) {
contents.splice(5, 1);
}
return {
title: `${isStar ? '[STAR] ' : ''}${title}`,
link,
description: art(path.join(__dirname, 'templates/description.art'), {
description: md.render(contents?.join('\n\n') ?? ''),
images: item.resourceList.map((i) => ({
src: i.externalLink,
alt: i.filename,
type: i.type,
})),
}),
author,
category: [category, isStar ? 'STAR' : undefined].filter(Boolean),
guid: `chuanliu-nice#${item.id}`,
pubDate: parseDate(item.createdTs, 'X'),
updated: parseDate(item.updatedTs, 'X'),
};
});
const { data: currentResponse } = await got(currentUrl);
const $ = load(currentResponse);
const icon = new URL($('link[rel="shortcut icon"]').prop('href'), currentUrl).href;
return {
item: items,
title: $('title').text(),
link: currentUrl,
description: $('span.rainbow-text').first().text(),
language: $('html').prop('lang'),
icon,
logo: icon,
subtitle: $('title').text(),
author: $('meta[name="author"]').prop('content'),
allowEmpty: true,
};
}