From 7a6849781494dbd213ad4c2d6a8b806bb9578f89 Mon Sep 17 00:00:00 2001 From: AngUOI <2503894740@QQ.COM> Date: Wed, 19 Feb 2020 23:02:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B4=9B=E9=98=B3=E7=90=86=E5=B7=A5?= =?UTF-8?q?=E5=AD=A6=E9=99=A2=20RSS=20(#4027)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/university.md | 16 ++--- lib/routes/universities/lit/jwc.js | 76 +++++++++++------------ lib/routes/universities/lit/tw.js | 95 +++++++++++++---------------- lib/routes/universities/lit/xwzx.js | 85 ++++++++++++++++---------- 4 files changed, 137 insertions(+), 135 deletions(-) diff --git a/docs/university.md b/docs/university.md index 1934bde939..c496be71ff 100644 --- a/docs/university.md +++ b/docs/university.md @@ -530,21 +530,21 @@ category 列表: ### 新闻中心 - + -| 公告通知 | 新闻快讯 | 学术信息 | 媒体新闻 | -| -------- | -------- | -------- | -------- | -| ggtz | xwkx | xsxx | mtxw | +| 全部 | 公告通知 | 新闻快讯 | 学术信息 | 媒体新闻 | +| ---- | -------- | -------- | -------- | -------- | +| all | ggtz | xwkx | xsxx | mtxw | ### 团委 - + -| 团内通知 | 青年快讯 | 理论学习 | -| -------- | -------- | -------- | -| tntz | qnkx | llxx | +| 全部 | 团内通知 | 青年快讯 | +| ---- | -------- | -------- | +| all | tntz | qnkx | diff --git a/lib/routes/universities/lit/jwc.js b/lib/routes/universities/lit/jwc.js index a58cb520ae..be67b23af5 100644 --- a/lib/routes/universities/lit/jwc.js +++ b/lib/routes/universities/lit/jwc.js @@ -1,55 +1,49 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const url = require('url'); const baseUrl = 'http://www.lit.edu.cn/jwc/'; module.exports = async (ctx) => { - const response = await got({ - method: 'get', - url: baseUrl, - }); + const response = await got.get(baseUrl); const $ = cheerio.load(response.data); - const items = []; + const list = $('article.post_box').get(); - // 获取头条文章 - $('article.ulist').each(function() { - const list = $(this); - items.push({ - title: `【头条】 ` + list.find('a').text(), - description: list.find('a').text(), - pubDate: list - .find('span') - .eq(-1) - .text(), - link: baseUrl + list.find('a').attr('href'), - }); - }); + const out = await Promise.all( + list.map(async (item) => { + item = $(item); + const link = url.resolve(baseUrl, item.find('a').attr('href')); + const single = { + title: item.find('a').attr('title'), + link: link, + }; - // 获取其它文章 - $('article.post_box').each(function() { - const list = $(this); - const ddate = list - .find('.c-top') - .children('div') - .text() - .trim(); - items.push({ - title: list.find('a').attr('title'), - description: list.children('.c-con').text(), - pubDate: ddate.substring(0, 4) + `-` + ddate.substring(4), - link: baseUrl + list.find('a').attr('href'), - }); - }); + const other = await ctx.cache.tryGet(link, async () => { + const result = await got.get(link); + const $ = cheerio.load(result.data); + + $('img[src="/system/resource/images/fileTypeImages/icon_xls.gif"]').remove(); + $('img[src="/system/resource/images/fileTypeImages/icon_doc.gif"]').remove(); + $('img[src="/system/resource/images/fileTypeImages/icon_ppt.gif"]').remove(); + + const description = $('article.entry-content').html(); + const pubDate = $('time').text(); + const author = $('aside > span:nth-child(1)').text(); + return { + description, + pubDate, + author, + }; + }); + + return Promise.resolve(Object.assign({}, single, other)); + }) + ); ctx.state.data = { - title: `洛理教务在线`, + title: `教务处 - 洛阳理工学院`, link: baseUrl, - description: `洛阳理工教务在线RSS,规范管理 用心服务 欢迎您访问!`, - item: items.map((item) => ({ - title: item.title, - description: item.description, - pubDate: item.pubDate, - link: item.link, - })), + description: `洛阳理工教务在线 RSS`, + item: out, }; }; diff --git a/lib/routes/universities/lit/tw.js b/lib/routes/universities/lit/tw.js index 37abc325ac..2f9b09f80d 100644 --- a/lib/routes/universities/lit/tw.js +++ b/lib/routes/universities/lit/tw.js @@ -3,67 +3,56 @@ const cheerio = require('cheerio'); const url = require('url'); const baseUrl = 'http://www.lit.edu.cn/tw/'; - const nameProps = { tntz: '团内通知', qnkx: '青年快讯', - llxx: '理论学习', }; module.exports = async (ctx) => { - const name = ctx.params.name || 'tntz'; - const u = url.resolve(baseUrl, `${name}.htm`); - const response = await got({ - method: 'get', - url: u, - }); - + const name = ctx.params.name || 'all'; + const urla = name === 'all' ? baseUrl : url.resolve(baseUrl, `${name}.htm`); + const response = await got.get(urla); const $ = cheerio.load(response.data); + const list = + name === 'all' + ? $('.con_wap') + .find('li') + .get() + : $('ul.caselist li').get(); + + const out = await Promise.all( + list.map(async (item) => { + item = $(item); + const link = url.resolve(baseUrl, item.find('a').attr('href')); + const pubDate = name === 'all' ? item.find('span').text() : item.children('.time').text(); + + const other = await ctx.cache.tryGet(link, async () => { + const result = await got.get(link); + const $ = cheerio.load(result.data); + $('img[src="/system/resource/images/fileTypeImages/icon_xls.gif"]').remove(); + $('img[src="/system/resource/images/fileTypeImages/icon_doc.gif"]').remove(); + $('img[src="/system/resource/images/fileTypeImages/icon_ppt.gif"]').remove(); + const description = $('#textarea').html(); + const title = $('article > section > form > h2').text(); + return { + description, + title, + }; + }); + + const single = { + link: link, + pubDate, + }; + + return Promise.resolve(Object.assign({}, single, other)); + }) + ); ctx.state.data = { - title: `${nameProps[name]} - 洛理团委`, - link: u, - description: `洛阳理工学院团委 - ${nameProps[name]}`, + title: name === 'all' ? `团委 - 洛阳理工学院` : `${nameProps[name]} - 洛理团委`, + link: urla, + description: `洛阳理工学院团委 RSS`, + item: out, }; - - if (name === 'llxx') { - ctx.state.data.item = $('li.newslist') - .map((index, item) => ({ - title: $(item) - .find('a.type_one') - .text(), - description: $(item) - .find('li.desc') - .text() - .trim(), - pubDate: $(item) - .find('small') - .first() - .text() - .replace(/\//g, '-'), - link: - baseUrl + - $(item) - .find('a.type_one') - .attr('href'), - })) - .get(); - } else { - ctx.state.data.item = $('ul.caselist li') - .map((index, item) => ({ - title: $(item) - .find('a') - .text(), - description: '', - pubDate: $(item) - .find('span.time') - .text(), - link: - baseUrl + - $(item) - .find('a') - .attr('href'), - })) - .get(); - } }; diff --git a/lib/routes/universities/lit/xwzx.js b/lib/routes/universities/lit/xwzx.js index d3f4c41d1c..7d69d82969 100644 --- a/lib/routes/universities/lit/xwzx.js +++ b/lib/routes/universities/lit/xwzx.js @@ -4,7 +4,6 @@ const url = require('url'); const host = 'http://www.lit.edu.cn/'; const baseUrl = 'http://www.lit.edu.cn/xwzx/'; - const nameProps = { ggtz: '公告通知', xwkx: '新闻快讯', @@ -13,39 +12,59 @@ const nameProps = { }; module.exports = async (ctx) => { - const name = ctx.params.name || 'ggtz'; - const u = url.resolve(baseUrl, `${name}.htm`); - const response = await got({ - method: 'get', - url: u, - }); + const name = ctx.params.name || 'all'; + const urla = name === 'all' ? host : url.resolve(baseUrl, `${name}.htm`); + const response = await got.get(urla); const $ = cheerio.load(response.data); + $('.xxlibottom').remove(); + const list = name === 'all' ? $('.xxlcleft ul li').get() : $('li.list_item').get(); + + const out = await Promise.all( + list.map(async (item) => { + item = $(item); + const link = url.resolve(host, item.find('a').attr('href')); + const single = { + title: item.find('a').attr('title'), + link: link, + }; + + const other = await ctx.cache.tryGet(link, async () => { + const result = await got.get(link); + const $ = cheerio.load(result.data); + + $('img[src="/system/resource/images/fileTypeImages/icon_xls.gif"]').remove(); + $('img[src="/system/resource/images/fileTypeImages/icon_doc.gif"]').remove(); + $('img[src="/system/resource/images/fileTypeImages/icon_ppt.gif"]').remove(); + const delp = $('.nr_text').find('p'); + delp.eq(-1).remove(); + delp.eq(-2).remove(); + + const description = $('.nr_text').html(); + + const time = $('h6').text(); + let pubDate = time.match(/\d{4}-\d{1,2}-\d{1,2}/g); + if (pubDate === null) { + pubDate = time + .match(/\d{4}年\d{1,2}月\d{1,2}日 \d{1,2}:\d{1,2}/g) + .toString() + .replace('年', '-') + .replace('月', '-') + .replace('日', ''); + } + return { + description, + pubDate, + }; + }); + + return Promise.resolve(Object.assign({}, single, other)); + }) + ); + ctx.state.data = { - title: `${nameProps[name]} - 洛理新闻中心`, - link: u, - description: `洛阳理工学院新闻中心 - ${nameProps[name]}`, - item: $('li.list_item') - .map((index, item) => ({ - title: $(item) - .find('a') - .attr('title'), - description: '', - author: $(item) - .find('.Article_PublishDate') - .text() - .replace(/\d{4}年\d{2}月\d{2}日/g, '') - .replace(',', '') - .trim(), - pubDate: $(item) - .find('.Article_PublishDate') - .text() - .match(/[A-Za-z0-9_]+/g) - .join('-'), - link: $(item) - .find('a') - .attr('href') - .replace('../', host), - })) - .get(), + title: name === 'all' ? `新闻中心 - 洛阳理工学院` : `${nameProps[name]} - 洛理新闻中心`, + link: urla, + description: `洛阳理工学院新闻中心 RSS`, + item: out, }; };