From 2c268759b25f5cab0a07fb467d882bee36f343ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=8D=A1=E7=90=B3?= Date: Sun, 27 May 2018 17:12:19 +0800 Subject: [PATCH] rss: add Konachan.com Anime Wallpapers (#208) * rss: add Konachan.com Anime Wallpapers * switch to ctx.params --- README.md | 3 ++ docs/README.md | 29 ++++++++++++ router.js | 6 +++ routes/konachan/post.js | 55 +++++++++++++++++++++++ routes/konachan/post_popular_recent.js | 62 ++++++++++++++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 routes/konachan/post.js create mode 100644 routes/konachan/post_popular_recent.js diff --git a/README.md b/README.md index bf8cee963..261bbb98a 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,9 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 * 分类 * GitHub * 用户仓库 +* Konachan.com Anime Wallpapers + * posts + * Popular Recent Posts ## 鸣谢 diff --git a/docs/README.md b/docs/README.md index f68356a9c..d6a55b8a4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -920,3 +920,32 @@ key: 产品密钥 | 热门话题 | 科技动态 | 开发者资讯 | 区块链快讯 | | -------- | -------- | ---------- | ---------- | | topic | news | technews | blockchain | + +## Konachan.com Anime Wallpapers + +### Posts + +路由: + +* `/konachan/post` +* `/konachan/post/:tags` + +举例: + +* [https://rsshub.app/konachan/post](https://rsshub.app/konachan/post) +* [https://rsshub.app/konachan/post/touhou](https://rsshub.app/konachan/post/touhou) +* [https://rsshub.app/konachan/post/panties+rating%3Asafe](https://rsshub.app/konachan/post/panties+rating%3Asafe) + +### Popular Recent Posts + +路由: + +* `/konachan/post/popular_recent` 默认过去 24 小时 +* `/konachan/post/popular_recent/:period` + +举例: + +* 过去 24 小时:[https://rsshub.app/konachan/post/popular_recent/1d](https://rsshub.app/konachan/post/popular_recent/1d) +* 过去一周:[https://rsshub.app/konachan/post/popular_recent/1w](https://rsshub.app/konachan/post/popular_recent/1w) +* 过去一月:[https://rsshub.app/konachan/post/popular_recent/1m](https://rsshub.app/konachan/post/popular_recent/1m) +* 过去一年:[https://rsshub.app/konachan/post/popular_recent/1y](https://rsshub.app/konachan/post/popular_recent?period=1y) diff --git a/router.js b/router.js index d06c6a28a..d19b5dccf 100644 --- a/router.js +++ b/router.js @@ -243,4 +243,10 @@ if (config.github && config.github.access_token) { logger.warn('GitHub RSS is disabled for lacking config.'); } +// konachan +router.get('/konachan/post', require('./routes/konachan/post')); +router.get('/konachan/post/popular_recent', require('./routes/konachan/post_popular_recent')); +router.get('/konachan/post/:tags', require('./routes/konachan/post')); +router.get('/konachan/post/popular_recent/:period', require('./routes/konachan/post_popular_recent')); + module.exports = router; diff --git a/routes/konachan/post.js b/routes/konachan/post.js new file mode 100644 index 000000000..6c490d64e --- /dev/null +++ b/routes/konachan/post.js @@ -0,0 +1,55 @@ +const axios = require('../../utils/axios'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const { tags = '' } = ctx.params; + + const response = await axios({ + method: 'get', + url: `https://konachan.com/post.json?tags=${tags}`, + headers: { + 'User-Agent': config.ua, + }, + }); + + const posts = response.data; + + let title = 'Konachan.com Anime Wallpapers'; + if (tags) { + title = decodeURIComponent(tags) + ' - ' + title; + } + + ctx.state.data = { + title, + link: 'https://konachan.com/post', + item: posts.map((post) => { + const content = (url) => { + let result = ``; + if (post.source) { + result += `source`; + } + if (post.parent_id) { + result += `parent`; + } + return result; + }; + + const created_at = post.created_at * 1e3; + + return { + title: post.tags, + id: `${ctx.path}#${post.id}`, + guid: `${ctx.path}#${post.id}`, + link: `https://konachan.com/post/show/${post.id}`, + author: post.author, + published: new Date(created_at).toISOString(), + pubDate: new Date(created_at).toUTCString(), + description: content(post.sample_url), + summary: content(post.sample_url), + content: content(post.file_url), + image: post.file_url, + category: post.tags.split(/\s+/), + }; + }), + }; +}; diff --git a/routes/konachan/post_popular_recent.js b/routes/konachan/post_popular_recent.js new file mode 100644 index 000000000..d937ba8c6 --- /dev/null +++ b/routes/konachan/post_popular_recent.js @@ -0,0 +1,62 @@ +const axios = require('../../utils/axios'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const { period = '1d' } = ctx.params; + + const response = await axios({ + method: 'get', + url: 'https://konachan.com/post/popular_recent.json', + headers: { + 'User-Agent': config.ua, + }, + params: { + period, + }, + }); + + const posts = response.data; + + const titles = { + '1d': 'Exploring last 24 hours ', + '1w': 'Exploring last week', + '1m': 'Exploring last month', + '1y': 'Exploring last year', + }; + + const title = titles[period] || titles['1d']; + + ctx.state.data = { + title: `${title} - Konachan.com Anime Wallpapers`, + link: 'https://konachan.com/post/popular_recent', + item: posts.map((post) => { + const content = (url) => { + let result = ``; + if (post.source) { + result += `source`; + } + if (post.parent_id) { + result += `parent`; + } + return result; + }; + + const created_at = post.created_at * 1e3; + + return { + title: post.tags, + id: `${ctx.path}#${post.id}`, + guid: `${ctx.path}#${post.id}`, + link: `https://konachan.com/post/show/${post.id}`, + author: post.author, + published: new Date(created_at).toISOString(), + pubDate: new Date(created_at).toUTCString(), + description: content(post.sample_url), + summary: content(post.sample_url), + content: content(post.file_url), + image: post.file_url, + category: post.tags.split(/\s+/), + }; + }), + }; +};