喜马拉雅电台-专辑 (#263)

* 喜马拉雅电台-专辑

* 格式化

* pubDate

* pubDate 格式化

* Update pubDate.js

改为toUTCSring 进行格式化
This commit is contained in:
YibuMe 2018-06-05 22:16:29 -05:00 committed by DIYgod
parent db605677b5
commit ddeddae6af
5 changed files with 119 additions and 0 deletions

View File

@ -134,6 +134,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
- 新闻早报
- UU 看书
- 小说章节
- 喜马拉雅
- 专辑
## 鸣谢

View File

@ -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 中找到

View File

@ -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;

53
routes/ximalaya/album.js Normal file
View File

@ -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,
};
};

47
utils/pubDate.js Normal file
View File

@ -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;
};