feat(route): add 东方网24小时热闻 (#7989)

* feat(route): add 东方网24小时热闻

* refactor: migrate to v2

* fix: docs wrong header

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
This commit is contained in:
Ethan Shen 2022-02-23 00:40:47 +08:00 committed by GitHub
parent c2d3df2d5f
commit d28b8aaad8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 134 additions and 13 deletions

View File

@ -665,6 +665,21 @@ IT・科学 tech_science
<Route author="nczitzk" example="/eastday/portrait" path="/eastday/portrait"/>
### 24 小时热闻
<Route author="nczitzk" example="/eastday/24" path="/eastday/24/:category?" :paramsDesc="['分类,见下表,默认为社会']">
| 推荐 | 社会 | 娱乐 | 国际 | 军事 |
| -- | -- | -- | -- | -- |
| 养生 | 汽车 | 体育 | 财经 | 游戏 |
| -- | -- | -- | -- | -- |
| 科技 | 国内 | 宠物 | 情感 | 人文 | 教育 |
| -- | -- | -- | -- | -- | -- |
</Route>
## 东网
<Route author="Fatpandac" example="/oncc/zh-hant/news" path="/oncc/:language/:channel?" :paramsDesc="['`zh-hans` 为简体,`zh-hant` 为繁体', '频道,默认为港澳']">

View File

@ -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'));

90
lib/v2/eastday/24.js Normal file
View File

@ -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,
};
};

View File

@ -1,4 +1,6 @@
module.exports = {
'/24/:category?': ['nczitzk'],
'/find': ['nczitzk'],
'/portrait': ['nczitzk'],
'/sh': ['saury'],
};

View File

@ -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: '热点搜索',

View File

@ -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'));
};

View File

@ -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);
}