Skip to content

Commit

Permalink
feat(route): add huggingface english blog (#17975)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesaryuan authored Dec 26, 2024
1 parent cc4c5d9 commit bca47a1
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions lib/routes/huggingface/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Route, type DataItem } from '@/types';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/blog',
categories: ['programming'],
example: '/huggingface/blog',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['huggingface.co/blog', 'huggingface.co/'],
},
],
name: '英文博客',
maintainers: ['cesaryuan', 'zcf0508'],
handler,
url: 'huggingface.co/blog',
};

interface Author {
user: string;
guest: boolean;
org?: string;
}

interface Blog {
authors: Author[];
canonical: boolean;
isUpvotedByUser: boolean;
publishedAt: string;
slug: string;
title: string;
upvotes: number;
thumbnail: string;
guest: boolean;
}

interface BlogData {
blog: Blog;
blogUrl: string;
lang: string;
loggedInUser: string;
}

async function handler() {
const { body: response } = await got('https://huggingface.co/blog');
const $ = load(response);

/** @type {Array<{blog: {local: string, title: string, author: string, thumbnail: string, date: string, tags: Array<string>}, blogUrl: string, lang: 'zh', link: string}>} */
const papers = $('div[data-target="BlogThumbnail"]')
.toArray()
.map((item) => {
const props = $(item).data('props') as BlogData;
const link = $(item).find('a').attr('href');
return {
...props,
link,
};
});

const items: DataItem[] = papers.map((item) => ({
title: item.blog.title,
link: `https://huggingface.co${item.link}`,
pubDate: parseDate(item.blog.publishedAt),
author: item.blog.authors.map((author) => ({
name: author.user,
})),
upvotes: item.blog.upvotes,
image: new URL(item.blog.thumbnail, 'https://huggingface.co').toString(),
}));

return {
title: 'Huggingface 英文博客',
link: 'https://huggingface.co/blog',
item: items,
};
}

0 comments on commit bca47a1

Please sign in to comment.