Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add route of qiche365 #16834

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/routes/qiche365/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '汽车365',
huanfe1 marked this conversation as resolved.
Show resolved Hide resolved
url: 'qiche365.org.cn',
};
58 changes: 58 additions & 0 deletions lib/routes/qiche365/recall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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';

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<Data> {
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;
});
huanfe1 marked this conversation as resolved.
Show resolved Hide resolved

const $ = load(<string>html);
const items: DataItem[] = $('li')
.toArray()
.map((item) => {
const cheerioItem = $(item);
return {
title: cheerioItem.find('h1').text(),
link: `${baseUrl}${cheerioItem.find('a').attr('href')}`,
pubDate: timezone(parseDate(cheerioItem.find('h2').html()!.match('</i>(.*?)<b>')![1]), +8),
description: cheerioItem.find('p').text().trim(),
author: cheerioItem.find('h3 span').text(),
image: cheerioItem.find('img').attr('src') && `${baseUrl}${cheerioItem.find('img').attr('src')}`,
};
});
return {
title: ['国内召回公告', '国内召回新闻', '国外召回公告', '国外召回新闻'][channel - 1],
link: 'https://www.qiche365.org.cn/index/recall/index.html',
item: items,
language: 'zh-CN',
};
}