feat: 增加北京林业大学绿色新闻网,国家留学网新闻资讯与新闻公告 (#4892)

This commit is contained in:
markmingjie
2020-06-02 11:43:52 +08:00
committed by GitHub
parent 906b47fd09
commit e331803be2
7 changed files with 153 additions and 16 deletions

View File

@@ -263,11 +263,11 @@ type 为 all 时category 参数不支持 cost 和 free
### 通知
<Route author="Derekmini" example="/csc/notice/lxtz" path="/csc/notice/:type?" :paramsDesc="['分类, 默认为 `lxtz`']" radar="1">
<Route author="Derekmini markmingjie" example="/csc/notice/lxtz" path="/csc/notice/:type?" :paramsDesc="['分类, 默认为 `lxtz`']" radar="1">
| 遴选通知 | 综合项目专栏 | 常见问题解答 | 录取公告 |
| -------- | ------------ | ------------ | -------- |
| lxtz | xmzl | wtjd | lqgg |
| 遴选通知 | 综合项目专栏 | 常见问题解答 | 录取公告 | 新闻资讯 | 新闻公告 |
| -------- | ------------ | ------------ | -------- | -------- | -------- |
| lxtz | xmzl | wtjd | lqgg | xwzx | xwgg |
</Route>

View File

@@ -216,6 +216,18 @@ count 的取值范围为 1-12为防止请求次数过多推荐设置为 5
| <http://science.bio1000.com/ecology-environment> | /biobio/science/ecology-environment |
| <http://www.bio1000.com/gnjz> | /biobio/gnjz |
## 生物谷
### 所有栏目
<Route author="xfangbao" example="/shengwugu/biology" path="/shengwugu/:uid/" :paramsDesc="['分栏代码, 可在 URL 找到']" />
具体栏目编号,去网站上看标签
| 网址 | 对应路由 |
| ------------------------------- | ------------------ |
| <http://news.bioon.com/biology> | /shengwugu/biology |
## 书趣阁
### 小说更新
@@ -267,15 +279,3 @@ count 的取值范围为 1-12为防止请求次数过多推荐设置为 5
### 主页
<Route author="kt286" example="/zreading" path="/zreading" />
## 生物谷
### 所有栏目
<Route author="xfangbao" example="/shengwugu/biology" path="/shengwugu/:uid/" :paramsDesc="['分栏代码, 可在 URL 找到']" />
具体栏目编号,去网站上看标签
| 网址 | 对应路由 |
| -------- | -------- |
| http://news.bioon.com/biology | /shengwugu/biology |

View File

@@ -158,6 +158,16 @@ pageClass: routes
## 北京林业大学
### 绿色新闻网
<Route author="markmingjie" example="/bjfu/news/lsyw" path="/bjfu/news/:type" :paramsDesc="['新闻栏目']">
| 绿色要闻 | 校园动态 | 教学科研 | 党建思政 | 一周排行 |
| -------- | -------- | -------- | -------- | -------- |
| lsyw | lsxy | jxky | djsz | yzph |
</Route>
### 研究生院培养动态
<Route author="markmingjie" example="/bjfu/grs" path="/bjfu/grs" />

View File

@@ -558,6 +558,7 @@ router.get('/zdfx', require('./routes/galgame/zdfx'));
// 北京林业大学
router.get('/bjfu/grs', require('./routes/universities/bjfu/grs'));
router.get('/bjfu/jwc/:type', require('./routes/universities/bjfu/jwc/index'));
router.get('/bjfu/news/:type', require('./routes/universities/bjfu/news/index'));
// 北京理工大学
router.get('/bit/jwc', require('./routes/universities/bit/jwc/jwc'));

View File

@@ -22,6 +22,14 @@ const typeMap = {
name: '录取公告',
url: '/chuguo/list/28',
},
xwzx: {
name: '新闻资讯',
url: '/news',
},
xwgg: {
name: '新闻公告',
url: '/news/gonggao',
},
};
module.exports = async (ctx) => {

View File

@@ -0,0 +1,55 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const util = require('./utils');
const iconv = require('iconv-lite'); // 转码
module.exports = async (ctx) => {
const type = ctx.params.type;
let title, path;
switch (type) {
case 'lsyw':
title = '绿色要闻';
path = 'lsyw/';
break;
case 'xydt':
title = '校园动态';
path = 'lsxy/';
break;
case 'jxky':
title = '教学科研';
path = 'jxky/';
break;
case 'djsz':
title = '党建思政';
path = 'djsz/';
break;
case 'yzph':
title = '一周排行';
path = 'yzph/';
break;
default:
title = '绿色要闻';
path = 'lsyw/';
}
const base = 'http://news.bjfu.edu.cn/' + path;
const response = await got({
method: 'get',
responseType: 'buffer', // 转码
url: base,
});
const data = iconv.decode(response.data, 'gb2312'); // 转码
const $ = cheerio.load(data);
const list = $('.news_ul li').slice(0, 10).get();
const result = await util.ProcessFeed(base, list, ctx.cache); // 感谢@hoilc指导
ctx.state.data = {
title: '北林新闻- ' + title,
link: 'http://news.bjfu.edu.cn/' + path,
description: '绿色新闻网 - ' + title,
item: result,
};
};

View File

@@ -0,0 +1,63 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite'); // 转码
// 完整文章页
async function load(link) {
const response = await got.get(link, {
responseType: 'buffer',
});
const data = iconv.decode(response.data, 'gb2312'); // 转码
// 加载文章内容
const $ = cheerio.load(data);
// 解析日期
const date = new Date(
$('.article')
.text()
.match(/\d{4}-\d{2}-\d{2}/)
);
const timeZone = 8;
const serverOffset = date.getTimezoneOffset() / 60;
const pubDate = new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString();
// 提取内容
const description = $('.article_con').html();
// 返回解析的结果
return { description, pubDate };
}
const ProcessFeed = async (base, list, caches) =>
// 使用 Promise.all() 进行 async 并发
await Promise.all(
// 遍历每一篇文章
list.map(async (item) => {
const $ = cheerio.load(item);
const $title = $('a');
// 还原相对链接为绝对链接
const itemUrl = new URL($title.attr('href'), base).href; // 感谢@hoilc指导
// 列表上提取到的信息
const single = {
title: $title.text(),
link: itemUrl,
author: '绿色新闻网',
guid: itemUrl,
};
// 使用tryGet方法从缓存获取内容。
// 当缓存中无法获取到链接内容的时候则使用load方法加载文章内容。
const other = await caches.tryGet(itemUrl, async () => await load(itemUrl));
// 合并解析后的结果集作为该篇文章最终的输出结果
return Promise.resolve(Object.assign({}, single, other));
})
);
module.exports = {
ProcessFeed,
};