feat: add changba (#2482)

This commit is contained in:
Cloud 2019-06-28 13:00:06 +08:00 committed by DIYgod
parent fb2a3ca9aa
commit 5d9b7ebe37
3 changed files with 80 additions and 0 deletions

View File

@ -408,6 +408,12 @@ pageClass: routes
<Route author="LogicJake" example="/bihu/activaties/1478342200" path="/bihu/activaties/:id" :paramsDesc="['用户 id']"/>
## 唱吧
### 用户
<Route author="kt286" example="/changba/34108440" path="/changba/:userid" :paramsDesc="['用户ID, 可在对应页面的 URL 中找到']" supportPodcast="1"/>
## 豆瓣
### 正在上映的电影

View File

@ -1446,4 +1446,7 @@ router.get('/csrc/fashenwei', require('./routes/csrc/fashenwei'));
// LWN.net Alerts
router.get('/lwn/alerts/:distributor', require('./routes/lwn/alerts'));
// 唱吧
router.get('/changba/:userid', require('./routes/changba/user'));
module.exports = router;

View File

@ -0,0 +1,71 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const userid = ctx.params.userid;
const url = `http://changba.com/u/${userid}`;
const response = await got({
method: 'get',
url: url,
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
},
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('.user-work .work-info').get();
const author = $('div.user-main-info > span.txt-info > a.uname').text();
const authorimg = $('div.user-main-info > .poster > img').attr('data-src');
const ProcessFeed = (data) => {
const $ = cheerio.load(data);
const description = $('.user-title').html() + '<br>' + $.html('audio');
const mp3 = $('audio').attr('src');
return { description, mp3 };
};
const items = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const link = $('a').attr('href');
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: link,
headers: {
Referer: url,
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
},
});
const result = ProcessFeed(response.data);
const single = {
title: $('.work-title').text(),
description: result.description,
link: link,
author: author,
itunes_item_image: authorimg,
enclosure_url: result.mp3,
enclosure_type: 'audio/mpeg',
};
ctx.cache.set(link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: $('title').text(),
link: url,
description: $('meta[name="description"]').attr('content') || $('title').text(),
item: items,
image: authorimg,
itunes_author: author,
itunes_category: '唱吧',
};
};