app: use JSON Feed as json format (#179)

This commit is contained in:
Liu 2018-05-22 23:18:35 +08:00 committed by DIYgod
parent 88b58a0b80
commit 0e258bf14d
3 changed files with 34 additions and 7 deletions

View File

@ -31,14 +31,14 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
### 输出格式
RSSHub 同时支持 RSS 2.0、Atom 和 json 输出格式,在路由末尾添加 `.rss` `.atom``.json` 即可请求对应输出格式,缺省为 RSS 2.0
RSSHub 同时支持 RSS 2.0、Atom 和 [JSON Feed](https://jsonfeed.org/) 输出格式,在路由末尾添加 `.rss` `.atom``.json` 即可请求对应输出格式,缺省为 RSS 2.0
举例:
* 缺省 RSS 2.0 - [https://rsshub.app/jianshu/home](https://rsshub.app/jianshu/home)
* RSS 2.0 - [https://rsshub.app/jianshu/home.rss](https://rsshub.app/jianshu/home.rss)
* Atom - [https://rsshub.app/jianshu/home.atom](https://rsshub.app/jianshu/home.atom)
* json - [https://rsshub.app/jianshu/home.json](https://rsshub.app/jianshu/home.json)
* JSON Feed - [https://rsshub.app/jianshu/home.json](https://rsshub.app/jianshu/home.json)
* 和 filter 或其他 URL query 一起使用 [https://rsshub.app/bilibili/user/coin/2267573.atom?filter=微小微|赤九玖|暴走大事件](https://rsshub.app/bilibili/user/coin/2267573.atom?filter=微小微|赤九玖|暴走大事件)
## RSSHub

View File

@ -20,6 +20,10 @@ module.exports = async (ctx, next) => {
template = path.resolve(__dirname, '../views/rss.art');
break;
case 'json':
template = path.resolve(__dirname, '../views/json.art');
ctx.set({
'Content-Type': 'application/json; charset=UTF-8',
});
break;
default:
template = path.resolve(__dirname, '../views/rss.art');
@ -34,11 +38,6 @@ module.exports = async (ctx, next) => {
};
if (template) {
ctx.body = art(template, data);
} else {
ctx.set({
'Content-Type': 'application/json; charset=UTF-8',
});
ctx.body = JSON.stringify(data);
}
}
};

28
views/json.art Normal file
View File

@ -0,0 +1,28 @@
{
"version": "https://jsonfeed.org/version/1",
"title": "{{ title || 'RSSHub' }}",
"home_page_url": "{{ link || 'https://github.com/DIYgod/RSSHub' }}",
"description": "{{ description || title }} - 使用 RSSHub(https://github.com/DIYgod/RSSHub) 构建",
"author": {
"name": "{{@ author || 'RSSHub' }}"
},
"items": [
{{each item}}
{
"id": "{{ $value.guid || $value.link }}",
"url": "{{ $value.link }}",
"external_url": "",
"title": "{{ $value.title }}",
"content_html": "",
"content_text": "",
"summary": "{{ $value.description }}",
"image": "{{ $value.image }}",
"banner_image": "",
"date_published": "{{ $value.pubDate }}",
"date_modified": "",
"tags": [],
"attachments": []
},
{{ /each }}
]
}