diff --git a/lib/v2/onet/maintainer.js b/lib/v2/onet/maintainer.js new file mode 100644 index 000000000..8a1875e34 --- /dev/null +++ b/lib/v2/onet/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news': ['Vegann'], +}; diff --git a/lib/v2/onet/news.js b/lib/v2/onet/news.js new file mode 100644 index 000000000..225e146a7 --- /dev/null +++ b/lib/v2/onet/news.js @@ -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', + }; +}; diff --git a/lib/v2/onet/radar.js b/lib/v2/onet/radar.js new file mode 100644 index 000000000..f29d2f6b0 --- /dev/null +++ b/lib/v2/onet/radar.js @@ -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', + }, + ], + }, +}; diff --git a/lib/v2/onet/router.js b/lib/v2/onet/router.js new file mode 100644 index 000000000..630036b8e --- /dev/null +++ b/lib/v2/onet/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/news', require('./news')); +}; diff --git a/lib/v2/onet/templates/article.art b/lib/v2/onet/templates/article.art new file mode 100644 index 000000000..36ed5db53 --- /dev/null +++ b/lib/v2/onet/templates/article.art @@ -0,0 +1,5 @@ +{{if lead }} +

{{ lead }}

+{{/if}} +{{@ mainImage }} +{{@ content }} diff --git a/lib/v2/onet/templates/image.art b/lib/v2/onet/templates/image.art new file mode 100644 index 000000000..a4a8c8724 --- /dev/null +++ b/lib/v2/onet/templates/image.art @@ -0,0 +1,9 @@ +
+ {{ alt }} + + {{if caption }} + {{ caption }} - + {{/if}} + {{ author }} + +
diff --git a/lib/v2/onet/utils.js b/lib/v2/onet/utils.js new file mode 100644 index 000000000..af8a9a814 --- /dev/null +++ b/lib/v2/onet/utils.js @@ -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, +}; diff --git a/website/docs/routes/new-media.mdx b/website/docs/routes/new-media.mdx index 02fd7947f..e0c42d38c 100644 --- a/website/docs/routes/new-media.mdx +++ b/website/docs/routes/new-media.mdx @@ -1107,6 +1107,14 @@ +## Onet {#onet} + +### News {#onet-news} + + + This route provides a better reading experience (full text articles) over the official one for `https://wiadomosci.onet.pl`. + + ## OpenAI {#openai} ### Blog {#openai-blog}