From d28b8aaad8bb2635dcb367acbb348cb37f5e3919 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Wed, 23 Feb 2022 00:40:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=B8=9C=E6=96=B9?= =?UTF-8?q?=E7=BD=9124=E5=B0=8F=E6=97=B6=E7=83=AD=E9=97=BB=20(#7989)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 东方网24小时热闻 * refactor: migrate to v2 * fix: docs wrong header Co-authored-by: TonyRL --- docs/traditional-media.md | 15 ++++++ lib/router.js | 5 +- lib/v2/eastday/24.js | 90 ++++++++++++++++++++++++++++++++ lib/v2/eastday/maintainer.js | 2 + lib/v2/eastday/radar.js | 16 ++++++ lib/v2/eastday/router.js | 2 + lib/{routes => v2}/eastday/sh.js | 17 +++--- 7 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 lib/v2/eastday/24.js rename lib/{routes => v2}/eastday/sh.js (68%) diff --git a/docs/traditional-media.md b/docs/traditional-media.md index fa4871246..788d40c4e 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -665,6 +665,21 @@ IT・科学 tech_science +### 24 小时热闻 + + + +| 推荐 | 社会 | 娱乐 | 国际 | 军事 | +| -- | -- | -- | -- | -- | + +| 养生 | 汽车 | 体育 | 财经 | 游戏 | +| -- | -- | -- | -- | -- | + +| 科技 | 国内 | 宠物 | 情感 | 人文 | 教育 | +| -- | -- | -- | -- | -- | -- | + + + ## 东网 diff --git a/lib/router.js b/lib/router.js index 10a242367..7aadd2b51 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1787,8 +1787,9 @@ router.get('/dekudeals/:type', lazyloadRouteHandler('./routes/dekudeals')); // router.get('/zhibo8/post/:id', lazyloadRouteHandler('./routes/zhibo8/post')); // router.get('/zhibo8/more/:category?', lazyloadRouteHandler('./routes/zhibo8/more')); -// 东方网-上海 -router.get('/eastday/sh', lazyloadRouteHandler('./routes/eastday/sh')); +// 东方网 migrated to v2 +// router.get('/eastday/sh', require('./routes/eastday/sh')); +// router.get('/eastday/24/:category?', require('./routes/eastday/24')); // Metacritic router.get('/metacritic/release/:platform/:type/:sort?', lazyloadRouteHandler('./routes/metacritic/release')); diff --git a/lib/v2/eastday/24.js b/lib/v2/eastday/24.js new file mode 100644 index 000000000..f13412b5c --- /dev/null +++ b/lib/v2/eastday/24.js @@ -0,0 +1,90 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +const categories = { + 社会: 'shehui', + 娱乐: 'yule', + 国际: 'guoji', + 军事: 'junshi', + 养生: 'yangsheng', + 汽车: 'qiche', + 体育: 'tiyu', + 财经: 'caijing', + 游戏: 'youxi', + 科技: 'keji', + 国内: 'guonei', + 宠物: 'chongwu', + 情感: 'qinggan', + 人文: 'renwen', + 教育: 'jiaoyu', +}; + +module.exports = async (ctx) => { + const category = ctx.params.category ?? '社会'; + + const rootUrl = 'https://mini.eastday.com'; + const currentUrl = `${rootUrl}/ns/api/detail/trust/trust-news-${categories[category]}.json`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const list = JSON.parse(response.data.match(/\((.*)\)/)[1]).data.trust.map((item) => ({ + title: item.topic, + link: `${rootUrl}${item.url}`, + })); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + const pageNumber = parseInt(detailResponse.data.match(/var page_num = '(\d+)'/)[1]); + + item.description = content('#J-contain_detail_cnt').html(); + item.pubDate = timezone(parseDate(content('meta[property="og:release_date"]').attr('content')), +8); + + if (pageNumber > 1) { + const links = []; + + for (let i = 2; i <= pageNumber; i++) { + links.push(item.link.replace(/\.html/, `-${i}.html`)); + } + + links.forEach( + async (link) => + await ctx.cache.tryGet(link, async () => { + const pageResponse = await got({ + method: 'get', + url: link, + }); + const subContent = cheerio.load(pageResponse.data); + + subContent('img').each(function () { + subContent(this).attr('src', subContent(this).attr('data-url')); + }); + + item.description += subContent('#J-contain_detail_cnt').html(); + }) + ); + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: `24小时${category}热闻 - 东方资讯`, + link: `${rootUrl}/#${categories[category]}`, + item: items, + }; +}; diff --git a/lib/v2/eastday/maintainer.js b/lib/v2/eastday/maintainer.js index faa6d7e16..31d8b218d 100644 --- a/lib/v2/eastday/maintainer.js +++ b/lib/v2/eastday/maintainer.js @@ -1,4 +1,6 @@ module.exports = { + '/24/:category?': ['nczitzk'], '/find': ['nczitzk'], '/portrait': ['nczitzk'], + '/sh': ['saury'], }; diff --git a/lib/v2/eastday/radar.js b/lib/v2/eastday/radar.js index a72380b77..8374c321c 100644 --- a/lib/v2/eastday/radar.js +++ b/lib/v2/eastday/radar.js @@ -1,6 +1,22 @@ module.exports = { 'eastday.com': { _name: '东方网', + mini: [ + { + title: '24 小时热闻', + docs: 'https://docs.rsshub.app/traditional-media.html#dong-fang-wang', + source: '/', + target: '/eastday/24', + }, + ], + sh: [ + { + title: '上海新闻', + docs: 'https://docs.rsshub.app/traditional-media.html#dong-fang-wang', + source: '/', + target: '/eastday/sh', + }, + ], www: [ { title: '热点搜索', diff --git a/lib/v2/eastday/router.js b/lib/v2/eastday/router.js index 84a23f5e1..899d65fd0 100644 --- a/lib/v2/eastday/router.js +++ b/lib/v2/eastday/router.js @@ -1,4 +1,6 @@ module.exports = function (router) { + router.get('/24/:category?', require('./24')); router.get('/find', require('./find')); router.get('/portrait', require('./portrait')); + router.get('/sh', require('./sh')); }; diff --git a/lib/routes/eastday/sh.js b/lib/v2/eastday/sh.js similarity index 68% rename from lib/routes/eastday/sh.js rename to lib/v2/eastday/sh.js index 27d95a9b3..5e2e99ca4 100644 --- a/lib/routes/eastday/sh.js +++ b/lib/v2/eastday/sh.js @@ -1,6 +1,8 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const logger = require('@/utils/logger'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); module.exports = async (ctx) => { const domain = 'http://wap.eastday.com'; @@ -16,28 +18,21 @@ module.exports = async (ctx) => { const entity = { title: item.title, description: item.abstracts, - pubDate: new Date(item.time).toUTCString(), + pubDate: timezone(parseDate(item.time), +8), link, }; try { const cacheKey = `eastday_sh_${link}`; - // 判断缓存中是否存在 - const cacheValue = await ctx.cache.get(cacheKey); - if (cacheValue) { - entity.description = cacheValue; - } else { + entity.description = await ctx.cache.tryGet(cacheKey, async () => { const article = await got({ method: 'get', url: link, }); // 解析html内容 const $ = cheerio.load(article.body); - const content = $('.article_wrapper .mainLayer .content').html() || $('.contentBox .article .detail').html(); - // 存放到缓存区 - ctx.cache.set(cacheKey, content); - entity.description = content; - } + return $('.article_wrapper .mainLayer .content').html() || $('.contentBox .article .detail').html(); + }); } catch (error) { logger.error(error); }