feat: add eastday shanghai channel (#2363)

* feat: add eastday shanghai channel

* fix: revert change in readme file
This commit is contained in:
Jin
2019-06-10 19:27:36 +08:00
committed by DIYgod
parent a002648819
commit 490fc8056b
3 changed files with 68 additions and 0 deletions

View File

@@ -114,6 +114,14 @@ Category 列表:
</Route>
## 东方网
### 上海新闻
<Route author="saury" example="/eastday/sh" path="/eastday/sh" />
</Route>
## 多维新闻网
### 要闻

View File

@@ -1395,4 +1395,7 @@ router.get('/005tv/zx/latest', require('./routes/005tv/zx'));
// Polimi News
router.get('/polimi/news/:language?', require('./routes/polimi/news'));
// 东方网-上海
router.get('/eastday/sh', require('./routes/eastday/sh'));
module.exports = router;

57
lib/routes/eastday/sh.js Normal file
View File

@@ -0,0 +1,57 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const domain = 'http://wap.eastday.com';
const response = await got({
method: 'get',
url: `${domain}/node2/node3/n5/index5_t81.html`,
});
const data = JSON.parse(response.body.trim());
const result = await Promise.all(
data.newslist.map(async (item) => {
const link = item.newsurl.match(/^http/) ? item.newsurl : domain + item.newsurl;
const description = item.imgurl1 ? `<img referrerpolicy="no-referrer" src="${domain}${item.imgurl1}"><br>` : '';
const entity = {
title: item.newstitle,
author: item.source,
description,
pubDate: new Date(item.createTime).toUTCString(),
link,
};
try {
const cacheKey = `eastday_sh_${item.newsurl}`;
const xmlLink = domain + item.newsurl.split('_')[0] + '_K77.xml';
// 判断缓存中是否存在
const cacheValue = await ctx.cache.get(cacheKey);
if (cacheValue) {
entity.description += cacheValue;
} else {
const article = await got({
method: 'get',
url: xmlLink,
});
// 解析html内容
const $ = cheerio.load(article.body, { xmlMode: true });
const content = $('zw').text();
// 存放到缓存区
ctx.cache.set(cacheKey, content);
entity.description += content;
}
} catch (error) {
console.log(error);
}
return entity;
})
);
ctx.state.data = {
title: `东方网-上海`,
link: `${domain}/wap/sh.html`,
item: result,
};
};