feat(route)(kyodonews): icon, main pic, updated date (#9459)
Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com>
This commit is contained in:
parent
7399d17591
commit
32fe3aeb65
|
|
@ -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('<div>');
|
||||
});
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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: '關鍵詞',
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
{{ if mainPic }}
|
||||
{{@ mainPic }}<br><br>
|
||||
{{ /if }}
|
||||
{{@ articleBody }}
|
||||
Loading…
Reference in New Issue