From f16a6d4cb765d37cf11f05fd663b0a870e90f344 Mon Sep 17 00:00:00 2001 From: Artin Date: Sat, 1 Sep 2018 23:39:09 +0800 Subject: [PATCH] =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=20=E6=96=B0=E5=8A=A0?= =?UTF-8?q?=E5=9D=A1=E8=81=94=E5=90=88=E6=97=A9=E6=8A=A5=20(#613)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加 新加坡联合早报 * update formation --- README.md | 2 +- docs/README.md | 30 ++++++++++++++ router.js | 4 ++ routes/zaobao/realtime.js | 85 +++++++++++++++++++++++++++++++++++++++ routes/zaobao/znews.js | 81 +++++++++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 routes/zaobao/realtime.js create mode 100644 routes/zaobao/znews.js diff --git a/README.md b/README.md index e21fa2541a..db61244f6b 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 2. 在 [/routes/](https://github.com/DIYgod/RSSHub/tree/master/routes) 中的路由对应路径添加获取 RSS 内容的脚本 -3. 更新 README 和文档: [/README.md](https://github.com/DIYgod/RSSHub/blob/master/README.md) [/docs/README.md](https://github.com/DIYgod/RSSHub/blob/master/docs/README.md) +3. 更新文档: [/docs/README.md](https://github.com/DIYgod/RSSHub/blob/master/docs/README.md) ### 参与讨论 diff --git a/docs/README.md b/docs/README.md index 01887b243b..9c34b61ce4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -505,6 +505,36 @@ RSSHub 同时支持 RSS 2.0、Atom 和 [JSON Feed](https://jsonfeed.org/) 输出 - category, 新京报的栏目名, 点击对应栏目后在地址栏找到 +### 联合早报 + +#### 即时新闻 + +举例: [https://rsshub.app/zaobao/realtime/china](https://rsshub.app/zaobao/realtime/china) + +路由: `/zaobao/realtime/:type?` + +参数: + +type,分类, 缺省为中港台 + +| 中港台 | 新加坡 | 国际 | 财经 | +| ------ | --------- | ----- | -------- | +| china | singapore | world | zfinance | + +#### 新闻 + +举例: [https://rsshub.app/zaobao/znews/greater-china](https://rsshub.app/zaobao/znews/greater-china) + +路由: `/zaobao/znews/:type?` + +参数: + +type,分类, 缺省为中港台 + +| 中港台 | 新加坡 | 东南亚 | 国际 | 体育 | +| ------------- | --------- | ------ | ------------- | ------ | +| greater-china | singapore | sea | international | sports | + ## 预报预警类 ### 停水通知 diff --git a/router.js b/router.js index 45575e7228..6e6b965ea6 100755 --- a/router.js +++ b/router.js @@ -498,6 +498,10 @@ router.get('/ifanr/appso', require('./routes/ifanr/appso')); // 果壳网 router.get('/guokr/scientific', require('./routes/guokr/scientific')); +// 联合早报 +router.get('/zaobao/realtime/:type?', require('./routes/zaobao/realtime')); +router.get('/zaobao/znews/:type?', require('./routes/zaobao/znews')); + // Apple router.get('/apple/exchange_repair', require('./routes/apple/exchange_repair')); diff --git a/routes/zaobao/realtime.js b/routes/zaobao/realtime.js new file mode 100644 index 0000000000..fc2c1c1517 --- /dev/null +++ b/routes/zaobao/realtime.js @@ -0,0 +1,85 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const baseUrl = 'https://www.zaobao.com.sg'; +const host = 'https://www.zaobao.com.sg/realtime'; +const axios_ins = axios.create({ + headers: { + Referer: host, + }, +}); + +module.exports = async (ctx) => { + const type = ctx.params.type || 'china'; + + let info = '中港台'; + let word = 'div#CN.list-sect-sub'; + if (type === '2') { + info = 'singapore'; + word = 'div#SG.list-sect-sub'; + } else if (type === 'world') { + info = '国际'; + word = 'div#Global.list-sect-sub'; + } else if (type === 'zfinance') { + info = '财经'; + word = 'div#Finance.list-sect-sub'; + } + + const response = await axios_ins.get(host); + const $ = cheerio.load(response.data); + const data = $('li', word).find('div'); + // .attr('about') + const resultItems = await Promise.all( + data.toArray().map(async (item) => { + const $item = $(item); + const link = baseUrl + $item.attr('about'); + + let resultItem = {}; + + const value = await ctx.cache.get(link); + + if (value) { + resultItem = JSON.parse(value); + } else { + const article = await axios_ins.get(link); + const $1 = cheerio.load(article.data); + const res = $1('.datestamp.date-updated.meta-date-updated', '.body-content') + .contents() + .filter(function() { + return this.nodeType === 3; + }) + .text() + .replace('年', '-') + .replace('月', '-') + .replace('日', ''); + + const yyyymmdd = res.replace('更新', '').toString(); + const hhmm = $item + .find('em') + .text() + .replace(/(.{2})/, '$1:'); + let description = ''; + $1('p', '.article-content-container').each(function() { + description = description + $(this).html() + '
'; + }); + + resultItem = { + title: $1('h1', '.body-content').text(), + description: description, + pubDate: new Date(yyyymmdd + hhmm).toUTCString(), + link: link, + }; + + ctx.cache.set(link, JSON.stringify(resultItem), 24 * 60 * 60); + } + // }; + return Promise.resolve(resultItem); + }) + ); + + ctx.state.data = { + title: `《联合早报》${info} 即时`, + link: host, + description: '《联合早报》被公认是一份素质高、负责任、报道客观、言论公正、可信度高的报纸,对中国的发展采取积极的态度,在华人世界中享有崇高的信誉。', + item: resultItems, + }; +}; diff --git a/routes/zaobao/znews.js b/routes/zaobao/znews.js new file mode 100644 index 0000000000..fb6ac48a59 --- /dev/null +++ b/routes/zaobao/znews.js @@ -0,0 +1,81 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const baseUrl = 'https://www.zaobao.com.sg'; +const axios_ins = axios.create({ + headers: { + Referer: baseUrl, + }, +}); + +module.exports = async (ctx) => { + const type = ctx.params.type || 'greater-china'; + + let info = '中港台'; + let word = '/znews/greater-china'; + + if (type === 'singapore') { + info = '新加坡'; + word = '/znews/singapore'; + } else if (type === 'international') { + info = '国际'; + word = '/znews/international'; + } else if (type === 'sea') { + info = '东南亚'; + word = '/znews/sea'; + } else if (type === 'sports') { + info = '体育'; + word = '/znews/sports'; + } + + const response = await axios_ins.get(baseUrl + word); + const $ = cheerio.load(response.data); + const data = $('.row.list', '.post-list').find('.col-md-8.col-sm-8.col-xs-8.content'); + + const resultItems = await Promise.all( + data.toArray().map(async (item) => { + const $item = $(item); + const link = baseUrl + $item.find('a')[1].attribs.href; + + let resultItem = {}; + + const value = await ctx.cache.get(link); + + if (value) { + resultItem = JSON.parse(value); + } else { + const article = await axios_ins.get(link); + const $1 = cheerio.load(article.data); + const res = $1('.datestamp.date-published.meta-date-published', '.body-content') + .contents() + .text() + .replace('年', '-') + .replace('月', '-') + .replace('日', ''); + + const date = res.replace('发布/', '').toString(); + let description = ''; + $1('p', '.article-content-container').each(function() { + description = description + $(this).html() + '
'; + }); + + resultItem = { + title: $1('h1', '.body-content').text(), + description: description, + pubDate: new Date(date).toUTCString(), + link: link, + }; + + ctx.cache.set(link, JSON.stringify(resultItem), 24 * 60 * 60); + } + + return Promise.resolve(resultItem); + }) + ); + + ctx.state.data = { + title: `《联合早报》${info} 新闻`, + link: baseUrl + word, + description: '《联合早报》被公认是一份素质高、负责任、报道客观、言论公正、可信度高的报纸,对中国的发展采取积极的态度,在华人世界中享有崇高的信誉。', + item: resultItems, + }; +};