-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): add huggingface english blog (#17975)
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |