diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 557579f72f..e7614727e0 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -2227,6 +2227,12 @@ category 对应的关键词有 +## 新华每日电讯 + +### 今日 + + + ## 新京报 ### 栏目 diff --git a/lib/v2/mrdx/daily.js b/lib/v2/mrdx/daily.js new file mode 100644 index 0000000000..b2dfa999c4 --- /dev/null +++ b/lib/v2/mrdx/daily.js @@ -0,0 +1,79 @@ +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const { getElementChildrenInnerText } = require('./utils'); +const dayjs = require('dayjs'); +const utc = require('dayjs/plugin/utc'); +dayjs.extend(utc); + +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const dateFormatted = dayjs().utcOffset(8).format('YYYYMMDD'); + + const baseUrl = `http://mrdx.cn/content/${dateFormatted}`; + const link = `${baseUrl}/Page01DK.htm`; + + const indexPageResponse = await got({ + url: link, + method: 'get', + }); + const $ = cheerio.load(indexPageResponse.data); + + // 当日所有文章Url在的`daoxiang` attribute中 + // 其站点使用**非标准**的HTML语法 + // eg: `Lorem ipsum` + const passagesList = $('.listdaohang > ul > li a') + .toArray() + .map((item) => ({ + link: new URL(`${baseUrl}/${item.attribs.daoxiang}`).href, + })); + + const passages = await Promise.all( + passagesList.map((passage) => + ctx.cache.tryGet(passage.link, async () => { + const response = await got({ + url: passage.link, + method: 'get', + }); + + const $ = cheerio.load(response.data); + + // 标题在`h2`,`h3`,`h4`,`h5`中,有多余空格和换行符 + // 但是Middleware会处理 + const title = + getElementChildrenInnerText($('.bggray > .margin15 > h2')[0]) + + getElementChildrenInnerText($('.bggray > .margin15 > h3')[0]) + + getElementChildrenInnerText($('.bggray > .margin15 > h4')[0]) + + getElementChildrenInnerText($('.bggray > .margin15 > h5')[0]); + + // 作者和发布时间 + // 中间用超多空格分隔 + // 头尾均有多余空格 + // eg: 新华每日电讯 2022年12月21日 + const [author, pubDate] = getElementChildrenInnerText($('.shijian')[0]) + .split(' ') + .filter((item) => item !== '') + .map((item) => item.trim()); + + const content = $('.contenttext').html(); + + return { + title, + author, + pubDate: parseDate(pubDate, 'YYYY年MM月DD日'), + guid: passage.link, + link: passage.link, + description: content, + }; + }) + ) + ); + + ctx.state.data = { + title: '新华每日电讯', + link, + item: passages, + description: + '中国报纸发行前三强。位列《人民日报》,《参考消息》之后。《新华每日电讯》是新华社出版的一份新闻电讯报,1993年创办,具有很高的权威性和准确性,有“一报在手,便知天下”之美誉。《新华每日电讯》为对开八版日报,集中刊登[新华社]每天向国内播发的电讯稿及图片稿。打开《新华每日电讯》,中国和全世界每天发生的重大事件便将一目了然。在人类生活节奏日益加快的当今世界,《新华每日电讯》以最便捷、最醒目的方式为公众提供最重要的新闻报道,受到公众的喜爱和拥护。', + }; +}; diff --git a/lib/v2/mrdx/maintainer.js b/lib/v2/mrdx/maintainer.js new file mode 100644 index 0000000000..23ca84c7f8 --- /dev/null +++ b/lib/v2/mrdx/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/today': ['Dustin-Jiang'], +}; diff --git a/lib/v2/mrdx/radar.js b/lib/v2/mrdx/radar.js new file mode 100644 index 0000000000..35b5dea293 --- /dev/null +++ b/lib/v2/mrdx/radar.js @@ -0,0 +1,13 @@ +module.exportss = { + 'mrdx.cn': { + _name: '新华每日电讯', + '.': [ + { + title: '今日', + docs: 'https://docs.rsshub.app/traditional-media.html#xin-hua-mei-ri-dian-xun', + source: ['*'], + target: '/mrdx/today', + }, + ], + }, +}; diff --git a/lib/v2/mrdx/router.js b/lib/v2/mrdx/router.js new file mode 100644 index 0000000000..c925841e1b --- /dev/null +++ b/lib/v2/mrdx/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/today', require('./daily')); +}; diff --git a/lib/v2/mrdx/utils.js b/lib/v2/mrdx/utils.js new file mode 100644 index 0000000000..40a9fecf91 --- /dev/null +++ b/lib/v2/mrdx/utils.js @@ -0,0 +1,17 @@ +function getElementChildrenInnerText(element) { + let text = ''; + for (const child of element.children) { + if (child.type === 'text') { + text += child.data.trim(); + } + if (child.children !== undefined) { + text += getElementChildrenInnerText(child); + } + } + + return text; +} + +module.exports = { + getElementChildrenInnerText, +};