Files
RSSHub/lib/v2/rsshub/transform/json.js
tmr 233fbd30df feat: 为html/json转化为rss提供通用支持 (#12882)
* feat: add rss proxy

* docs: 增加文档

* feat: 增加description和自动链接提取

* feat: 增加一些有用的radar

* fix: 链接补全

* fix: lint

* fix: request config

* docs: example encode

* fix: rename proxy to transform

* refactor: move it under rsshub

fix: split radar rules

* fix: maintainer build

* style: camelCase

* docs: fix example

---------
2023-08-04 00:43:39 +08:00

58 lines
1.6 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const config = require('@/config').value;
function jsonGet(obj, attr) {
if (typeof attr !== 'string') {
return obj;
}
// a.b.c
// a.b[0].c => a.b.0.c
attr.split('.').forEach((key) => {
obj = obj[key];
});
return obj;
}
module.exports = async (ctx) => {
if (!config.feature.allow_user_supply_unsafe_domain) {
ctx.throw(403, `This RSS is disabled unless 'ALLOW_USER_SUPPLY_UNSAFE_DOMAIN' is set to 'true'.`);
}
const { url } = ctx.params;
const response = await got({
method: 'get',
url,
});
const routeParams = new URLSearchParams(ctx.params.routeParams);
let rssTitle = routeParams.get('title');
if (!rssTitle) {
const resp = await got({
method: 'get',
url: new URL(url).origin,
});
const $ = cheerio.load(resp.data);
rssTitle = $('title').text();
}
const items = jsonGet(response.data, routeParams.get('item')).map((item) => {
let link = jsonGet(item, routeParams.get('itemLink')).trim();
// 补全绝对链接
if (link && !link.startsWith('http')) {
link = `${new URL(url).origin}${link}`;
}
return {
title: jsonGet(item, routeParams.get('itemTitle')),
link,
description: routeParams.get('itemDesc') ? jsonGet(item, routeParams.get('itemDesc')) : '',
};
});
ctx.state.data = {
title: rssTitle,
link: url,
description: `Proxy ${url}`,
item: items,
};
};