diff --git a/src/js/api.js b/src/js/api.js index 5d1db30..fa1d030 100644 --- a/src/js/api.js +++ b/src/js/api.js @@ -1,143 +1,63 @@ /** - * 音乐APP接口 - * @author yutent - * @date 2018/12/24 16:02:00 + * 部分远程接口 + * @author yutent + * @date 2020/11/20 19:07:32 */ -'use strict' +import fetch from './lib/fetch/index.js' -import request from '/lib/request/index.js' - -// `http://lyrics.kugou.com/search?client=pc&duration=188000&keyword=%E6%98%93%E7%83%8A%E5%8D%83%E7%8E%BA%20-%20%E4%BD%A0%E6%9B%BE%E6%98%AF%E5%B0%91%E5%B9%B4%20%28Live%29&man=no&ver=1` - -// `http://lyrics.kugou.com/download?ver=1&client=pc&fmt=lrc&charset=utf8&accesskey=2128A0ED9B2F6B34C3AE23EA7ACC46A7&id=43066968` - -const log = console.log - -const BASE_API_URI = 'http://mobilecdnbj.kugou.com' - -request.init({ dataType: 'json' }) - -const get = uri => { - return request.get(BASE_API_URI + uri) -} - -const post = uri => { - return request.post(BASE_API_URI + uri) +function tojson(r) { + return r.json() } export default { - getLastHot100Artists() { - return get('/api/v5/singer/list', { - data: { - sort: 1, - showtype: 1, - sextype: 0, - musician: 0, - pagesize: 100, - plat: 2, - type: 0, - page: 1 - } - }).then(res => { - if (res.status === 200) { - return res.body + searchLrc(keyword, duration) { + return fetch('http://lyrics.kugou.com/search', { + body: { + client: 'pc', + duration: duration * 1000, + keyword, + man: 'no', + ver: 1 } }) - }, - - getArtistList(sextype = 1, type = 1) { - return get('/api/v5/singer/list', { - data: { - showtype: 2, - musician: 0, - type, - sextype - } - }).then(res => { - if (res.status === 200) { - return res.body - } - }) - }, - - getArtistInfo(singerid) { - return get('/api/v3/singer/info', { data: { singerid } }).then(res => { - if (res.status === 200) { - return res.body - } - }) - }, - - getArtistInfo(singerid) { - return get('/api/v3/singer/info', { data: { singerid } }).then(res => { - if (res.status === 200) { - return res.body - } - }) - }, - - getArtistSongs(singerid, page = 1) { - return get('/api/v3/singer/song', { - data: { - sorttype: 2, - pagesize: 50, - singerid, - area_code: 1, - page - } - }).then(res => { - if (res.status === 200) { - return res.body - } - }) - }, - - getArtistAlbums(singerid, page = 1) { - return get('/api/v3/singer/album', { - data: { - pagesize: 50, - singerid, - area_code: 1, - page - } - }).then(res => { - if (res.status === 200) { - return res.body - } - }) - }, - - search(keyword, page = 1, pagesize = 20) { - return request - .get('https://songsearch.kugou.com/song_search_v2', { - data: { - keyword, - platform: 'WebFilter', - tag: '', - page, - pagesize - } - }) - .then(res => { - if (res.status === 200) { - return res.body.data.lists + .then(tojson) + .then(r => { + if (r.candidates && r.candidates.length) { + return r.candidates.map(it => ({ + id: it.id, + accesskey: it.accesskey, + duration: it.duration, + singer: it.singer, + song: it.song + })) } + return [] }) }, - getSongInfoByHash(hash, album_id = '') { - return request - .get('https://wwwapi.kugou.com/yy', { - data: { - r: 'play/getdata', - hash, - album_id - } - }) - .then(res => { - if (res.status === 200) { - return res.body.data + downloadLrc(id, accesskey) { + return fetch('http://lyrics.kugou.com/download', { + body: { + client: 'pc', + id, + accesskey, + ver: 1, + fmt: 'lrc', + charset: 'utf8' + } + }) + .then(tojson) + .then(r => { + if (r.status === 200 && r.content) { + var lrc + try { + lrc = Buffer.from(r.content, 'base64').toString() + lrc = lrc.split(/[\r\n]+/).join('\n') + } catch (e) { + console.error(e, r) + } + return lrc } }) } diff --git a/src/js/app.js b/src/js/app.js index 8da6b03..77d2f4f 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -10,6 +10,7 @@ import '/js/lib/scroll/index.js' import Keyboard from '/js/lib/keyboard/index.js' import app from '/js/lib/socket.js' import fetch from '/js/lib/fetch/index.js' +import Api from '/js/api.js' import Player from '/js/lib/audio/index.js' @@ -123,7 +124,7 @@ Anot({ break } - this.playSong(this.list[idx], idx, repeat) + this.playSong(idx, repeat) }) }, watch: { @@ -226,6 +227,15 @@ Anot({ this.previewSong(it) player.play(i) this.$refs.list.scrollTop = (i - 3) * 26 + + // if (!it.lrc) { + // Api.searchLrc(it.name, it.duration).then(list => { + // var { id, accesskey } = list[0] + // Api.downloadLrc(id, accesskey).then(lrc => { + // console.log(lrc) + // }) + // }) + // } } } })