From 809dd41ba985ac977f7e21781acdedb298f65a04 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 17 May 2023 22:54:43 +0700 Subject: [PATCH] fix(route): yomiuri (#12526) --- docs/en/traditional-media.md | 23 +++++- docs/traditional-media.md | 23 +++++- lib/v2/yomiuri/maintainer.js | 2 +- lib/v2/yomiuri/news.js | 150 ++++++++++++----------------------- lib/v2/yomiuri/radar.js | 6 +- 5 files changed, 92 insertions(+), 112 deletions(-) diff --git a/docs/en/traditional-media.md b/docs/en/traditional-media.md index 25c97e62e..33f9b3234 100644 --- a/docs/en/traditional-media.md +++ b/docs/en/traditional-media.md @@ -750,12 +750,27 @@ Provides all of the articles by the specified Yahoo! author. ### News - + 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 | diff --git a/docs/traditional-media.md b/docs/traditional-media.md index d392dd26f..5430478b3 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -912,13 +912,28 @@ IT・科学 tech_science ### 新聞 - + 无料全文,综合页 (新着・速報) 文章标题补充板块标签。 -| 新着・速報 | 社会 | 政治 | 経済 | スポーツ | 国際 | 科学・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 | diff --git a/lib/v2/yomiuri/maintainer.js b/lib/v2/yomiuri/maintainer.js index 219f20c4d..f240e8217 100644 --- a/lib/v2/yomiuri/maintainer.js +++ b/lib/v2/yomiuri/maintainer.js @@ -1,3 +1,3 @@ module.exports = { - '/yomiuri/:category?': ['Arracc'], + '/:category?': ['Arracc'], }; diff --git a/lib/v2/yomiuri/news.js b/lib/v2/yomiuri/news.js index f5c5bb586..c47031d81 100644 --- a/lib/v2/yomiuri/news.js +++ b/lib/v2/yomiuri/news.js @@ -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, }; }; diff --git a/lib/v2/yomiuri/radar.js b/lib/v2/yomiuri/radar.js index 5030af25b..fd1bd7fab 100644 --- a/lib/v2/yomiuri/radar.js +++ b/lib/v2/yomiuri/radar.js @@ -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?', }, ], },