Skip to content
Open
Show file tree
Hide file tree
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
103 changes: 103 additions & 0 deletions lib/routes/humanlayer/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { load } from 'cheerio';

import type { DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/blog',
categories: ['blog'],
example: '/humanlayer/blog',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.humanlayer.dev/blog'],
target: '/humanlayer/blog',
},
],
name: 'Blog',
maintainers: ['zj1123581321'],
handler,
url: 'www.humanlayer.dev/blog',
};

async function handler(ctx) {
const baseUrl = 'https://www.humanlayer.dev';
const listUrl = `${baseUrl}/blog`;
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20;

const response = await ofetch(listUrl);
const $ = load(response);

const list = $('a.block.py-2.group[href^="/blog/"]:not([href^="/blog/tags/"])')
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again redundant filtering using not(). Provide a screenshot if the site does return a[href^="/blog/tags/"] to you with class name block py-2 group.

Here's what I'm seeing Image

.toArray()
.slice(0, limit)
.map((el) => {
const $el = $(el);
const href = $el.attr('href')!;
const title = $el.find('h2').text().trim();
const metaLine = $el.find('p.text-sm').text().trim();
const description = $el.find('p[style]').text().trim();

// meta format: "Author · Date · Read time · #tag1 #tag2"
const parts = metaLine.split('·').map((s) => s.trim());
const author = parts[0] || '';
const dateStr = parts[1] || '';
const category = parts
.slice(3)
Comment on lines +55 to +56
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use parts[3] instead of copying it again.

.join(' ')
.match(/#\w+/g)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/#\w+/ cannot match words after - which you can see from the results.

?.map((t) => t.slice(1));

return {
title,
link: `${baseUrl}${href}`,
author,
description,
pubDate: dateStr ? parseDate(dateStr) : undefined,
category,
} as DataItem;
});

const items = (await Promise.all(
list.map((item) =>
cache.tryGet(item.link!, async () => {
const resp = await ofetch(item.link!);
const $detail = load(resp);

const ogTitle = $detail('meta[property="og:title"]').attr('content');
const ogDesc = $detail('meta[property="og:description"]').attr('content');
const publishedTime = $detail('meta[property="article:published_time"]').attr('content');
const ogAuthor = $detail('meta[property="article:author"]').attr('content');
const ogImage = $detail('meta[property="og:image"]').attr('content');

const content = $detail('div.prose').html();

return {
...item,
title: ogTitle || item.title,
description: content || ogDesc || item.description,
pubDate: publishedTime ? parseDate(publishedTime) : item.pubDate,
author: ogAuthor || item.author,
banner: ogImage,
} as DataItem;
})
)
)) as DataItem[];

return {
title: 'HumanLayer Blog',
link: listUrl,
language: 'en',
item: items,
};
}
7 changes: 7 additions & 0 deletions lib/routes/humanlayer/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: 'HumanLayer',
url: 'www.humanlayer.dev',
lang: 'en',
};
Loading