From a16886f8d3c9c2c78d3b871badc826883771f2ca Mon Sep 17 00:00:00 2001 From: Yunfi Date: Sun, 1 Sep 2024 20:12:00 +0800 Subject: [PATCH] fix(route): add full-text output for yystv (#16593) * fix(route): add full-text output for yystv * fix: cheerio issue * fix: type --- lib/routes/yystv/docs.ts | 47 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/routes/yystv/docs.ts b/lib/routes/yystv/docs.ts index a98f55e0c..350a99cad 100644 --- a/lib/routes/yystv/docs.ts +++ b/lib/routes/yystv/docs.ts @@ -1,7 +1,8 @@ -import { Route } from '@/types'; -import got from '@/utils/got'; +import type { Route, DataItem } from '@/types'; +import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { parseRelativeDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; export const route: Route = { path: '/docs', @@ -22,34 +23,42 @@ export const route: Route = { }, ], name: '游研社 - 全部文章', - maintainers: ['HaitianLiu'], + maintainers: ['HaitianLiu', 'yy4382'], handler, url: 'yystv.cn/docs', }; async function handler() { const url = `https://www.yystv.cn/docs`; - const response = await got({ - method: 'get', - url, - }); + const response = await ofetch(url); - const data = response.data; - const $ = load(data); + const $ = load(response); - const items = $('.list-container li') - .slice(0, 18) - .map(function () { + const itemList = $('.list-container li') + .toArray() + .map((item) => { + const itemElement = $(item); const info = { - title: $('.list-article-title', this).text(), - link: 'https://www.yystv.cn' + $('a', this).attr('href'), - pubDate: parseRelativeDate($('.c-999', this).text()), - author: $('.handler-author-link', this).text(), - description: $('.list-article-intro', this).text(), + title: itemElement.find('.list-article-title').text(), + link: 'https://www.yystv.cn' + itemElement.find('a').attr('href'), + pubDate: parseRelativeDate(itemElement.find('.c-999').text()), + author: itemElement.find('.handler-author-link').text(), + description: itemElement.find('.list-article-intro').text(), }; return info; - }) - .get(); + }) satisfies DataItem[]; + + const items = (await Promise.all( + itemList.map( + (item) => + cache.tryGet(item.link, async () => { + const resp = await ofetch(item.link); + const $ = load(resp); + item.description = $('#main section.article-section .doc-content > div').html() || item.description; + return item; + }) as Promise + ) + )) satisfies DataItem[]; return { title: '游研社-' + $('title').text(),