Skip to content

Commit

Permalink
refactor(route/hex-rays): update data acquisition method (#17130)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mas0nShi authored Oct 14, 2024
1 parent 74f9863 commit e953515
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions lib/routes/hex-rays/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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',
};
}

0 comments on commit e953515

Please sign in to comment.