feat(route): Add Onet (#14200)
* feat(route): Add Onet * use arrow function in `router.js` * Update lib/v2/onet/templates/article.art * Update lib/v2/onet/templates/image.art * Update lib/v2/onet/maintainer.js * Update website/docs/routes/new-media.mdx ---------
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
'/news': ['Vegann'],
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
const parser = require('@/utils/rss-parser');
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const { art } = require('@/utils/render');
|
||||
const path = require('path');
|
||||
const { parseArticleContent, parseMainImage } = require('./utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const rssUrl = 'https://wiadomosci.onet.pl/.feed';
|
||||
const feed = await parser.parseURL(rssUrl);
|
||||
const items = await Promise.all(
|
||||
feed.items.map(async (item) => {
|
||||
const { description, author, category } = await ctx.cache.tryGet(item.link, async () => {
|
||||
const { data: response } = await got(item.link, {
|
||||
headers: {
|
||||
referer: 'https://www.onet.pl/', // for some reason onet.pl will redirect to the main page if referer is not set
|
||||
},
|
||||
});
|
||||
|
||||
const $ = cheerio.load(response);
|
||||
const content = parseArticleContent($);
|
||||
|
||||
const mainImage = parseMainImage($);
|
||||
|
||||
const description = art(path.join(__dirname, 'templates/article.art'), {
|
||||
mainImage,
|
||||
lead: $('#lead').text()?.trim(),
|
||||
content: content.html()?.trim(),
|
||||
});
|
||||
|
||||
const author = $('.authorNameWrapper span[itemprop="name"]').text()?.trim();
|
||||
const category = $('span.relatedTopic').text()?.trim();
|
||||
|
||||
return { description, author, category };
|
||||
});
|
||||
return {
|
||||
title: item.title,
|
||||
link: item.link,
|
||||
description,
|
||||
author,
|
||||
category,
|
||||
pubDate: parseDate(item.pubDate),
|
||||
guid: item.id,
|
||||
};
|
||||
})
|
||||
);
|
||||
ctx.state.data = {
|
||||
title: feed.title,
|
||||
link: feed.link,
|
||||
description: feed.title,
|
||||
item: items,
|
||||
language: 'pl',
|
||||
image: 'https://ocdn.eu/wiadomosciucs/static/logo2017/onet2017big_dark.png',
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
module.exports = {
|
||||
'onet.pl': {
|
||||
_name: 'Onet',
|
||||
wiadomosci: [
|
||||
{
|
||||
title: 'News',
|
||||
docs: 'https://docs.rsshub.app/routes/new-media#onet',
|
||||
source: '/',
|
||||
target: '/onet/news',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = (router) => {
|
||||
router.get('/news', require('./news'));
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
{{if lead }}
|
||||
<p><strong>{{ lead }}</strong></p>
|
||||
{{/if}}
|
||||
{{@ mainImage }}
|
||||
{{@ content }}
|
||||
@@ -0,0 +1,9 @@
|
||||
<figure>
|
||||
<img src="{{ url }}" alt="{{ alt }}">
|
||||
<figcation>
|
||||
{{if caption }}
|
||||
<cite>{{ caption }}</cite> -
|
||||
{{/if}}
|
||||
{{ author }}
|
||||
</figcation>
|
||||
</figure>
|
||||
@@ -0,0 +1,51 @@
|
||||
const { art } = require('@/utils/render');
|
||||
const path = require('path');
|
||||
|
||||
const parseMainImage = ($) => {
|
||||
const mainImage = $('figure.mainPhoto');
|
||||
const img = mainImage.find('img');
|
||||
const author = mainImage.find('span.copyright');
|
||||
const caption = mainImage.find('span.imageDescription');
|
||||
|
||||
return art(path.join(__dirname, 'templates/image.art'), {
|
||||
url: img.attr('src'),
|
||||
alt: img.attr('alt')?.trim(),
|
||||
author: author.text()?.trim(),
|
||||
caption: caption.text()?.trim(),
|
||||
});
|
||||
};
|
||||
|
||||
const parseArticleContent = ($) => {
|
||||
const content = $('[itemprop="articleBody"]');
|
||||
$('*')
|
||||
.contents()
|
||||
.filter(function () {
|
||||
return this.nodeType === 8;
|
||||
})
|
||||
.remove();
|
||||
content.find('aside').remove();
|
||||
content.find('.videoPlayerContainer').remove();
|
||||
content.find('.pulsevideo').remove();
|
||||
content.find('.adsContainer').remove();
|
||||
content.find('.placeholder').remove();
|
||||
content.find('.contentPremium').removeAttr('style');
|
||||
content.find('div.image').each((i, el) => {
|
||||
const img = $(el).find('img');
|
||||
const author = $(el).find('span.author');
|
||||
const caption = $(el).find('span.caption');
|
||||
const html = art(path.join(__dirname, 'templates/image.art'), {
|
||||
url: img.attr('src'),
|
||||
alt: img.attr('alt')?.trim(),
|
||||
caption: caption.text()?.trim(),
|
||||
author: author.text()?.trim(),
|
||||
});
|
||||
|
||||
$(el).replaceWith(html);
|
||||
});
|
||||
|
||||
return content;
|
||||
};
|
||||
module.exports = {
|
||||
parseArticleContent,
|
||||
parseMainImage,
|
||||
};
|
||||
@@ -1107,6 +1107,14 @@
|
||||
|
||||
<Route author="ncziztk" example="/odaily/activity" path="/odaily/activity" />
|
||||
|
||||
## Onet {#onet}
|
||||
|
||||
### News {#onet-news}
|
||||
|
||||
<Route author="Vegann" example="/onet/news" path="/onet/news" radar="1">
|
||||
This route provides a better reading experience (full text articles) over the official one for `https://wiadomosci.onet.pl`.
|
||||
</Route>
|
||||
|
||||
## OpenAI {#openai}
|
||||
|
||||
### Blog {#openai-blog}
|
||||
|
||||
Reference in New Issue
Block a user