Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions lib/routes/53ai/namespace.ts
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',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
name: '53ai',
name: '53AI',

url: '53ai.com',
description: `53AI是一个人工智能技术社区,致力于推动人工智能技术的发展和应用。`,

zh: {
name: '53AI',
},
};
66 changes: 66 additions & 0 deletions lib/routes/53ai/news.ts
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 = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing categories

path: '/news',
name: '53AI - AI News Feed',
Copy link
Collaborator

Choose a reason for hiding this comment

The 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'],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
maintainers: ['houzl'],
maintainers: ['houzl3416'],

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
Loading