Skip to content

Commit

Permalink
feat(route): add fisher spb news (DIYgod#8343)
Browse files Browse the repository at this point in the history
* feat(route): add fisher spb news

* Apply suggestions from code review

Co-authored-by: Tony <[email protected]>

* templates art for html render

Co-authored-by: Tony <[email protected]>
  • Loading branch information
denis-ya and Tony authored Oct 6, 2021
1 parent c954d14 commit 8f08dba
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/en/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,9 @@ Type
| all | rec |

</RouteEn>

## Fisher Spb

### News

<RouteEn author="denis-ya" example="/fisher-spb/news" path="/fisher-spb/news" />
3 changes: 3 additions & 0 deletions lib/v2/fisher-spb/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/news': ['denis-ya'],
};
50 changes: 50 additions & 0 deletions lib/v2/fisher-spb/news.js
Original file line number Diff line number Diff line change
@@ -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
};
};
13 changes: 13 additions & 0 deletions lib/v2/fisher-spb/radar.js
Original file line number Diff line number Diff line change
@@ -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',
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/fisher-spb/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news', require('./news'));
};
1 change: 1 addition & 0 deletions lib/v2/fisher-spb/templates/image.art
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img style="max-width: 650px; height: auto; object-fit: contain; flex: 0 0 auto;" src="{{ href }}"></img><br/>
1 change: 1 addition & 0 deletions lib/v2/fisher-spb/templates/video.art
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<iframe type="text/html" width="640" height="360" src="https://youtube.com/embed/{{ link }}" frameborder="0" allowfullscreen></iframe><br/>

0 comments on commit 8f08dba

Please sign in to comment.