Skip to content

Commit

Permalink
feat(route): add nielsberglund (DIYgod#17398)
Browse files Browse the repository at this point in the history
fix(route): fix pr issue
  • Loading branch information
liyaozhong authored Nov 2, 2024
1 parent 6fc4814 commit f17408d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/routes/nielsberglund/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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: '/nielsberglund/blog',
radar: [
{
source: ['nielsberglund.com/'],
},
],
url: 'nielsberglund.com/',
name: 'Blog',
maintainers: ['liyaozhong'],
handler,
description: 'Niels Berglund Blog Posts',
};

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

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

let items = $('.post-preview')
.toArray()
.map((item) => {
const $item = $(item);
const $link = $item.find('a').first();
const href = $link.attr('href');
const title = $item.find('.post-title').first().text().trim();
const dateStr = $item.find('.post-meta').text().trim();

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

const link = new URL(href, rootUrl).href;
const pubDate = parseDate(dateStr);

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);

item.description = $detail('.post-container').html() || '';

return item;
} catch {
return item;
}
})
)
)
).filter((item): item is DataItem => item !== null);

return {
title: 'Niels Berglund Blog',
link: rootUrl,
item: items,
};
}
7 changes: 7 additions & 0 deletions lib/routes/nielsberglund/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: 'Niels Berglund Blog',
url: 'nielsberglund.com',
lang: 'en',
};

0 comments on commit f17408d

Please sign in to comment.