Skip to content

Commit

Permalink
fix(route): add full-text output for yystv (#16593)
Browse files Browse the repository at this point in the history
* fix(route): add full-text output for yystv

* fix: cheerio issue

* fix: type
  • Loading branch information
yy4382 authored Sep 1, 2024
1 parent a9caa10 commit a16886f
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions lib/routes/yystv/docs.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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<DataItem>
)
)) satisfies DataItem[];

return {
title: '游研社-' + $('title').text(),
Expand Down

0 comments on commit a16886f

Please sign in to comment.