feat(route): add 信息学院路由 (#7777)

* feat:add 信息学院路由

* refactor: migrate to v2

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
This commit is contained in:
Elliott
2022-02-22 22:48:59 +08:00
committed by GitHub
co-authored by TonyRL
parent f6fd024df9
commit 7dd591ca82
13 changed files with 208 additions and 23 deletions
-65
View File
@@ -1,65 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
module.exports = async (ctx) => {
const url = 'http://graduate.bjfu.edu.cn/pygl/pydt/index.html';
const response = await got.get(url, {
responseType: 'buffer',
});
const data = iconv.decode(response.data, 'gb2312');
const $ = cheerio.load(data);
const list = $('.itemList li')
.slice(0, 10)
.map((i, e) => {
const element = $(e);
const title = element.find('li a').attr('title');
const link = element.find('li a').attr('href');
const date = new Date(
element
.find('li a')
.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();
return {
title,
description: '',
link: 'http://graduate.bjfu.edu.cn/pygl/pydt/' + link,
author: '北京林业大学研究生院培养动态',
pubDate,
};
})
.get();
const result = await Promise.all(
list.map(async (item) => {
const link = item.link;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemReponse = await got.get(link, {
responseType: 'buffer',
});
const data = iconv.decode(itemReponse.data, 'gb2312');
const itemElement = cheerio.load(data);
item.description = itemElement('.articleTxt').html();
ctx.cache.set(link, JSON.stringify(item));
return item;
})
);
ctx.state.data = {
title: '北林研培养动态',
link: url,
item: result,
};
};
-55
View File
@@ -1,55 +0,0 @@
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 'jwkx':
title = '教务快讯';
path = 'jwkx/';
break;
case 'jgdt':
title = '教改动态';
path = 'jgdt/';
break;
case 'ksxx':
title = '考试信息';
path = 'ksxx/';
break;
case 'kcxx':
title = '课程信息';
path = 'tkxx/';
break;
case 'tpxw':
title = '图片新闻';
path = 'tpxw/';
break;
default:
title = '教务快讯';
path = 'jwkx/';
}
const base = 'http://jwc.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 = $('.list_c li').slice(0, 10).get();
const result = await util.ProcessFeed(base, list, ctx.cache); // 感谢@hoilc指导
ctx.state.data = {
title: '北林教务处 - ' + title,
link: 'http://jwc.bjfu.edu.cn/' + path,
description: '北京林业大学教务处 - ' + title,
item: result,
};
};
-62
View File
@@ -1,62 +0,0 @@
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(
$('div #con_djl')
.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 = $('#con_c').html();
// 返回解析的结果
return { description, pubDate };
}
const ProcessFeed = (base, list, caches) =>
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, () => load(itemUrl));
// 合并解析后的结果集作为该篇文章最终的输出结果
return Promise.resolve(Object.assign({}, single, other));
})
);
module.exports = {
ProcessFeed,
};
-65
View File
@@ -1,65 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
module.exports = async (ctx) => {
const url = 'http://kyc.bjfu.edu.cn/tztg/index.html';
const response = await got.get(url, {
responseType: 'buffer',
});
const data = iconv.decode(response.data, 'gb2312');
const $ = cheerio.load(data);
const list = $('.ll_con_r_b li')
.slice(0, 10)
.map((i, e) => {
const element = $(e);
const title = element.find('.ll_con_r_b_title a').text();
const link = element.find('a').attr('href');
const date = new Date(
element
.find('.ll_con_r_b_time')
.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();
return {
title,
description: '',
link: 'http://kyc.bjfu.edu.cn/tztg/' + link,
author: '北京林业大学科技处通知公告',
pubDate,
};
})
.get();
const result = await Promise.all(
list.map(async (item) => {
const link = item.link;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemReponse = await got.get(link, {
responseType: 'buffer',
});
const data = iconv.decode(itemReponse.data, 'gb2312');
const itemElement = cheerio.load(data);
item.description = itemElement('#a_con_l_con').html();
ctx.cache.set(link, JSON.stringify(item));
return item;
})
);
ctx.state.data = {
title: '北林科技处通知',
link: url,
item: result,
};
};
@@ -1,55 +0,0 @@
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,
};
};
@@ -1,62 +0,0 @@
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 = (base, list, caches) =>
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, () => load(itemUrl));
// 合并解析后的结果集作为该篇文章最终的输出结果
return Promise.resolve(Object.assign({}, single, other));
})
);
module.exports = {
ProcessFeed,
};