From 23a5b7fb17f3308e99435d3551708aa18573f124 Mon Sep 17 00:00:00 2001 From: Rongrong <15956627+Rongronggg9@users.noreply.github.com> Date: Fri, 1 Apr 2022 16:27:44 +0800 Subject: [PATCH] feat(route)(kyodonews): icon, main pic, updated date Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com> --- lib/v2/kyodonews/index.js | 59 ++++++++++++++++++++------ lib/v2/kyodonews/radar.js | 2 +- lib/v2/kyodonews/templates/article.art | 4 ++ 3 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 lib/v2/kyodonews/templates/article.art diff --git a/lib/v2/kyodonews/index.js b/lib/v2/kyodonews/index.js index 2df6c220ee1978..b80ef5a3e046d1 100644 --- a/lib/v2/kyodonews/index.js +++ b/lib/v2/kyodonews/index.js @@ -2,6 +2,10 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const timezone = require('@/utils/timezone'); const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +const resolveRelativeLink = (link, baseUrl) => (link.startsWith('http') ? link : `${baseUrl}${link}`); module.exports = async (ctx) => { const language = ctx.params.language ?? 'china'; @@ -24,7 +28,9 @@ module.exports = async (ctx) => { const $ = cheerio.load(response.data, { xmlMode: keyword === 'rss' }); - let title, description, items; + let title, description, image, items; + image = `${rootUrl}/apple-touch-icon-180x180.png`; + if (keyword === 'rss') { title = $('channel > title').text(); description = $('channel > description').text(); @@ -42,12 +48,13 @@ module.exports = async (ctx) => { } else { title = $('head > title').text(); description = $('meta[name="description"]').attr('content'); + image = resolveRelativeLink($('head > link[rel="apple-touch-icon"]').attr('href'), rootUrl) || image; items = $('div.sec-latest > ul > li') .map((_, item) => { item = $(item); const link = item.find('a').attr('href'); return { - link: `${link.startsWith('http') ? '' : rootUrl}${link}`, + link: resolveRelativeLink(link, rootUrl), }; }) .get(); @@ -56,24 +63,51 @@ module.exports = async (ctx) => { items = await Promise.all( items.map((item) => ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + const detailResponse = await got(item.link); const $ = cheerio.load(detailResponse.data); item.title = $('head > title').text(); item.author = $('meta[name="author"]').attr('content'); - item.description = $('div.article-body') - .html() - .replace(/(完)(?=<\/p>\s*$)/m, ''); - const pubDate_match = $('script[type="application/ld+json"]') - .html() - .match(/"datePublished":"([\d\s-:]*?)"/); + // add main pic + const mainPicArea = $('div.mainpic'); + mainPicArea.find('div').each((_, elem) => { + elem = $(elem); + elem.css('text-align', 'center'); + }); + // moving `data-src` to `src` + mainPicArea.find('img').each((_, img) => { + img = $(img); + img.attr('src', img.attr('data-src')); + img.removeAttr('data-src'); + img.wrap('
'); + }); + let mainPic = mainPicArea.html(); + mainPic = mainPic ? mainPic.trim() : ''; + + // add article body + let articleBody = $('div.article-body').html(); + articleBody = articleBody ? articleBody.trim().replace(/(完)(?=<\/p>\s*$)/m, '') : ''; + + // render description + item.description = art(path.join(__dirname, 'templates/article.art'), { + mainPic, + articleBody, + }); + + const ldJson = $('script[type="application/ld+json"]').html(); + const pubDate_match = ldJson && ldJson.match(/"datePublished":"([\d\s-:]*?)"/); + const updated_match = ldJson && ldJson.match(/"dateModified":"([\d\s-:]*?)"/); if (pubDate_match) { item.pubDate = timezone(parseDate(pubDate_match[1]), 9); } + if (updated_match) { + item.updated = timezone(parseDate(updated_match[1]), 9); + } + + item.category = $('p.credit > a') + .map((_, a) => $(a).text()) + .get(); return item; }) ) @@ -84,5 +118,6 @@ module.exports = async (ctx) => { description, link: currentUrl, item: items, + image, }; }; diff --git a/lib/v2/kyodonews/radar.js b/lib/v2/kyodonews/radar.js index 875a36260dfd31..a9b5460e6320c6 100644 --- a/lib/v2/kyodonews/radar.js +++ b/lib/v2/kyodonews/radar.js @@ -20,7 +20,7 @@ module.exports = { title: '最新報道', docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao', source: '/', - target: '/kyodonews/tchina/:keyword?', + target: '/kyodonews/tchina', }, { title: '關鍵詞', diff --git a/lib/v2/kyodonews/templates/article.art b/lib/v2/kyodonews/templates/article.art new file mode 100644 index 00000000000000..ac741750228f11 --- /dev/null +++ b/lib/v2/kyodonews/templates/article.art @@ -0,0 +1,4 @@ +{{ if mainPic }} +{{@ mainPic }}

+{{ /if }} +{{@ articleBody }}