From 1754ffa804151d678538dab185558070ae378bea Mon Sep 17 00:00:00 2001 From: Hualiang <78242797+hualiong@users.noreply.github.com> Date: Sat, 27 Jul 2024 20:51:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E6=8A=93=E5=8F=96=E5=8C=97?= =?UTF-8?q?=E6=9E=81=E6=98=9F=E9=A3=8E=E7=94=B5=E7=BD=91=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E5=85=A8=E6=96=87=20(#16273)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): 抓取北极星风电网文章全文 * Update lib/routes/bjx/fd.ts --------- --- lib/routes/bjx/fd.ts | 61 +++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/routes/bjx/fd.ts b/lib/routes/bjx/fd.ts index 54a8fd2310b378..fb172ddbe03e36 100644 --- a/lib/routes/bjx/fd.ts +++ b/lib/routes/bjx/fd.ts @@ -1,13 +1,14 @@ import { DataItem, Route } from '@/types'; -import got from '@/utils/got'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; +import ofetch from '@/utils/ofetch'; +import cache from '@/utils/cache'; export const route: Route = { path: '/fd/:type', categories: ['traditional-media'], example: '/bjx/fd/yw', - parameters: { type: '文章分类' }, + parameters: { type: '文章分类,详见下表' }, features: { requireConfig: false, requirePuppeteer: false, @@ -18,37 +19,45 @@ export const route: Route = { }, name: '风电', maintainers: ['hualiong'], + description: `\`:type\` 类型可选如下 + + | 要闻 | 政策 | 数据 | 市场 | 企业 | 招标 | 技术 | 报道 | +| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | +| yw | zc | sj | sc | mq | zb | js | bd |`, handler: async (ctx) => { const type = ctx.req.param('type'); - const response = await got({ - method: 'get', - url: `https://fd.bjx.com.cn/${type}/`, - }); - const data = response.data; - const $ = load(data); - const typeName = $('div.box2 em:last').text(); - const list = $('div.cc-list-content ul li'); + const response = await ofetch(`https://fd.bjx.com.cn/${type}/`); + + const $ = load(response); + const typeName = $('div.box2 em:last-child').text(); + const list = $('div.cc-list-content ul li:nth-child(-n+20)') + .toArray() + .map((item): DataItem => { + const each = $(item); + return { + title: each.find('a').attr('title')!, + link: each.find('a').attr('href'), + pubDate: parseDate(each.find('span').text()), + }; + }); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link!, async () => { + const response = await ofetch(item.link!); + const $ = load(response); + + item.description = $('#article_cont').html()!; + return item; + }) + ) + ); return { title: `北极星风力发电网${typeName}`, description: $('meta[name="Description"]').attr('content'), link: `https://fd.bjx.com.cn/${type}/`, - item: list - .map((index, item) => { - const each = $(item); - return { - title: each.find('a').attr('title'), - description: each.html(), - link: each.find('a').attr('href'), - pubDate: parseDate(each.find('span').text()), - }; - }) - .get() as DataItem[], + item: items as DataItem[], }; }, - description: `\`:type\` 类型可选如下 - - | 要闻 | 政策 | 数据 | 市场 | 企业 | 招标 | 技术 | 报道 | -| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | -| yw | zc | sj | sc | mq | zb | js | bd |`, };