fix(route): yomiuri (#12526)

This commit is contained in:
Tony
2023-05-17 23:54:43 +08:00
committed by GitHub
parent 1e3a272876
commit 809dd41ba9
5 changed files with 92 additions and 112 deletions
+19 -4
View File
@@ -750,12 +750,27 @@ Provides all of the articles by the specified Yahoo! author.
### News
<RouteEn author="Arracc" example="/yomiuri/news" path="/yomiuri/:category" :paramsDesc="['category']">
<RouteEn author="Arracc" example="/yomiuri/news" path="/yomiuri/:category?" :paramsDesc="['Category, `news` by default']">
Free articles only.
| 新着・速報 |   社会 | 政治 | 経済 | スポーツ | 国際 | 科学・IT | 選挙・世論調査 | エンタメ・文化 | 囲碁・将棋 | ライフ | 地域 | 社説 | 皇室 |
| ---------- | -------- | -------- | ------- | -------- | ----- | ---------- | -------------- | -------------- | ---------- | ------ | ----- | --------- | --------- |
|   news   | national | politics | economy | sports | world | science | election | culture | igoshougi | life | local | editorial | koushitsu |
| Category | Parameter |
| -------------- | --------- |
| 新着・速報 | news |
| 社会 | national |
| 政治 | politics |
| 経済 | economy |
| スポーツ | sports |
| 国際 | world |
| 地域 | local |
| 科学・IT | science |
| エンタメ・文化 | culture |
| ライフ | life |
| 医療・健康 | medical |
| 教育・就活 | kyoiku |
| 選挙・世論調査 | election |
| 囲碁・将棋 | igoshougi |
| 社説 | editorial |
| 皇室 | koushitsu |
</RouteEn>
+19 -4
View File
@@ -912,13 +912,28 @@ IT・科学 tech_science
### 新聞
<Route author="Arracc" example="/yomiuri/news" path="/yomiuri/:category" :paramsDesc="['板块']">
<Route author="Arracc" example="/yomiuri/news" path="/yomiuri/:category?" :paramsDesc="['板块,默认为 `news`']">
无料全文,综合页 (新着・速報) 文章标题补充板块标签。
| 新着・速報 | 社会 | 政治 | 経済 | スポーツ | 国際 | 科学・IT | 選挙・世論調査 | エンタメ・文化 | 囲碁・将棋 | ライフ | 地域 | 社説 | 皇室 |
| ---------- | -------- | -------- | ------- | -------- | ----- | ---------- | -------------- | -------------- | ---------- | ------ | ----- | --------- | --------- |
| news | national | politics | economy | sports | world | science | election | culture | igoshougi | life | local | editorial | koushitsu |
| Category | Parameter |
| -------------- | --------- |
| 新着・速報 | news |
| 社会 | national |
| 政治 | politics |
| 経済 | economy |
| スポーツ | sports |
| 国際 | world |
| 地域 | local |
| 科学・IT | science |
| エンタメ・文化 | culture |
| ライフ | life |
| 医療・健康 | medical |
| 教育・就活 | kyoiku |
| 選挙・世論調査 | election |
| 囲碁・将棋 | igoshougi |
| 社説 | editorial |
| 皇室 | koushitsu |
</Route>
+1 -1
View File
@@ -1,3 +1,3 @@
module.exports = {
'/yomiuri/:category?': ['Arracc'],
'/:category?': ['Arracc'],
};
+50 -100
View File
@@ -4,126 +4,76 @@ const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const category = ctx.params.category;
const { category = 'news' } = ctx.params;
const url = `https://www.yomiuri.co.jp/${category}`;
const response = await got({
method: 'get',
url,
});
const response = await got(url);
const data = response.data;
const $ = cheerio.load(data);
let categoryName;
let list = null;
let list;
if (category === 'news') {
categoryName = '新着・速報';
list = $('div#latest_list_news').find('article').append($('div#latest_list_news_more').find('article'));
list = list.map((index, item) => {
item = $(item);
// remove matome title
const matomeTitle = item.find('div.c-matome-title');
if (matomeTitle.length !== 0) {
matomeTitle.remove();
}
// remove member only
const lockedIcon = item.find('svg[class=icon-locked]');
if (lockedIcon.length === 0) {
return item;
} else {
return null;
}
});
list = $('.news-top-latest__list .news-top-latest__list-item__inner')
.toArray()
.map((item) => {
item = $(item);
const a = item.find('h3 a');
return {
title: a.text(),
link: a.attr('href'),
pubDate: timezone(parseDate(item.find('time').attr('datetime')), +9),
locked: item.find('.icon-locked').length,
};
});
} else {
let parentSelector = 'div.p-category-organization,.p-category-time-series';
// igoshougi channel sepcial div selector
if (category === 'igoshougi') {
parentSelector = 'div.uni-news-igoshougi-top';
}
categoryName = $('h1.p-header-category-current-title,h1.p-header-category-content-title').text();
// p-header-category-content-title
list = $(parentSelector).find('li').not('.p-member-only').not('.p-ad-list-item');
list = list.map((index, item) => {
item = $(item);
// remove matome title
const matomeTitle = item.find('div.c-matome-title');
if (matomeTitle.length !== 0) {
matomeTitle.remove();
}
return item;
});
}
// format category name
let formattedCategoryName = categoryName;
if (categoryName !== '') {
formattedCategoryName = ' - ' + categoryName.trim();
$('.p-category-reading-recommend').remove();
list = $('.layout-contents__main .c-list-title')
.toArray()
.map((item) => {
item = $(item);
const a = item.find('h3 a');
const parent = item.parent();
return {
title: a.text(),
link: a.attr('href'),
pubDate: timezone(parseDate(parent.find('time').attr('datetime')), +9),
locked: parent.find('.c-list-member-only').length,
};
});
}
const items = await Promise.all(
list
.map(async (index, item) => {
item = $(item);
let link = '';
if (categoryName === '新着・速報') {
link = item.find('h3').children('a').attr('href');
} else {
link = item.find('a').attr('href');
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
if (item.locked) {
return item;
}
const description = await ctx.cache.tryGet(link, async () => {
const response = await got(link);
const $ = cheerio.load(response.data);
const mainContent = $('div.p-main-contents');
// remove articles recomended
const recommendNode = mainContent.find('aside.ev-article-manual-related__article-inline');
if (recommendNode.length !== 0) {
recommendNode.remove();
}
return mainContent.html();
const response = await got(item.link);
const $ = cheerio.load(response.data);
const mainContent = $('.p-main-contents');
mainContent.find('[class^=ev-article], svg').remove();
mainContent.find('img').each((_, img) => {
img.attribs.src = img.attribs.src.split('?')[0];
});
let tag = '';
if (categoryName === '新着・速報') {
const tag_table = {
news: '新着・速報',
national: '社会',
politics: '政治',
economy: '経済',
sports: 'スポーツ',
world: '国際',
science: '科学・IT',
election: '選挙・世論調査',
culture: 'エンタメ・文化',
igoshougi: '囲碁・将棋',
life: 'ライフ',
local: '地域',
editorial: '社説',
medical: '医療・健康',
olympic: 'オリンピック',
koushitsu: '皇室',
};
const tag_value = tag_table[link.split('/')[3]];
tag = tag_value ? tag_value : 'その他';
tag = '[' + tag + '] ';
}
item.description = mainContent.html();
item.pubDate = parseDate($('meta[property="article:published_time"]').attr('content')); // 2023-05-17T22:33:00+09:00
item.updated = parseDate($('meta[property="article:modified_time"]').attr('content'));
return {
title: tag + item.find('a').text(),
description,
pubDate: timezone(parseDate(item.find('time').prop('datetime')), +9),
link,
};
const tag = $('.p-header-category-breadcrumbs li a').last().text();
item.category = tag;
item.title = `[${tag}] ${item.title}`;
return item;
})
.get()
)
);
ctx.state.data = {
title: '読売新聞' + formattedCategoryName,
title: $('head title').text(),
link: url,
image: 'https://www.yomiuri.co.jp/apple-touch-icon.png',
item: items,
};
};
+3 -3
View File
@@ -3,10 +3,10 @@ module.exports = {
_name: '読売新聞',
www: [
{
title: '読売新聞',
title: '新聞',
docs: 'https://docs.rsshub.app/traditional-media.html#du-mai-xin-wen',
source: ['/:category'],
target: '/yomiuri/:category',
source: ['/:category?'],
target: '/yomiuri/:category?',
},
],
},