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

fix(route): 中债资信评估有限责任公司中债研究 #18076

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all 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
71 changes: 63 additions & 8 deletions lib/routes/chinaratings/credit-research.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { type CheerioAPI, type Cheerio, type Element, load } from 'cheerio';
import { type Context } from 'hono';

import { type DataItem, type Route, type Data, ViewType } from '@/types';
import { type Data, type DataItem, type Route, ViewType } from '@/types';

import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

import { type CheerioAPI, type Cheerio, type Element, load } from 'cheerio';
import { type Context } from 'hono';

export const handler = async (ctx: Context): Promise<Data> => {
const { category = 'Industry/Comment' } = ctx.req.param();
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '15', 10);
Expand All @@ -15,9 +16,11 @@ export const handler = async (ctx: Context): Promise<Data> => {

const response = await ofetch(targetUrl);
const $: CheerioAPI = load(response);
const language: string = $('html').attr('lang') ?? 'zh-CN';
const language = 'zh-CN';

const items: DataItem[] = $('div.contRight ul.list li')
let items: DataItem[] = [];

items = $('div.contRight ul.list li')
.slice(0, limit)
.toArray()
.map((el): Element => {
Expand All @@ -38,8 +41,60 @@ export const handler = async (ctx: Context): Promise<Data> => {
};

return processedItem;
})
.filter((_): _ is DataItem => true);
});

items = (
await Promise.all(
items.map((item) => {
if (!item.link) {
return item;
}

return cache.tryGet(item.link, async (): Promise<DataItem> => {
const detailResponse = await ofetch(item.link);
const $$: CheerioAPI = load(detailResponse);

const title: string = $$('div.newshead h2, div.title h3').text();
const description: string = $$('div.news div.content').html() ?? '';

const metaStr: string = $$('div.newshead p span, div.title p span').text();
const pubDateStr: string | undefined = metaStr?.match(/(\d{4}-\d{2}-\d{2})/)?.[1];
const authors: DataItem['author'] = metaStr?.match(/来源:(.*?)/)?.[1];
const upDatedStr: string | undefined = pubDateStr;

let processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate,
author: authors,
content: {
html: description,
text: description,
},
updated: upDatedStr ? parseDate(upDatedStr) : item.updated,
language,
};

const docUrl: string | undefined = detailResponse.match(/(\/upload\/docs\/\d{4}-\d{2}-\d{2}\/doc_\d+)"/)?.[1];
const enclosureUrl: string | undefined = docUrl ? `${new URL(docUrl, baseUrl).href}.pdf` : undefined;

if (enclosureUrl) {
processedItem = {
...processedItem,
enclosure_url: enclosureUrl,
enclosure_type: 'application/pdf',
enclosure_title: title,
};
}

return {
...item,
...processedItem,
};
});
})
)
).filter((_): _ is DataItem => true);

const title: string = $('title').text();

Expand Down
Loading