feat(route): add xinhua-daily-telegraph (#11487)

* feat(route): add xinhua-daily-telegraph

Add xinhua-daily telegraph route and doc mentioned in issue #11390.

* feat(route): add radar for xinhua-daily-telegraph

Add required radar config file.

* feat(router): change router name and cache method

Change router name from `xinhua-daily-telegraph` to `mrdx`.
Fix cache method to avoid processing at each request.

* feat(route): fix wrong date and improper cache method

Fix wrong `pubDate` by using `parseDate` in `utils/parse-date`.
Fix improper cache method. Now it caches proceeded RSS results.

* feat(radar): update source wildcard in lib/v2/mrdx/radar.js

* fix(router): use unique article link as guid

Use specific article link as guid.
Use specific article link for every article instead of the homepage.
This commit is contained in:
Dustin Jiang
2022-12-25 21:50:07 +08:00
committed by GitHub
parent 0128bf798a
commit a69dfacf2d
6 changed files with 121 additions and 0 deletions
+6
View File
@@ -2227,6 +2227,12 @@ category 对应的关键词有
<Route author="nczitzk" example="/hkcd/pdf" path="/hkcd/pdf"/>
## 新华每日电讯
### 今日
<Route author="Dustin-Jiang" example="/mrdx/today" path="/mrdx/today" />
## 新京报
### 栏目
+79
View File
@@ -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在<a>的`daoxiang` attribute中
// 其站点使用**非标准**的HTML语法
// eg: `<a class="foo" daoxiang="foo-bar.htm">Lorem ipsum</a>`
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年创办,具有很高的权威性和准确性,有“一报在手,便知天下”之美誉。《新华每日电讯》为对开八版日报,集中刊登[新华社]每天向国内播发的电讯稿及图片稿。打开《新华每日电讯》,中国和全世界每天发生的重大事件便将一目了然。在人类生活节奏日益加快的当今世界,《新华每日电讯》以最便捷、最醒目的方式为公众提供最重要的新闻报道,受到公众的喜爱和拥护。',
};
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/today': ['Dustin-Jiang'],
};
+13
View File
@@ -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',
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/today', require('./daily'));
};
+17
View File
@@ -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,
};