feat: update eastday shanghai channel (#3661)

This commit is contained in:
Jin 2020-01-03 11:31:26 +08:00 committed by DIYgod
parent 6532a1bfe8
commit d3f69f86bb
1 changed files with 22 additions and 16 deletions

View File

@ -6,41 +6,47 @@ module.exports = async (ctx) => {
const domain = 'http://wap.eastday.com';
const response = await got({
method: 'get',
url: `${domain}/node2/node3/n5/index5_t81.html`,
method: 'post',
url: `https://apin.eastday.com/api/news/list`,
json: true,
data: {
channelId: '15',
currentPage: 1,
pageSize: 20,
appId: '190504',
deviceId: '00000000',
version: '0.0.0.2',
},
});
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 src="${domain}${item.imgurl1}"><br>` : '';
data.list.map(async (item) => {
const link = item.url;
const entity = {
title: item.newstitle,
author: item.source,
description,
pubDate: new Date(item.createTime).toUTCString(),
title: item.title,
description: item.abstracts + '...',
pubDate: new Date(item.time).toUTCString(),
link,
};
try {
const cacheKey = `eastday_sh_${item.newsurl}`;
const xmlLink = domain + item.newsurl.split('_')[0] + '_K77.xml';
const cacheKey = `eastday_sh_${link}`;
// 判断缓存中是否存在
const cacheValue = await ctx.cache.get(cacheKey);
if (cacheValue) {
entity.description += cacheValue;
entity.description = cacheValue;
} else {
const article = await got({
method: 'get',
url: xmlLink,
url: link,
});
// 解析html内容
const $ = cheerio.load(article.body, { xmlMode: true });
const content = $('zw').text();
const $ = cheerio.load(article.body);
const content = $('.article_wrapper .mainLayer .content').html();
// 存放到缓存区
ctx.cache.set(cacheKey, content);
entity.description += content;
entity.description = content;
}
} catch (error) {
logger.error(error);