From c0c7817ba2f6a9622fb66597e9b5606f30781598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=89=E5=87=89?= Date: Sun, 14 Oct 2018 16:30:04 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=EF=BC=9A=E8=80=81?= =?UTF-8?q?=E5=8F=B8=E6=9C=BA-=E6=B1=BD=E8=BD=A6=E7=A4=BE=E5=8C=BA?= =?UTF-8?q?=EF=BC=88=E9=87=8D=E6=8F=90=EF=BC=89=20(#871)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现 #759 // 刚才忘记同步代码了 = = --- docs/README.md | 5 +++++ router.js | 4 ++++ routes/laosiji/feed.js | 22 ++++++++++++++++++++++ routes/laosiji/hot.js | 22 ++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 routes/laosiji/feed.js create mode 100644 routes/laosiji/hot.js diff --git a/docs/README.md b/docs/README.md index 00c4e2ba4..47224eb6f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -735,6 +735,11 @@ GitHub 官方也提供了一些 RSS: +### 老司机 + + + + ## 图片 ### 妹子图 diff --git a/router.js b/router.js index c88aadbdb..33c826146 100644 --- a/router.js +++ b/router.js @@ -625,4 +625,8 @@ router.get('/pediy/topic/:category?/:type?', require('./routes/pediy/topic')); // 观止(每日一文) router.get('/guanzhi', require('./routes/guanzhi/guanzhi')); +// 老司机 +router.get('/laosiji/hot', require('./routes/laosiji/hot')); +router.get('/laosiji/feed', require('./routes/laosiji/feed')); + module.exports = router; diff --git a/routes/laosiji/feed.js b/routes/laosiji/feed.js new file mode 100644 index 000000000..5462ab705 --- /dev/null +++ b/routes/laosiji/feed.js @@ -0,0 +1,22 @@ +const axios = require('../../utils/axios'); + +module.exports = async (ctx) => { + const response = await axios({ + method: 'post', + url: 'http://www.laosiji.com/api/topic/feed/list', + }); + + const data = response.data.body.sns.list; + + ctx.state.data = { + title: '老司机-首页', + link: 'http://www.laosiji.com/new_web/index.html', + description: '老司机-首页', + item: data.map(({ title, resourceid, image, publishtime }) => ({ + title, + link: `http://www.laosiji.com/thread/${resourceid}.html`, + description: ``, + pubDate: publishtime, + })), + }; +}; diff --git a/routes/laosiji/hot.js b/routes/laosiji/hot.js new file mode 100644 index 000000000..e4d84e156 --- /dev/null +++ b/routes/laosiji/hot.js @@ -0,0 +1,22 @@ +const axios = require('../../utils/axios'); + +module.exports = async (ctx) => { + const response = await axios({ + method: 'get', + url: 'http://www.laosiji.com/thread/hotList', + }); + + const data = response.data; + + ctx.state.data = { + title: '老司机-24小时热门', + link: 'http://www.laosiji.com/new_web/index.html', + description: '老司机-24小时热门', + item: data.map(({ title, id, imageInfo, createtime }) => ({ + title, + link: `http://www.laosiji.com/thread/${id}.html`, + description: ``, + pubDate: new Date(createtime).toUTCString(), + })), + }; +}; From 7881264a551aaa5ffb8497966f313fa1ad1c4320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jiaxi=20=C2=AE?= <4772169+renzhexigua@users.noreply.github.com> Date: Sun, 14 Oct 2018 16:31:33 +0800 Subject: [PATCH 2/8] Fix pediy wrong timezone issue (#873) --- routes/pediy/topic.js | 60 ++++++++++++++++++++++--------------------- routes/pediy/utils.js | 27 +++++++++++++++++++ 2 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 routes/pediy/utils.js diff --git a/routes/pediy/topic.js b/routes/pediy/topic.js index 043c14c9a..3b5a862d2 100644 --- a/routes/pediy/topic.js +++ b/routes/pediy/topic.js @@ -1,5 +1,5 @@ const axios = require('../../utils/axios'); -const dateHelper = require('../../utils/date'); +const pediyUtils = require('./utils'); const cheerio = require('cheerio'); const baseUrl = 'https://bbs.pediy.com/'; @@ -56,40 +56,42 @@ module.exports = async (ctx) => { }, }); - const data = response.data; - const $ = cheerio.load(data); + const $ = cheerio.load(response.data); + const list = $('.thread'); ctx.state.data = { title: `${title}`, link: baseUrl + path, - item: $('.thread') - .map((_, elem) => { - const subject = $('.subject a', elem).eq(1); - const author = $('.username', elem).eq(0); + item: + list && + list + .map((_, elem) => { + const subject = $('.subject a', elem).eq(1); + const author = $('.username', elem).eq(0); - let pubDate = $('.date', elem).eq(0); - if (pubDate.text().indexOf('前') !== -1) { - pubDate = dateHelper(pubDate.text(), 8); - } else { - pubDate = pubDate.text(); - } + const pubDate = pediyUtils.dateParser( + $('.date', elem) + .eq(0) + .text(), + 8 + ); - let topic; - if (isSpecific) { - topic = categoryId[category][1]; - } else { - topic = $('.subject a.small', elem) - .eq(0) - .text(); - } + let topic; + if (isSpecific) { + topic = categoryId[category][1]; + } else { + topic = $('.subject a.small', elem) + .eq(0) + .text(); + } - return { - title: subject.text(), - link: baseUrl + subject.attr('href'), - pubDate: pubDate, - description: `作者: ${author.text()} 版块: ${topic}`, - }; - }) - .get(), + return { + title: subject.text(), + link: baseUrl + subject.attr('href'), + pubDate: `${pubDate}`, + description: `作者: ${author.text()} 版块: ${topic}`, + }; + }) + .get(), }; }; diff --git a/routes/pediy/utils.js b/routes/pediy/utils.js new file mode 100644 index 000000000..ce37785d7 --- /dev/null +++ b/routes/pediy/utils.js @@ -0,0 +1,27 @@ +const pediyUtils = { + dateParser: (html, timeZone) => { + let math; + let date = new Date(); + if (/(\d+)分钟前/.exec(html)) { + math = /(\d+)分钟前/.exec(html); + date.setMinutes(date.getMinutes() - math[1]); + return date.toUTCString(); + } else if (/(\d+)小时前/.exec(html)) { + math = /(\d+)小时前/.exec(html); + date.setHours(date.getHours() - math[1]); + return date.toUTCString(); + } else if (/(\d+)天前/.exec(html)) { + math = /(\d+)天前/.exec(html); + date.setDate(date.getDate() - math[1]); + return date.toUTCString(); + } else if (/(\d+)-(\d+)-(\d+) (\d+):(\d+)/.exec(html)) { + math = /(\d+)-(\d+)-(\d+) (\d+):(\d+)/.exec(html); + date = new Date(math[1], parseInt(math[2]) - 1, math[3], math[4], math[5]); + const serverOffset = new Date().getTimezoneOffset() / 60; + return new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString(); + } + return html; + }, +}; + +module.exports = pediyUtils; From 8ee6dde31377f9312404489d4d3bbf9b23a2993d Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Sun, 14 Oct 2018 09:32:46 +0100 Subject: [PATCH 3/8] Fix ifanr (#874) --- routes/ifanr/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/ifanr/index.js b/routes/ifanr/index.js index 0fc9d3259..5d09f58e5 100644 --- a/routes/ifanr/index.js +++ b/routes/ifanr/index.js @@ -1,9 +1,9 @@ const axios = require('../../utils/axios'); const cheerio = require('cheerio'); -let host = 'http://www.ifanr.com'; - module.exports = async (ctx) => { + let host = 'http://www.ifanr.com'; + if (ctx.params.channel) { let channel = ctx.params.channel.toLowerCase(); channel = channel.split('-').join('/'); From ecb0fdc57e1be2756653305afcc02c634cefcfe9 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Sun, 14 Oct 2018 09:33:08 +0100 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E4=BB=B7=E6=A0=BC=20(#875)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/xueqiu/fund.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/routes/xueqiu/fund.js b/routes/xueqiu/fund.js index 35d9367dd..14e45e853 100644 --- a/routes/xueqiu/fund.js +++ b/routes/xueqiu/fund.js @@ -31,11 +31,12 @@ module.exports = async (ctx) => { }); const data = response.data.data.fund_nav_growth.pop(); + const yesterday = response.data.data.fund_nav_growth.pop(); let description = `${fundNameShort}
最新净值 ${data.nav}
今日`; let title = `${fundNameShort} ${data.date.substring(5)} `; - const value = parseFloat(data.value); + const value = (parseFloat(data.nav) - parseFloat(yesterday.nav)).toFixed(4); if (value > 0) { description += '涨幅'; @@ -48,7 +49,7 @@ module.exports = async (ctx) => { title += '持平'; } - description += ` ${data.percentage}%(¥${data.value})`; + description += ` ${data.percentage}%(¥${value})`; const single = { title, From 37071454284ff20ba70d28bf2c0aced54193708f Mon Sep 17 00:00:00 2001 From: fengkx Date: Sun, 14 Oct 2018 16:33:36 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=90=8E=E7=BB=AD=20RSS=20(#872)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加 puppeteer 依赖 * houxu --- docs/README.md | 4 ++++ router.js | 3 +++ routes/houxu/houxu.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 routes/houxu/houxu.js diff --git a/docs/README.md b/docs/README.md index 47224eb6f..f239f10be 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1851,3 +1851,7 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运 ### 观止(每日一文) + +### 后续 + + diff --git a/router.js b/router.js index 33c826146..f9b055b51 100644 --- a/router.js +++ b/router.js @@ -625,6 +625,9 @@ router.get('/pediy/topic/:category?/:type?', require('./routes/pediy/topic')); // 观止(每日一文) router.get('/guanzhi', require('./routes/guanzhi/guanzhi')); +// 后续 +router.get('/houxu/:type/:id', require('./routes/houxu/houxu')); + // 老司机 router.get('/laosiji/hot', require('./routes/laosiji/hot')); router.get('/laosiji/feed', require('./routes/laosiji/feed')); diff --git a/routes/houxu/houxu.js b/routes/houxu/houxu.js new file mode 100644 index 000000000..4996df5ff --- /dev/null +++ b/routes/houxu/houxu.js @@ -0,0 +1,35 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const { type, id } = ctx.params; + const baseUrl = `https://houxu.app/${type}/${id}`; + const res = await axios.get(baseUrl); + const data = res.data; + const $ = cheerio.load(data); + + const list = $('section.threads > div').get(); + const descriptionSelector = type === 'live' ? '.summary' : '.readable'; + const out = list.map((item) => { + const each = $(item); + return { + title: each + .find('a') + .text() + .trim(), + description: each + .find(descriptionSelector) + .text() + .trim(), + link: each.find('a').attr('href'), + }; + }); + ctx.state.data = { + title: $('.large-title').text(), + description: $('title') + .text() + .trim(), + link: baseUrl, + item: out, + }; +}; From bcabc041563a5e6184eec9bd24de447d42ee8321 Mon Sep 17 00:00:00 2001 From: Artin Date: Sun, 14 Oct 2018 16:36:26 +0800 Subject: [PATCH 6/8] =?UTF-8?q?swust:=20=E4=BF=AE=E5=A4=8D=E6=95=99?= =?UTF-8?q?=E5=8A=A1=E5=A4=84=20(#882)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/README.md | 12 +++-- router.js | 5 +- routes/universities/swust/cs.js | 2 +- routes/universities/swust/jwc.js | 63 ------------------------- routes/universities/swust/jwc_news.js | 53 +++++++++++++++++++++ routes/universities/swust/jwc_notice.js | 59 +++++++++++++++++++++++ 6 files changed, 123 insertions(+), 71 deletions(-) delete mode 100644 routes/universities/swust/jwc.js create mode 100644 routes/universities/swust/jwc_news.js create mode 100644 routes/universities/swust/jwc_notice.js diff --git a/docs/README.md b/docs/README.md index f239f10be..442b0b934 100644 --- a/docs/README.md +++ b/docs/README.md @@ -950,20 +950,22 @@ GitHub 官方也提供了一些 RSS: ### 西南科技大学 - + + -| 通知公告 | 学校新闻 | -| -------- | -------- | -| 1 | 2 | +| 创新创业教育 | 学生学业 | 建设与改革 | 教学质量保障 | 教学运行 | 教师教学 | +| ------------ | -------- | ---------- | ------------ | -------- | -------- | +| 1 | 2 | 3 | 4 | 5 | 6 | - + | 新闻动态 | 学术动态 | 通知公告 | 教研动态 | | -------- | -------- | -------- | -------- | | 1 | 2 | 3 | 4 | + ### 北京大学 diff --git a/router.js b/router.js index f9b055b51..22746ff16 100644 --- a/router.js +++ b/router.js @@ -505,8 +505,9 @@ router.get('/shmtu/notes', require('./routes/universities/shmtu/notes')); router.get('/shmtu/jwc/:type', require('./routes/universities/shmtu/jwc')); // 西南科技大学 -router.get('/swust/jwc/:type', require('./routes/universities/swust/jwc')); -router.get('/swust/cs/:type', require('./routes/universities/swust/cs')); +router.get('/swust/jwc/news', require('./routes/universities/swust/jwc_news')); +router.get('/swust/jwc/notice/:type?', require('./routes/universities/swust/jwc_notice')); +router.get('/swust/cs/:type?', require('./routes/universities/swust/cs')); // 华南师范大学 router.get('/scnu/jw', require('./routes/universities/scnu/jw')); diff --git a/routes/universities/swust/cs.js b/routes/universities/swust/cs.js index 825aecca9..11aa3e37a 100644 --- a/routes/universities/swust/cs.js +++ b/routes/universities/swust/cs.js @@ -10,7 +10,7 @@ module.exports = async (ctx) => { responseType: 'arraybuffer', }); - let type = ctx.params.type; + let type = ctx.params.type || 1; let info = '新闻动态'; let word = 'news'; if (type === '2') { diff --git a/routes/universities/swust/jwc.js b/routes/universities/swust/jwc.js deleted file mode 100644 index 73fdb9584..000000000 --- a/routes/universities/swust/jwc.js +++ /dev/null @@ -1,63 +0,0 @@ -const axios = require('../../../utils/axios'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - const host = 'http://www.dean.swust.edu.cn'; - let type = ctx.params.type; - - let info = '通知公告'; - let word = 'notices/'; - - if (type === '2') { - info = '站点新闻'; - word = 'news/'; - } else { - type = '1'; - } - - const web = 'http://www.dean.swust.edu.cn/xml/' + word + 'index.xml'; - const response = await axios({ - method: 'get', - url: web, - headers: { - Referer: host, - }, - }); - - const data = response.data; - - const $ = cheerio.load(data, { - xmlMode: true, - }); - const list = $('entrity'); - - ctx.state.data = { - title: '西南科技大学 教务处 ' + info, - link: web, - description: $('title') - .first() - .text(), - item: - list && - list - .map((index, item) => { - item = $(item); - return { - title: item.find('title').text(), - description: item.find('summary').text(), - pubDate: new Date( - Date.parse( - item - .find('date') - .text() - .replace('年', '-') - .replace('月', '-') - .replace('日', '') - ) - ).toUTCString(), - link: host + '/xml/' + word + item.attr('id') + '.xml', - }; - }) - .get(), - }; -}; diff --git a/routes/universities/swust/jwc_news.js b/routes/universities/swust/jwc_news.js new file mode 100644 index 000000000..2754cd256 --- /dev/null +++ b/routes/universities/swust/jwc_news.js @@ -0,0 +1,53 @@ +const axios = require('../../../utils/axios'); + +// 转码需要设定返回数据的格式,其可选项是arraybuffer,blob,document,json,text,stream +// 默认为json +const axios_ins = axios.create({ responseType: 'blob' }); + +const api = 'http://www.dean.swust.edu.cn/cms/portal/news.json'; +const host = 'http://www.dean.swust.edu.cn/'; +const page = 'http://www.dean.swust.edu.cn/news/'; + +function evil(fn) { + const Fn = Function; // 一个变量指向Function,防止有些前端编译工具报错 + return new Fn('return ' + fn)(); +} + +module.exports = async (ctx) => { + const response = await axios_ins.get(api); + const data = evil(response.data); + + const resultItems = await Promise.all( + data.map(async (item) => { + const title = item.title; + const date = item.outdate; + const link = page + item.id; + const author = item.publisher; + + let resultItem = {}; + + const value = await ctx.cache.get(link); + if (value) { + resultItem = JSON.parse(value); + } else { + resultItem = { + title: title, + link: link, + pubDate: new Date(date), + author: author, + }; + + ctx.cache.set(link, JSON.stringify(resultItem), 24 * 60 * 60); + } + + return Promise.resolve(resultItem); + }) + ); + + ctx.state.data = { + title: '西南科技大学教务处 新闻', + link: host, + description: '西南科技大学教务处 新闻', + item: resultItems, + }; +}; diff --git a/routes/universities/swust/jwc_notice.js b/routes/universities/swust/jwc_notice.js new file mode 100644 index 000000000..2893ea1d2 --- /dev/null +++ b/routes/universities/swust/jwc_notice.js @@ -0,0 +1,59 @@ +const axios = require('../../../utils/axios'); + +// 转码需要设定返回数据的格式,其可选项是arraybuffer,blob,document,json,text,stream +// 默认为json +const axios_ins = axios.create({ responseType: 'blob' }); + +const api = 'http://www.dean.swust.edu.cn/cms/portal/notice.json'; +const page = 'http://www.dean.swust.edu.cn/notice/page/'; +const tag = 'http://www.dean.swust.edu.cn/notice/tag/'; + +function evil(fn) { + const Fn = Function; // 一个变量指向Function,防止有些前端编译工具报错 + return new Fn('return ' + fn)(); +} + +module.exports = async (ctx) => { + const type = ctx.params.type || 1; + + const response = await axios_ins.get(api); + const responsedata = evil(response.data); + const data = responsedata[type - 1]; + + const info = data.name || ''; + const tag_url = data.id || ''; + + const resultItems = await Promise.all( + data.items.map(async (item) => { + const link = page + item.id; + const author = item.publisher; + const date = item.outdate; + const title = item.title; + + let resultItem = {}; + + const value = await ctx.cache.get(link); + if (value) { + resultItem = JSON.parse(value); + } else { + resultItem = { + title: title, + link: link, + pubDate: new Date(date), + author: author, + }; + + ctx.cache.set(link, JSON.stringify(resultItem), 24 * 60 * 60); + } + + return Promise.resolve(resultItem); + }) + ); + + ctx.state.data = { + title: '西南科技大学教务处 ' + info, + link: tag + tag_url, + description: `西南科技大学教务处 ${info}`, + item: resultItems, + }; +}; From e84e880e2e8d5f89b44a4f0adfb12f4aa48facb5 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Sun, 14 Oct 2018 09:36:39 +0100 Subject: [PATCH 7/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9F=A5=E6=99=93?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=20(#878)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #864 --- docs/README.md | 10 +++++++ router.js | 3 +++ routes/miniapp/article.js | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 routes/miniapp/article.js diff --git a/docs/README.md b/docs/README.md index 442b0b934..a25c934b3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1854,6 +1854,16 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运 +### 知晓程序 + + + +| 全部 | 小程序资讯 | 知晓云 | 小程序推荐 | 榜单 | 晓组织 | 新能力 | 小程序问答 | +| ---- | ---------- | ------ | -------------- | ---- | ------ | ---------- | ---------- | +| all | news | cloud | recommendation | rank | group | capability | qa | + + + ### 后续 diff --git a/router.js b/router.js index 22746ff16..746c6b383 100644 --- a/router.js +++ b/router.js @@ -626,6 +626,9 @@ router.get('/pediy/topic/:category?/:type?', require('./routes/pediy/topic')); // 观止(每日一文) router.get('/guanzhi', require('./routes/guanzhi/guanzhi')); +// 知晓程序 +router.get('/miniapp/article/:category', require('./routes/miniapp/article')); + // 后续 router.get('/houxu/:type/:id', require('./routes/houxu/houxu')); diff --git a/routes/miniapp/article.js b/routes/miniapp/article.js new file mode 100644 index 000000000..9c53a53ba --- /dev/null +++ b/routes/miniapp/article.js @@ -0,0 +1,57 @@ +const axios = require('../../utils/axios'); + +module.exports = async (ctx) => { + const category = ctx.params.category.toLowerCase(); + + let host = `https://minapp.com/api/v5/trochili/post/?category=${category}&post_type=article&limit=10`; + + if (category === 'all') { + host = 'https://minapp.com/api/v5/trochili/post/?post_type=article&limit=10'; + } + + let title; + switch (category) { + case 'news': + title = '小程序资讯'; + break; + case 'cloud': + title = '知晓云'; + break; + case 'recommendation': + title = '小程序推荐'; + break; + case 'rank': + title = '榜单'; + break; + case 'group': + title = '晓组织'; + break; + case 'capability': + title = '新能力'; + break; + case 'qa': + title = '小程序问答'; + break; + default: + break; + } + + const response = await axios.get(host); + + const list = response.data.objects; + + const out = list.map((item) => ({ + title: item.title, + link: `https://minapp.com/article/${item.id}`, + author: item.created_by.nickname, + description: item.content, + pubDate: new Date(item.created_at * 1000), + })); + + ctx.state.data = { + title: `知晓程序${title ? ' - ' + title : ''}`, + description: '知晓程序 | 一扫即用的小程序大全', + link: `https://minapp.com/article/?page=1&category=${category}`, + item: out, + }; +}; From 2dfb7773dc9cc4959218c578821be076b688a25d Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Sun, 14 Oct 2018 09:39:16 +0100 Subject: [PATCH 8/8] =?UTF-8?q?=E5=A4=9A=E7=BB=B4=E6=96=B0=E9=97=BB?= =?UTF-8?q?=E7=BD=91=20(#877)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 多维要闻 * 新闻排行榜 * Add docs * Rename and fixes --- docs/README.md | 12 +++++++ router.js | 4 +++ routes/dwnews/rank.js | 65 ++++++++++++++++++++++++++++++++++ routes/dwnews/utils.js | 78 +++++++++++++++++++++++++++++++++++++++++ routes/dwnews/yaowen.js | 69 ++++++++++++++++++++++++++++++++++++ 5 files changed, 228 insertions(+) create mode 100644 routes/dwnews/rank.js create mode 100644 routes/dwnews/utils.js create mode 100644 routes/dwnews/yaowen.js diff --git a/docs/README.md b/docs/README.md index a25c934b3..fe0d70f96 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1393,6 +1393,18 @@ Category 列表: +### 多维新闻网 + + + +| 全部 | 国际 | 中国 | 香港 | 台湾 | +| ------ | ------ | ----- | -------- | ------ | +| yaowen | global | china | hongkong | taiwan | + + + + + ## 预报预警 ### 停水通知 diff --git a/router.js b/router.js index 746c6b383..e60f06800 100644 --- a/router.js +++ b/router.js @@ -626,6 +626,10 @@ router.get('/pediy/topic/:category?/:type?', require('./routes/pediy/topic')); // 观止(每日一文) router.get('/guanzhi', require('./routes/guanzhi/guanzhi')); +// 多维新闻网 +router.get('/dwnews/yaowen/:region?', require('./routes/dwnews/yaowen')); +router.get('/dwnews/rank/:type/:range', require('./routes/dwnews/rank')); + // 知晓程序 router.get('/miniapp/article/:category', require('./routes/miniapp/article')); diff --git a/routes/dwnews/rank.js b/routes/dwnews/rank.js new file mode 100644 index 000000000..b69253efe --- /dev/null +++ b/routes/dwnews/rank.js @@ -0,0 +1,65 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const utils = require('./utils'); + +module.exports = async (ctx) => { + const response = await axios.get('http://www.dwnews.com/html/share/24-7topnews.js'); + + const data = JSON.parse(response.data.replace('var rankingData = ', '')); + + let list, title; + switch (ctx.params.type) { + case 'news': + if (ctx.params.range === '1') { + list = data.newsRanklist['24H']; + title = '新闻 24 小时排行'; + } else { + list = data.newsRanklist['7D']; + title = '新闻 7 日排行'; + } + break; + case 'photo': + if (ctx.params.range === '1') { + list = data.photoRanklist['24H']; + title = '图集 24 小时排行'; + } else { + list = data.photoRanklist['7D']; + title = '图集 7 日排行'; + } + + break; + default: + break; + } + + const out = await Promise.all( + list.slice(0, 10).map(async (item) => { + const cache = await ctx.cache.get(item.url); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + item.url = 'http://news.dwnews.com/global/news/2018-10-08/60089463.html'; + const response = await axios.get(item.url.replace('.html', '_all.html')); + const $ = cheerio.load(response.data); + + const result = utils.ProcessRank($, item); + + const single = { + title: item.title, + link: item.url, + author: result.author, + description: result.description, + pubDate: result.pubDate, + }; + ctx.cache.set(item.url, JSON.stringify(single), 2 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `多维新闻网 - ${title}`, + description: '多维新闻网—记住世界的轨迹 更需多维的视线,海外华人首选的中文门户新闻网站,及时全面的向全球海外华人更新世界各地时事政治、经济、科技、人文历史、图片、视频等新闻内容,是海外华人必上的新闻门户网站。', + link: 'http://www.dwnews.com/', + item: out, + }; +}; diff --git a/routes/dwnews/utils.js b/routes/dwnews/utils.js new file mode 100644 index 000000000..8d20abeed --- /dev/null +++ b/routes/dwnews/utils.js @@ -0,0 +1,78 @@ +const ProcessFeed = ($, item) => { + let content; + if (item.filetype === 'TJ') { + const images = $('div#slider_component_4_0 > img'); + + $('div.captions > p').each((i, e) => { + $(images[i]).insertBefore(e); + }); + + content = $('div.captions'); + } else { + content = $('div.dia-lead-one'); + } + + ProcessQuote($, content); + ProcessImage($, content); + Clean($, content); + + return content.html(); +}; + +const ProcessRank = ($) => { + let content, author; + + const pubDate = new Date($('div.time').text()); + pubDate.setHours(pubDate.getHours() - 8); + + content = $('div.dia-lead-one'); + + if (content.length === 0) { + content = $('div.container'); + + author = $('div.author').text(); + } else { + author = $('div.nw').text(); + } + + ProcessQuote($, content); + ProcessImage($, content); + Clean($, content); + + return { + description: content.html(), + author, + pubDate, + }; +}; + +const ProcessQuote = ($, content) => { + content.find('div.ed-top').each((i, e) => { + const quote = $(e).siblings('p'); + if (quote.length > 0) { + $(`
${quote.text()}
`).insertBefore(e); + + $(quote).remove(); + $(e).remove(); + } + }); +}; + +const ProcessImage = ($, content) => { + content.find('a[href="javascript:;"]').each((i, e) => { + const img = $(e).find('img'); + $(img).insertBefore(e); + $(e).remove(); + }); +}; + +const Clean = ($, content) => { + content.find('div.clear, div.badoo, div.hu-bqsm, div.sign, div.xyy, div#component_14_0').each((i, e) => { + $(e).remove(); + }); +}; + +module.exports = { + ProcessFeed, + ProcessRank, +}; diff --git a/routes/dwnews/yaowen.js b/routes/dwnews/yaowen.js new file mode 100644 index 000000000..f024dc436 --- /dev/null +++ b/routes/dwnews/yaowen.js @@ -0,0 +1,69 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const utils = require('./utils'); + +module.exports = async (ctx) => { + const link = 'http://news.dwnews.com'; + let region, + host, + title = '要闻'; + + if (ctx.params.region) { + region = ctx.params.region.toLowerCase(); + switch (region) { + case 'china': + title = `中国${title}`; + break; + case 'global': + title = `国际${title}`; + break; + case 'hongkong': + title = `香港${title}`; + break; + case 'taiwan': + title = `台湾${title}`; + break; + default: + break; + } + host = `${link}/html/news/${region}/list0.json`; + } else { + region = 'yaowen'; + host = `${link}/html/news/yaowen/list0.json`; + } + + const list = await axios.get(host); + + const out = await Promise.all( + list.data.slice(0, 10).map(async (item) => { + const cache = await ctx.cache.get(item.url); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const response = await axios.get(item.url.replace('.html', '_all.html')); + const $ = cheerio.load(response.data); + + const pubDate = new Date(item.showTime); + pubDate.setHours(pubDate.getHours() - 8); + + const description = utils.ProcessFeed($, item); + + const single = { + title: item.title, + link: item.url, + author: $('div.nw').text(), + description, + pubDate: pubDate.toUTCString(), + }; + ctx.cache.set(item.url, JSON.stringify(single), 2 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `多维新闻网 - ${title}`, + description: '多维新闻网—记住世界的轨迹 更需多维的视线,海外华人首选的中文门户新闻网站,及时全面的向全球海外华人更新世界各地时事政治、经济、科技、人文历史、图片、视频等新闻内容,是海外华人必上的新闻门户网站。', + link: `${region === 'yaowen' ? link : link + '/' + region}`, + item: out, + }; +};