feat(route): add Fermilab News (#7371)

Co-authored-by: NeverBehave <gayhub@never.pet>
This commit is contained in:
Ethan Shen 2021-08-12 15:39:02 +08:00 committed by GitHub
parent d5e78e712c
commit c7169f4021
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 0 deletions

View File

@ -175,6 +175,18 @@ Compared to the official one, the RSS feed generated by RSSHub not only has more
</RouteEn>
## Fermilab
### News
<RouteEn author="nczitzk" example="/fnal/news" path="/fnal/news/:category?" :paramsDesc="['Category, see below, All News by default']">
| All News | Fermilab features | Press releases | Symmetry features |
| -------- | ----------------- | -------------- | ----------------- |
| allnews | 269 | 55 | 12580 |
</RouteEn>
## Google News
### News

View File

@ -1093,6 +1093,18 @@ area 分区选项
<Route author="emdoe" example="/plainlaw/archives" path="/plainlaw/archives"/>
## 费米实验室
### 新闻
<Route author="nczitzk" example="/fnal/news" path="/fnal/news/:category?" :paramsDesc="['分类,见下表,默认为 All News']">
| All News | Fermilab features | Press releases | Symmetry features |
| -------- | ----------------- | -------------- | ----------------- |
| allnews | 269 | 55 | 12580 |
</Route>
## 飞雪娱乐网
<Route author="nczitzk" example="/feixuew/rj" path="/feixuew/:id?" :paramsDesc="['分类 id可在对应分类页面的 URL 中找到,默认为首页最近更新']">

View File

@ -4158,8 +4158,12 @@ router.get('/tanchinese/:category?', require('./routes/tanchinese'));
// Harvard
router.get('/harvard/health/blog', require('./routes/universities/harvard/health/blog'));
// 费米实验室
router.get('/fnal/news/:category?', require('./routes/fnal/news'));
// X410
router.get('/x410/news', require('./routes/x410/news'));
// 恩山无线论坛
router.get('/right/forum/:id?', require('./routes/right/forum'));

51
lib/routes/fnal/news.js Normal file
View File

@ -0,0 +1,51 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const category = ctx.params.category || 'allnews';
const rootUrl = 'https://news.fnal.gov';
const currentUrl = `${rootUrl}/newsroom/news/?se=&c=${category}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('.post-title a')
.map((_, item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
})
.get();
const items = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.author = content('.author').text();
item.description = content('.entry-content, .article-content').html();
item.pubDate = content('meta[property="article:published_time"]').length > 0 ? Date.parse(content('meta[property="article:published_time"]').attr('content')) : parseDate(content('.teaser-date').text(), 'MM/DD/YY');
return item;
})
)
);
ctx.state.data = {
title: `${$('title').text()} - Fermilab`,
link: currentUrl,
item: items,
};
};