-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): APNIC Blog 全文 RSS (#17839)
- Loading branch information
Showing
2 changed files
with
69 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,61 @@ | ||
import { Route } from '@/types'; | ||
|
||
import cache from '@/utils/cache'; | ||
import got from '@/utils/got'; | ||
import { load } from 'cheerio'; | ||
import { parseDate } from '@/utils/parse-date'; | ||
|
||
export const route: Route = { | ||
path: '/blog', | ||
categories: ['blog'], | ||
example: '/apnic/blog', | ||
url: 'blog.apnic.net', | ||
name: 'Blog', | ||
maintainers: ['p3psi-boo'], | ||
handler, | ||
}; | ||
|
||
async function handler() { | ||
const baseUrl = 'https://blog.apnic.net'; | ||
const feedUrl = `${baseUrl}/feed/`; | ||
|
||
const response = await got(feedUrl); | ||
const $ = load(response.data, { xmlMode: true }); | ||
|
||
// 从 RSS XML 中直接提取文章信息 | ||
const list = $('item') | ||
.toArray() | ||
.map((item) => { | ||
const $item = $(item); | ||
return { | ||
title: $item.find('title').text(), | ||
link: $item.find('link').text(), | ||
author: $item.find(String.raw`dc\:creator`).text(), | ||
category: | ||
$item | ||
.find('category') | ||
.text() | ||
.match(/>([^<]+)</)?.[1] || '', | ||
pubDate: parseDate($item.find('pubDate').text()), | ||
}; | ||
}); | ||
|
||
const items = await Promise.all( | ||
list.map((item) => | ||
cache.tryGet(item.link, async () => { | ||
const { data: articleData } = await got(item.link); | ||
const $article = load(articleData); | ||
|
||
// 获取文章正文内容 | ||
item.description = $article('.entry-content').html(); | ||
return item; | ||
}) | ||
) | ||
); | ||
|
||
return { | ||
title: 'APNIC Blog', | ||
link: baseUrl, | ||
item: items, | ||
}; | ||
} |
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,8 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: 'APNIC', | ||
url: 'blog.apnic.net', | ||
description: 'Asia-Pacific Network Information Centre', | ||
lang: 'en', | ||
}; |