增加 新加坡联合早报 (#613)

* 增加 新加坡联合早报

* update formation
This commit is contained in:
Artin
2018-09-01 23:39:09 +08:00
committed by DIYgod
parent d52fb080d1
commit f16a6d4cb7
5 changed files with 201 additions and 1 deletions

View File

@@ -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)
### 参与讨论

View File

@@ -505,6 +505,36 @@ RSSHub 同时支持 RSS 2.0、Atom 和 [JSON Feed](https://jsonfeed.org/) 输出
- category, 新京报的栏目名, 点击对应栏目后在地址栏找到
### 联合早报
#### 即时新闻 <Author uid="lengthmin"/>
举例: [https://rsshub.app/zaobao/realtime/china](https://rsshub.app/zaobao/realtime/china)
路由: `/zaobao/realtime/:type?`
参数:
type分类, 缺省为中港台
| 中港台 | 新加坡 | 国际 | 财经 |
| ------ | --------- | ----- | -------- |
| china | singapore | world | zfinance |
#### 新闻 <Author uid="lengthmin"/>
举例: [https://rsshub.app/zaobao/znews/greater-china](https://rsshub.app/zaobao/znews/greater-china)
路由: `/zaobao/znews/:type?`
参数:
type分类, 缺省为中港台
| 中港台 | 新加坡 | 东南亚 | 国际 | 体育 |
| ------------- | --------- | ------ | ------------- | ------ |
| greater-china | singapore | sea | international | sports |
## 预报预警类
### 停水通知

View File

@@ -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'));

85
routes/zaobao/realtime.js Normal file
View File

@@ -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() + '<br/>';
});
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,
};
};

81
routes/zaobao/znews.js Normal file
View File

@@ -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() + '<br/>';
});
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,
};
};