diff --git a/docs/README.md b/docs/README.md index 7e81cfeefb..634cc0aaa5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -536,6 +536,8 @@ RSSHub 提供下列 API 接口: + + ### Disqus diff --git a/lib/router.js b/lib/router.js index a34a7abe19..8c53f9128b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -214,6 +214,7 @@ router.get('/douban/book/rank/:type', require('./routes/douban/book/rank')); router.get('/douban/doulist/:id', require('./routes/douban/doulist')); router.get('/douban/explore/column/:id', require('./routes/douban/explore_column')); router.get('/douban/people/:userid/status', require('./routes/douban/people/status.js')); +router.get('/douban/topic/:id/:sort?', require('./routes/douban/topic.js')); // 煎蛋 router.get('/jandan/:sub_model', require('./routes/jandan/pic')); diff --git a/lib/routes/douban/topic.js b/lib/routes/douban/topic.js new file mode 100644 index 0000000000..df97da8cef --- /dev/null +++ b/lib/routes/douban/topic.js @@ -0,0 +1,78 @@ +const axios = require('../../utils/axios'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + const sort = ctx.params.sort || 'new'; + + const link = `https://www.douban.com/gallery/topic/${id}/?sort=${sort}`; + + const api = `https://m.douban.com/rexxar/api/v2/gallery/topic/${id}/items?sort=${sort}&start=0&count=10&status_full_text=1`; + const response = await axios({ + method: 'GET', + url: api, + headers: { + Referer: link, + }, + }); + + const data = response.data.items; + const title = data[0].topic.name; + const description = data[0].topic.introduction; + + const out = await Promise.all( + data.map(async (item) => { + const type = item.target.type; + let author; + let date; + let description; + let link; + let title; + if (type === 'status') { + link = item.target.status.sharing_url; + author = item.target.status.author.name; + title = author + '的广播'; + date = item.target.status.create_time; + description = item.target.status.text; + const images = item.target.status.images; + if (images) { + let i; + for (i in images) { + description += `
`; + } + } + } else { + link = item.target.sharing_url; + author = item.target.author.name; + title = author + '的日记'; + date = item.target.create_time; + + const id = item.target.id; + const itemUrl = `https://www.douban.com/j/note/${id}/full`; + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const response = await axios.get(itemUrl); + description = response.data.html; + } + const single = { + title: title, + link: link, + author: author, + pubDate: new Date(date).toUTCString(), + description: description, + }; + + ctx.cache.set(link, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `${title}-豆瓣话题`, + description: description, + link: link, + item: out, + }; +};