feat(route): add Bad.news (#12547)

* feat(route): add Bad.news

* fix: load lazy images and video posters
This commit is contained in:
Ethan Shen 2023-05-23 06:40:02 +08:00 committed by GitHub
parent dd2be12722
commit 8e147c70b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 0 deletions

View File

@ -185,6 +185,22 @@ pageClass: routes
<Route author="nczitzk" example="/asml/press-releases" path="/asml/press-releases"/>
## Bad.news
### 通用
<Route author="nczitzk" example="/bad" path="/bad/:path+" :paramsDesc="['路径,默认为首页热门']">
::: tip 提示
若订阅 [每日热点 - 最新](https://bad.news/tag/每日热点/sort-new),网址为 <https://bad.news/tag/每日热点/sort-new>。截取 `https://bad.news` 到末尾的部分 `/tag/每日热点/sort-new` 作为参数,此时路由为 [`/bad/tag/每日热点/sort-new`](https://rsshub.app/bad/tag/每日热点/sort-new)。
若订阅子分类 [大陆资讯 - 热门](https://bad.news/tag/大陆资讯/sort-hot),网址为 <https://bad.news/tag/大陆资讯/sort-hot>。截取 `https://bad.news` 到末尾的部分 `/tag/大陆资讯/sort-hot` 作为参数,路由为 [`/bad/tag/大陆资讯/sort-hot`](https://rsshub.app/bad/tag/大陆资讯/sort-hot)。
:::
</Route>
## Bell Labs
### Event and News

58
lib/v2/bad/index.js Normal file
View File

@ -0,0 +1,58 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const rootUrl = 'https://bad.news';
const currentUrl = `${rootUrl}${ctx.path === '/' ? '' : ctx.path}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
$('.option, .pagination').remove();
const items = $('.entry')
.toArray()
.map((item) => {
item = $(item);
const a = item.find('a.title');
item.find('img').each(function () {
$(this).attr('src', $(this).attr('data-echo'));
$(this).removeClass('lazy');
$(this).removeAttr('data-echo');
$(this).removeAttr('id');
});
item.find('video').each(function () {
$(this).attr('poster', $(this).attr('data-echo'));
$(this).removeAttr('data-echo');
$(this).removeAttr('onerror');
$(this).removeAttr('id');
});
return {
title: a.text(),
link: a.attr('href'),
description: item.find('.coverdiv').html(),
author: item.find('.author').text().trim(),
pubDate: timezone(parseDate(item.find('time').attr('datetime')), +8),
category: item
.find('.label')
.toArray()
.map((l) => $(l).text().trim()),
};
});
ctx.state.data = {
title: `Bad.news - ${$('.active').text()}${$('.selected').text()}`,
link: currentUrl,
item: items,
};
};

3
lib/v2/bad/maintainer.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
'/:path+': ['nczitzk'],
};

13
lib/v2/bad/radar.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = {
'bad.news': {
_name: 'Bad.news',
'.': [
{
title: '通用',
docs: 'https://docs.rsshub.app/new-media.html#bad-news-tong-yong',
source: ['/'],
target: (params, url) => `/bad${new URL(url).href.match(/bad\.news(.*?)/)[1]}`,
},
],
},
};

3
lib/v2/bad/router.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = function (router) {
router.get(/([\w-/]+)?/, require('./index'));
};