refactor(route/hex-rays): update data acquisition method (#17130)

This commit refactors the data acquisition method in the hex-rays route. It adds type annotations for the Data, DataItem, and Route types. It also imports the Context type from the hono library. The maintainers list is updated to include Mas0n. The handler function now takes a Context parameter and returns a Promise<Data>. The link to fetch data is updated to 'https://hex-rays.com/blog/'. The parsing logic for the list of articles is modified to use the correct selectors. The category and description properties of each article are updated to match the new HTML structure. The image property is added to the returned data object.
This commit is contained in:
Mas0nShi 2024-10-15 02:24:36 +08:00 committed by GitHub
parent 74f9863a19
commit e953515cd2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 23 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import { Route } from '@/types';
import type { Data, DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
@ -23,51 +23,45 @@ export const route: Route = {
},
],
name: 'Hex-Rays News',
maintainers: ['hellodword ', 'TonyRL'],
maintainers: ['hellodword ', 'TonyRL', 'Mas0n'],
handler,
url: 'hex-rays.com/',
};
async function handler() {
const link = 'https://www.hex-rays.com/blog/';
async function handler(/* ctx*/): Promise<Data> {
const link = 'https://hex-rays.com/blog/';
const response = await got.get(link);
const $ = load(response.data);
const list = $('.post-list-container')
.map((_, ele) => ({
title: $('h3 > a', ele).text(),
link: $('h3 > a', ele).attr('href'),
pubDate: parseDate($('.post-meta:nth-of-type(1)', ele).first().text().trim().replace('Posted on:', '')),
author: $('.post-meta:nth-of-type(2)', ele).first().text().replace('By:', '').trim(),
}))
.get();
const list: DataItem[] = $('.article ')
.toArray()
.map(
(ele): DataItem => ({
title: $('h2 > a', ele).text(),
link: $('h2 > a', ele).attr('href'),
pubDate: parseDate($('div.by-line > time', ele).attr('datetime')!),
author: $('div.by-line > a', ele).text(),
})
);
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const items: DataItem[] = await Promise.all(
list.map((item: DataItem) =>
cache.tryGet(item.link!, async () => {
const detailResponse = await got.get(item.link);
const content = load(detailResponse.data);
item.category = (
content('.category-link')
.toArray()
.map((e) => $(e).text()) +
',' +
content('.tag-link')
.toArray()
.map((e) => $(e).text())
).split(',');
item.description = content('.post-content').html();
item.category = content('.div.topics > a')
.toArray()
.map((ele) => content(ele).text());
item.description = content('.post-body').toString();
return item;
})
)
) as Promise<DataItem>[]
);
return {
title: 'Hex-Rays Blog',
link,
item: items,
image: 'https://hex-rays.com/hubfs/Ico-logo.png',
};
}