/
search.ts
45 lines (42 loc) · 1.45 KB
/
search.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
import { Route } from '@/types';
import { originUrl, getArticleDesc } from './utils';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
path: '/search/:kw',
categories: ['picture'],
example: '/jpxgmn/search/candy',
parameters: { kw: '搜索关键词' },
name: '搜索',
maintainers: ['Urabartin'],
handler,
};
async function handler(ctx) {
const { kw } = ctx.req.param();
const searchUrl = originUrl + `/plus/search/index.asp?keyword=${kw}`;
const response = await ofetch.raw(searchUrl);
const baseUrl = new URL(response.url).origin;
const $ = load(response._data);
const items = $('div.list div.list div.node p')
.toArray()
.map((item) => ({
title: $(item).find('b').text(),
link: new URL($(item).find('a').attr('href'), baseUrl).href,
pubDate: parseDate($(item).next().next().next().find('span').first().text()),
}))
.filter((item) => item.title.length !== 0);
return {
title: `极品性感美女搜索 - ${kw}`,
link: response.url,
item: await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
item.description = await getArticleDesc(item.link);
return item;
})
)
),
};
}