diff --git a/docs/social-media.md b/docs/social-media.md index d15d6bfaf..bcacf90a7 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -408,6 +408,12 @@ pageClass: routes +## 唱吧 + +### 用户 + + + ## 豆瓣 ### 正在上映的电影 diff --git a/lib/router.js b/lib/router.js index bfb0d0007..72a8edf65 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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; diff --git a/lib/routes/changba/user.js b/lib/routes/changba/user.js new file mode 100644 index 000000000..fd3f8b9fe --- /dev/null +++ b/lib/routes/changba/user.js @@ -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() + '
' + $.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: '唱吧', + }; +};