diff --git a/README.md b/README.md index 0f8daf54f..b4a20fd4c 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - 新闻早报 - UU 看书 - 小说章节 +- 喜马拉雅 + - 专辑 ## 鸣谢 diff --git a/docs/README.md b/docs/README.md index 30addfe89..cc37b2625 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1084,3 +1084,17 @@ language,语言,可在 [Trending 页](https://github.com/trending/javascript 路由: `/uukanshu/chapter/:id` 参数: id,小说 id,可在对应小说页 URL 中找到 + +## 喜马拉雅 + +### 专辑 + +举例: [https://rsshub.app/ximalaya/album/shangye/299146/](https://rsshub.app/ximalaya/album/shangye/299146/) + +路由: `/ximalaya/album/:classify/:id` + +参数: + +classify, 专辑分类, 可在对应专辑页面的 URL 中找到 + +id, 专辑 id, 可在对应专辑页面的 URL 中找到 diff --git a/router.js b/router.js index 50178cec8..533cc01a9 100644 --- a/router.js +++ b/router.js @@ -284,4 +284,7 @@ router.get('/nytimes/morning_post', require('./routes/nytimes/morning_post')); // uukanshu router.get('/uukanshu/chapter/:uid', require('./routes/uukanshu/chapter')); +// 喜马拉雅 +router.get('/ximalaya/album/:classify/:id', require('./routes/ximalaya/album')); + module.exports = router; diff --git a/routes/ximalaya/album.js b/routes/ximalaya/album.js new file mode 100644 index 000000000..e9a0c1153 --- /dev/null +++ b/routes/ximalaya/album.js @@ -0,0 +1,53 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); +const formatPubDate = require('../../utils/pubDate.js'); + +const baseUrl = 'http://www.ximalaya.com'; +module.exports = async (ctx) => { + const id = ctx.params.id; // 专辑id + const classify = ctx.params.classify; // 专辑分类 + const apiUrl = `${baseUrl}/revision/album/getTracksList?albumId=${id}&pageNum=1&sort=1`; + const responseApi = await axios({ + method: 'get', + url: `${apiUrl}`, + headers: { + 'User-Agent': config.ua, + Host: 'www.ximalaya.com', + Referer: `${baseUrl}/${classify}/${id}`, + }, + }); + const responseDom = await axios({ + method: 'get', + url: `${baseUrl}/${classify}/${id}`, + headers: { + 'User-Agent': config.ua, + Host: 'www.ximalaya.com', + Referer: `${baseUrl}/${classify}/${id}`, + }, + }); + const $ = cheerio.load(responseDom.data); + const title = $('div.info > h1') + .eq(0) + .text(); + const cover_url = $('div.album-info>img') + .eq(0) + .attr('src'); + const trackList = responseApi.data.data.tracks; + const items = []; + for (let i = 0; i < trackList.length; i++) { + const track = trackList[i]; + const item = { + title: track.title, + link: baseUrl + track.url, + pubDate: formatPubDate(track.createDateFormat), + }; + items.push(item); + } + ctx.state.data = { + title: `喜马拉雅专辑 - ${title}`, + link: `${baseUrl}/${classify}/${id}`, + image: cover_url, + item: items, + }; +}; diff --git a/utils/pubDate.js b/utils/pubDate.js new file mode 100644 index 000000000..36b7513aa --- /dev/null +++ b/utils/pubDate.js @@ -0,0 +1,47 @@ +// 格式化 类型这个的时间 , 几分钟前 | 几小时前 | 几天前 | 几月前 | 几年前 | 具体的格式不对的时间 +module.exports = (html) => { + let math; + let date = new Date(); + if (/(\d+)分钟前/.exec(html)) { + math = /(\d+)分钟前/.exec(html); + date.setMinutes(date.getMinutes() - math[1]); + return date.toUTCString(); + } else if (/(\d+)小时前/.exec(html)) { + math = /(\d+)小时前/.exec(html); + date.setHours(date.getHours() - math[1]); + return date.toUTCString(); + } else if (/(\d+)天前/.exec(html)) { + math = /(\d+)天前/.exec(html); + date.setDate(date.getDate() - math[1]); + return date.toUTCString(); + } else if (/(\d+)月前/.exec(html)) { + math = /(\d+)月前/.exec(html); + date.setMonth(date.getMonth() - math[1]); + return date.toUTCString(); + } else if (/(\d+)年前/.exec(html)) { + math = /(\d+)年前/.exec(html); + date.setFullYear(date.getFullYear() - math[1]); + return date.toUTCString(); + } else if (/今天 (\d+):(\d+)/.exec(html)) { + math = /今天 (\d+):(\d+)/.exec(html); + date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), math[1], math[2]); + return date.toUTCString(); + } else if (/昨天 (\d+):(\d+)/.exec(html)) { + math = /昨天 (\d+):(\d+)/.exec(html); + date = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1, math[1], math[2]); + return date.toUTCString(); + } else if (/(\d+)月(\d+)日 (\d+):(\d+)/.exec(html)) { + math = /(\d+)月(\d+)日 (\d+):(\d+)/.exec(html); + date = new Date(date.getFullYear(), parseInt(math[1]) - 1, math[2], math[3], math[4]); + return date.toUTCString(); + } else if (/(\d+)-(\d+)-(\d+)/.exec(html)) { + math = /(\d+)-(\d+)-(\d+)/.exec(html); + date = new Date(math[1], parseInt(math[2]) - 1, math[3]); + return date.toUTCString(); + } else if (/(\d+)-(\d+)/.exec(html)) { + math = /(\d+)-(\d+)/.exec(html); + date = new Date(date.getFullYear(), parseInt(math[1]) - 1, math[2]); + return date.toUTCString(); + } + return html; +};