From 892fb2389cfb39eaa0c0ff3888190008996f08d1 Mon Sep 17 00:00:00 2001 From: huanfe1 <41602338+huanfe1@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:52:30 +0800 Subject: [PATCH 1/5] feat(route): add feed for qiche365 --- lib/routes/qiche365/namespace.ts | 5 +++ lib/routes/qiche365/recall.ts | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 lib/routes/qiche365/namespace.ts create mode 100644 lib/routes/qiche365/recall.ts diff --git a/lib/routes/qiche365/namespace.ts b/lib/routes/qiche365/namespace.ts new file mode 100644 index 0000000000000..6328a39e64c21 --- /dev/null +++ b/lib/routes/qiche365/namespace.ts @@ -0,0 +1,5 @@ +import type { Namespace } from '@/types'; +export const namespace: Namespace = { + name: '汽车365', + url: 'qiche365.org.cn', +}; diff --git a/lib/routes/qiche365/recall.ts b/lib/routes/qiche365/recall.ts new file mode 100644 index 0000000000000..b88733d73ef6e --- /dev/null +++ b/lib/routes/qiche365/recall.ts @@ -0,0 +1,57 @@ +import { Route, Data, DataItem } from '@/types'; +import ofetch from '@/utils/ofetch'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import cache from '@/utils/cache'; + +const baseUrl = 'https://www.qiche365.org.cn'; + +export const route: Route = { + path: '/recall/:channel', + name: '汽车召回', + example: '/qiche365/recall/1', + parameters: { channel: '频道,见下表' }, + description: `| 国内召回新闻 | 国内召回公告 | 国外召回新闻 | 国外召回公告 | + | ------------ | ------------ | ------------ | ------------ | + | 1 | 2 | 3 | 4 |`, + categories: ['government'], + maintainers: ['huanfe1'], + handler, + url: 'qiche365.org.cn/index/recall/index.html', +}; + +async function handler(ctx): Promise { + const { channel } = ctx.req.param(); + const link = `https://www.qiche365.org.cn/index/recall/index/item/${channel}.html?loadmore=1`; + + const html = await cache.tryGet(link, async () => { + const { html } = await ofetch(link, { + method: 'get', + headers: { + 'Accept-Language': 'zh-CN,zh;q=0.9', + }, + }); + return html; + }); + + const $ = load(html); + const items: DataItem[] = $('li') + .toArray() + .map((item) => { + const cheerioItem = $(item); + return { + title: cheerioItem.find('h1').text(), + link: cheerioItem.find('a').attr('href'), + pubDate: parseDate(cheerioItem.find('h2').html()!.match('(.*?)')![1]), + description: cheerioItem.find('p').text().trim(), + author: cheerioItem.find('h3 span').text(), + image: `${baseUrl}${cheerioItem.find('img').attr('src')}`, + }; + }); + return { + allowEmpty: true, + title: ['国内召回公告', '国内召回新闻', '国外召回公告', '国外召回新闻'][channel - 1], + link: 'https://www.qiche365.org.cn/index/recall/index.html', + item: items, + }; +} From d43cb3b42c3081bf7ba8a8cf65fff7f7a63e5a1b Mon Sep 17 00:00:00 2001 From: huanfe1 <41602338+huanfe1@users.noreply.github.com> Date: Fri, 20 Sep 2024 11:57:47 +0800 Subject: [PATCH 2/5] feat: add timezone --- lib/routes/qiche365/recall.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/routes/qiche365/recall.ts b/lib/routes/qiche365/recall.ts index b88733d73ef6e..14d95d713b918 100644 --- a/lib/routes/qiche365/recall.ts +++ b/lib/routes/qiche365/recall.ts @@ -3,6 +3,7 @@ import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; import cache from '@/utils/cache'; +import timezone from '@/utils/timezone'; const baseUrl = 'https://www.qiche365.org.cn'; @@ -42,7 +43,7 @@ async function handler(ctx): Promise { return { title: cheerioItem.find('h1').text(), link: cheerioItem.find('a').attr('href'), - pubDate: parseDate(cheerioItem.find('h2').html()!.match('(.*?)')![1]), + pubDate: timezone(parseDate(cheerioItem.find('h2').html()!.match('(.*?)')![1]), +8), description: cheerioItem.find('p').text().trim(), author: cheerioItem.find('h3 span').text(), image: `${baseUrl}${cheerioItem.find('img').attr('src')}`, From 494dcced37c344a30e8c05c5a672e5227993086d Mon Sep 17 00:00:00 2001 From: huanfe1 <41602338+huanfe1@users.noreply.github.com> Date: Fri, 20 Sep 2024 13:00:36 +0800 Subject: [PATCH 3/5] feat: format url --- lib/routes/qiche365/recall.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/routes/qiche365/recall.ts b/lib/routes/qiche365/recall.ts index 14d95d713b918..46f66b1bfa6ed 100644 --- a/lib/routes/qiche365/recall.ts +++ b/lib/routes/qiche365/recall.ts @@ -42,7 +42,7 @@ async function handler(ctx): Promise { const cheerioItem = $(item); return { title: cheerioItem.find('h1').text(), - link: cheerioItem.find('a').attr('href'), + link: `${baseUrl}${cheerioItem.find('a').attr('href')}`, pubDate: timezone(parseDate(cheerioItem.find('h2').html()!.match('(.*?)')![1]), +8), description: cheerioItem.find('p').text().trim(), author: cheerioItem.find('h3 span').text(), From b61b3f56adfb3beab8b0f339888fd16ea169cd6a Mon Sep 17 00:00:00 2001 From: huanfe1 <41602338+huanfe1@users.noreply.github.com> Date: Fri, 20 Sep 2024 19:33:09 +0800 Subject: [PATCH 4/5] feat: image maybe undefined --- lib/routes/qiche365/recall.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/routes/qiche365/recall.ts b/lib/routes/qiche365/recall.ts index 46f66b1bfa6ed..b4cd25b405c1d 100644 --- a/lib/routes/qiche365/recall.ts +++ b/lib/routes/qiche365/recall.ts @@ -46,13 +46,13 @@ async function handler(ctx): Promise { pubDate: timezone(parseDate(cheerioItem.find('h2').html()!.match('(.*?)')![1]), +8), description: cheerioItem.find('p').text().trim(), author: cheerioItem.find('h3 span').text(), - image: `${baseUrl}${cheerioItem.find('img').attr('src')}`, + image: cheerioItem.find('img').attr('src') && `${baseUrl}${cheerioItem.find('img').attr('src')}`, }; }); return { - allowEmpty: true, title: ['国内召回公告', '国内召回新闻', '国外召回公告', '国外召回新闻'][channel - 1], link: 'https://www.qiche365.org.cn/index/recall/index.html', item: items, + language: 'zh-CN', }; } From 0b7398802ae312ea2b00e6fe6ccb4d71994e4991 Mon Sep 17 00:00:00 2001 From: huanfe1 <41602338+huanfe1@users.noreply.github.com> Date: Fri, 20 Sep 2024 23:34:51 +0800 Subject: [PATCH 5/5] fix: solve review --- lib/routes/qiche365/namespace.ts | 2 +- lib/routes/qiche365/recall.ts | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/routes/qiche365/namespace.ts b/lib/routes/qiche365/namespace.ts index 6328a39e64c21..5809b196c7132 100644 --- a/lib/routes/qiche365/namespace.ts +++ b/lib/routes/qiche365/namespace.ts @@ -1,5 +1,5 @@ import type { Namespace } from '@/types'; export const namespace: Namespace = { - name: '汽车365', + name: '汽车召回网', url: 'qiche365.org.cn', }; diff --git a/lib/routes/qiche365/recall.ts b/lib/routes/qiche365/recall.ts index b4cd25b405c1d..eda92e94b514f 100644 --- a/lib/routes/qiche365/recall.ts +++ b/lib/routes/qiche365/recall.ts @@ -2,7 +2,6 @@ import { Route, Data, DataItem } from '@/types'; import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; -import cache from '@/utils/cache'; import timezone from '@/utils/timezone'; const baseUrl = 'https://www.qiche365.org.cn'; @@ -23,16 +22,12 @@ export const route: Route = { async function handler(ctx): Promise { const { channel } = ctx.req.param(); - const link = `https://www.qiche365.org.cn/index/recall/index/item/${channel}.html?loadmore=1`; - const html = await cache.tryGet(link, async () => { - const { html } = await ofetch(link, { - method: 'get', - headers: { - 'Accept-Language': 'zh-CN,zh;q=0.9', - }, - }); - return html; + const { html } = await ofetch(`${baseUrl}/index/recall/index/item/${channel}.html?loadmore=1`, { + method: 'get', + headers: { + 'Accept-Language': 'zh-CN,zh;q=0.9', + }, }); const $ = load(html); @@ -51,7 +46,7 @@ async function handler(ctx): Promise { }); return { title: ['国内召回公告', '国内召回新闻', '国外召回公告', '国外召回新闻'][channel - 1], - link: 'https://www.qiche365.org.cn/index/recall/index.html', + link: `${baseUrl}/index/recall/index.html`, item: items, language: 'zh-CN', };