From 490fc8056b88ea35ba99f979dae438f7f0f8ffad Mon Sep 17 00:00:00 2001 From: Jin Date: Mon, 10 Jun 2019 19:27:36 +0800 Subject: [PATCH] feat: add eastday shanghai channel (#2363) * feat: add eastday shanghai channel * fix: revert change in readme file --- docs/traditional-media.md | 8 ++++++ lib/router.js | 3 +++ lib/routes/eastday/sh.js | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 lib/routes/eastday/sh.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 47ad1c900e..bf4c4ffb02 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -114,6 +114,14 @@ Category 列表: +## 东方网 + +### 上海新闻 + + + + + ## 多维新闻网 ### 要闻 diff --git a/lib/router.js b/lib/router.js index aa310eaa35..e86b19739b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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; diff --git a/lib/routes/eastday/sh.js b/lib/routes/eastday/sh.js new file mode 100644 index 0000000000..ed432ec220 --- /dev/null +++ b/lib/routes/eastday/sh.js @@ -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 ? `
` : ''; + 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, + }; +};