-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add 53ai/news route. #16943
base: master
Are you sure you want to change the base?
feat: add 53ai/news route. #16943
Changes from all commits
5c3d2a1
76abee7
197ebf6
118feff
d23655f
3e4ae57
2c21858
3df0c95
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: '53ai', | ||
url: '53ai.com', | ||
description: `53AI是一个人工智能技术社区,致力于推动人工智能技术的发展和应用。`, | ||
|
||
zh: { | ||
name: '53AI', | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||
import { Route } from '@/types'; | ||||||
import ofetch from '@/utils/ofetch'; | ||||||
import { load } from 'cheerio'; | ||||||
import { parseDate } from '@/utils/parse-date'; | ||||||
|
||||||
export const route: Route = { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||||||
path: '/news', | ||||||
name: '53AI - AI News Feed', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to include namespace's name in route name. |
||||||
description: 'RSS feed for 53AI news articles from the last 3 days.', | ||||||
maintainers: ['houzl'], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
example: '/53ai/news', | ||||||
handler: async () => { | ||||||
const baseUrl = 'https://www.53ai.com'; | ||||||
const threeDaysAgo = new Date(Date.now() - 3 * 24 * 60 * 60 * 1000); | ||||||
|
||||||
const fetchPage = async (page: number) => { | ||||||
const url = page === 1 ? `${baseUrl}/news.html` : `${baseUrl}/news.html?page=${page}`; | ||||||
const response = await ofetch(url); | ||||||
const $ = load(response); | ||||||
return $('.news .news-box .new-left ul.list li') | ||||||
.toArray() | ||||||
.map((element) => { | ||||||
const item = $(element); | ||||||
const a = item.find('a.item').first(); | ||||||
const title = a.find('.title span').text(); | ||||||
const href = a.attr('href'); | ||||||
if (!href) { | ||||||
throw new Error('链接不存在'); | ||||||
} | ||||||
const link = new URL(href, baseUrl).href; | ||||||
const desc = a.find('.desc').text().trim(); | ||||||
const pubDate = parseDate(a.find('.release-date span').eq(1).text().trim()); | ||||||
return { | ||||||
title, | ||||||
link, | ||||||
description: desc, | ||||||
pubDate, | ||||||
}; | ||||||
}); | ||||||
}; | ||||||
const fetchAllPages = async (page: number, accumulatedItems: NewsItem[] = []): Promise<NewsItem[]> => { | ||||||
const pageItems: NewsItem[] = await fetchPage(page); | ||||||
const newItems: NewsItem[] = [...accumulatedItems, ...pageItems]; | ||||||
const lastItem: NewsItem | undefined = pageItems.at(-1); | ||||||
|
||||||
return pageItems.length === 0 || (lastItem && lastItem.pubDate < threeDaysAgo) ? newItems : fetchAllPages(page + 1, newItems); | ||||||
}; | ||||||
|
||||||
const items = await fetchAllPages(1); | ||||||
|
||||||
// 过滤掉超过3天的项目 | ||||||
const filteredItems = items.filter((item) => item.pubDate >= threeDaysAgo); | ||||||
Comment on lines
+41
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No page turn. Extract content from the first page is enough. |
||||||
|
||||||
return { | ||||||
title: '53AI - AI News Feed (Last 3 Days)', | ||||||
link: `${baseUrl}/news.html`, | ||||||
item: filteredItems, | ||||||
}; | ||||||
}, | ||||||
}; | ||||||
|
||||||
interface NewsItem { | ||||||
pubDate: Date; | ||||||
title?: string; | ||||||
content?: string; | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.