Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route)(kyodonews): icon, pic, updated date, category #9459

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions lib/v2/kyodonews/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
})
)
Expand All @@ -84,5 +118,6 @@ module.exports = async (ctx) => {
description,
link: currentUrl,
item: items,
image,
};
};
2 changes: 1 addition & 1 deletion lib/v2/kyodonews/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '關鍵詞',
Expand Down
4 changes: 4 additions & 0 deletions lib/v2/kyodonews/templates/article.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ if mainPic }}
{{@ mainPic }}<br><br>
{{ /if }}
{{@ articleBody }}