Skip to content

Commit

Permalink
feat(route): add the hamel blog
Browse files Browse the repository at this point in the history
fix(route): fix pr issue
  • Loading branch information
liyaozhong committed Nov 5, 2024
1 parent a63c8b0 commit d91b940
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lib/routes/hamel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Route, DataItem } from '@/types';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import cache from '@/utils/cache';

export const route: Route = {
path: '/blog',
categories: ['blog'],
example: '/hamel/blog',
radar: [
{
source: ['hamel.dev/'],
},
],
url: 'hamel.dev/',
name: 'Blog',
maintainers: ['liyaozhong'],
handler,
description: "Hamel's Blog Posts",
};

async function handler() {
const rootUrl = 'https://hamel.dev';
const currentUrl = rootUrl;

const response = await got(currentUrl);
const $ = load(response.data);

let items = $('tr[data-index]')
.toArray()
.map((item) => {
const $item = $(item);
const $link = $item.find('td a').last();
const $date = $item.find('.listing-date');

const href = $link.attr('href');
const title = $link.text().trim();
const dateStr = $date.text().trim();

if (!href || !title || !dateStr) {
return null;
}

const link = new URL(href, rootUrl).href;
const pubDate = parseDate(dateStr, 'M/D/YY');

return {
title,
link,
pubDate,
} as DataItem;
})
.filter((item): item is DataItem => item !== null);

items = (
await Promise.all(
items.map((item) =>
cache.tryGet(item.link as string, async () => {
try {
const detailResponse = await got(item.link);
const $detail = load(detailResponse.data);

return {
...item,
description: $detail('.content').html() || '',
} as DataItem;
} catch {
return item;
}
})
)
)
).filter((item): item is DataItem => item !== null);

return {
title: "Hamel's Blog",
link: rootUrl,
item: items,
};
}
7 changes: 7 additions & 0 deletions lib/routes/hamel/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: "Hamel's Blog",
url: 'hamel.dev',
lang: 'en',
};

0 comments on commit d91b940

Please sign in to comment.