feat(route): add fisher spb news (#8343)

* feat(route): add fisher spb news

* Apply suggestions from code review

Co-authored-by: Tony <TonyRL@users.noreply.github.com>

* templates art for html render

Co-authored-by: Tony <TonyRL@users.noreply.github.com>
This commit is contained in:
denis-ya 2021-10-06 09:26:24 +03:00 committed by GitHub
parent c954d142d6
commit 8f08dba301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 77 additions and 0 deletions

View File

@ -220,3 +220,9 @@ Type
| all | rec |
</RouteEn>
## Fisher Spb
### News
<RouteEn author="denis-ya" example="/fisher-spb/news" path="/fisher-spb/news" />

View File

@ -0,0 +1,3 @@
module.exports = {
'/news': ['denis-ya'],
};

50
lib/v2/fisher-spb/news.js Normal file
View File

@ -0,0 +1,50 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const renderVideo = (link) => art(path.join(__dirname, './templates/video.art'), { link });
const renderImage = (href) => art(path.join(__dirname, './templates/image.art'), { href });
const rootUrl = 'https://fisher.spb.ru/news/';
const response = await got({
method: 'get',
url: rootUrl,
responseType: 'buffer'
});
const $ = cheerio.load(iconv.decode(response.data, 'windows-1251'));
const descBuilder = (element) => {
const content = cheerio.load(`<p>${$('.news-message-text', element).html()}</p>`).root();
$('.news-message-media a', element).each((_, elem) => {
if ($(elem).hasClass('news-message-youtube')) {
content.append(renderVideo($(elem).attr('data-youtube')));
} else {
content.append(renderImage($(elem).attr('href')));
}
});
return content;
};
const items = $('.news-message')
.map((_, elem) => ({
pubDate: parseDate($('.news-message-date', elem).text().trim(), 'DD.MM.YYYY HH:mm'),
title: $('.news-message-location', elem).text().trim(),
description: descBuilder(elem).html(),
author: $('.news-message-user', elem).text().trim(),
guid: $(elem).attr('id'),
link: rootUrl + $('.news-message-comments-number > a', elem).attr('href')
}))
.get();
ctx.state.data = {
title: $('head > title').text().trim(),
link: rootUrl,
item: items
};
};

View File

@ -0,0 +1,13 @@
module.exports = {
'fisher.spb.ru': {
_name: 'fisher spb',
'.': [
{
title: 'news',
docs: 'https://docs.rsshub.app/en/other.html#fisher-spb',
source: ['/news'],
target: '/fisher-spb/news',
},
],
},
};

View File

@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news', require('./news'));
};

View File

@ -0,0 +1 @@
<img style="max-width: 650px; height: auto; object-fit: contain; flex: 0 0 auto;" src="{{ href }}"></img><br/>

View File

@ -0,0 +1 @@
<iframe type="text/html" width="640" height="360" src="https://youtube.com/embed/{{ link }}" frameborder="0" allowfullscreen></iframe><br/>