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('