fix(route): reuters (#11073)

This commit is contained in:
Tony 2022-10-12 20:57:52 +04:00 committed by GitHub
parent e5a997c824
commit cbe111a000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 9 deletions

View File

@ -1,6 +1,8 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const category = ctx.params.category;
@ -12,15 +14,15 @@ module.exports = async (ctx) => {
const $ = cheerio.load(response.data);
let list = $('.media-story-card__body__3tRWy a.media-story-card__heading__eqhp9')
.map((_, item) => {
.toArray()
.map((item) => {
item = $(item);
item.find('span.visually-hidden__hidden__2qXMW').remove();
return {
title: item.text(),
link: rootUrl + item.prop('href'),
};
})
.get();
});
if (!list.length) {
const metadata = $('script#fusion-metadata').html();
const metadataObj = JSON.parse(metadata.match(/Fusion.globalContent=(\{[\s\S]*?});/)[1]);
@ -36,10 +38,19 @@ module.exports = async (ctx) => {
const detailResponse = await got(item.link);
const content = cheerio.load(detailResponse.data);
item.title = content('meta[property="og:title"]').attr('content') || item.title;
item.description = content('p[data-testid="paragraph-0"]').text();
item.pubDate = parseDate(content('meta[name="article:published_time"]').attr('content'));
item.author = content('meta[name="article:author"]').attr('content');
const data = JSON.parse(
content('script#fusion-metadata')
.text()
.match(/Fusion.globalContent=(\{[\s\S]*?});/)[1]
);
item.title = data.result.title || item.title;
item.description = art(path.join(__dirname, 'templates/description.art'), {
result: data.result,
});
item.pubDate = parseDate(data.result.display_time);
item.author = data.result.authors.map((author) => author.name).join(', ');
item.category = data.result.taxonomy.keywords;
return item;
})
@ -47,7 +58,9 @@ module.exports = async (ctx) => {
);
ctx.state.data = {
title: topic ? `Reuters - ${category} - ${topic}` : `Reuters - ${category}`,
title: $('head title').text(),
description: $('head meta[name=description]').attr('content'),
image: 'https://www.reuters.com/pf/resources/images/reuters/logo-vertical-default-512x512.png?d=116',
link: currentUrl,
item: items,
};

View File

@ -22,7 +22,7 @@ module.exports = async (ctx) => {
const content = cheerio.load(detailResponse.data);
item.title = content('title').text();
item.description = content('#paragraph-0').text();
item.description = content('article.special-report').html();
item.pubDate = parseDate(content('time[itemprop="datePublished"]').attr('datetime'));
item.author = content('meta[property="og:article:publisher"]').attr('content');

View File

@ -0,0 +1,44 @@
{{ if result.summary }}
Summary:
<ul>{{ each result.summary s }}
<li>{{ s.description }}</li>
{{ /each }}</ul>
{{ /if }}
{{ if result.related_content }}
{{ each result.related_content.galleries g }}
{{ each g.content_elements e }}
{{ if e.type === 'image' }}
<figure><img src="{{ e.url }}" alt="{{ e.alt_text }}">
<figcaption>{{ e.caption }}</figcaption>
</figure>
{{ /if }}
{{ /each }}
{{ /each }}
{{ each result.related_content.images i }}
{{ if i.type === 'image' }}
<figure><img src="{{ i.url }}" alt="{{ i.alt_text }}">
<figcaption>{{ i.caption }}</figcaption>
</figure>
{{ /if }}
{{ /each }}
{{ each result.related_content.galleries v }}
{{ each v.content_elements c }}
{{ if c.type === 'video' }}
<video controls poster="{{ c.thumbnail.url }}">
<source src="{{ c.source.mp4 }}" type="video/mp4" />
</video>
{{ c.description }}
{{ /if }}
{{ /each }}
{{ /each }}
{{ /if }}
{{ if result.content_elements }}
{{ each result.content_elements e }}
{{ if e.type === 'paragraph' }}<p>{{@ e.content }}</p>{{ /if }}
{{ if e.type === 'header' }}<h{{ e.level }}>{{@ e.content }}</h{{ e.level }}>{{ /if }}
{{ /each }}
{{ /if }}
{{ if result.sign_off }}<span>{{ result.sign_off }}</span>{{ /if }}