Lizc博客

简单获取网易云歌单以及歌词列表并生成json文件

发表于2018-04-26 04:32:16分类于前端0条评论阅读次数340

在开始之前我们需要用到这几个api

  1. https://music.163.com/api/playlist/detail?id=歌单的id // 获取歌单api
  2. https://music.163.com/song/media/outer/url?id=歌曲的id.mp3 // 歌曲链接
  3. https://music.163.com/api/song/media?id=歌曲的id // 歌词

更多的网易云音乐api,可以参考这个文档,它帮我们整理了非常完整的api接口https://binaryify.github.io/NeteaseCloudMusicApi/#/

准备工作完成后,开始进入进入主题:

直接对 https://music.163.com/api/playlist/detail?id=2161739123,进行访问可以看到返回的json内容; 可自行根据自己需要的字段进行组织。我把获取这个api的内容进行了封装

const playlist = async (id) => {
    return Axios.get('https://music.163.com/api/playlist/detail?id=' + id).then(async (res) => {
        let result = res.data.result;
        let tracks = result.tracks;
        let arr = tracks.map((item) => {
            return {
                id: item.id,
                src: `https://music.163.com/song/media/outer/url?id=${item.id}.mp3`, // 此处为歌曲链接
                name: item.name,
                singer: item.artists[0].name,
                time: item.duration // 自行对时间进行格式化处理
            };
        });
        return {
            coverImgUrl: (result.coverImgUrl && result.coverImgUrl.replace('http:', '')) || '',
            tracks: arr,
            name: result.name,
            createTime: result.createTime,
            description: result.description,
        };
    });
};

返回字段详解:

{
  coverImgUrl: 歌单的封面,
  tracks: 歌曲列表,
  name: 歌单的名称,
  createTime: 创建日期,
  description: 描述,
}

接着,对歌词进行获取函数封装

// 获取网易云歌曲歌词
const getLyric = async (id) => {
    return Axios.post(`https://music.163.com/api/song/media?id=` + id, {
        Referer: "https://music.163.com",
        Cookie: "appver=1.5.2",
        "Content-Type": "application/x-www-form-urlencoded",
        "User-Agent": randomUserAgent()
    }).then(_ => _.data.lyric);
};

这里用到了一个模拟随机请求头的函数;如下

function randomUserAgent() {
    const userAgentList = [
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
        "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36",
        "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36",
        "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89;GameHelper",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4",
        "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:46.0) Gecko/20100101 Firefox/46.0",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:46.0) Gecko/20100101 Firefox/46.0",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
        "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
        "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
        "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
        "Mozilla/5.0 (Windows NT 6.3; Win64, x64; Trident/7.0; rv:11.0) like Gecko",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/13.10586",
        "Mozilla/5.0 (iPad; CPU OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1"
    ];
    const num = Math.floor(Math.random() * userAgentList.length);
    return userAgentList[num];
}

最后,生成json文件,将封装好的函数进行调用。

(async () => {
    const sheet = await playlist(2161739123);
    /**
     * 生成歌曲列表
     */
    fs.writeFileSync(path.resolve(__dirname, '../server/data/musicList.json'), JSON.stringify(sheet));
    /**
     * 根据歌曲id生成歌词,返回[{id: lyric}]
     */
    Promise.all(sheet.tracks.map((item, index) => {
        return new Promise((resolve, reject) => {
            setTimeout(async () => {
                try {
                    const lyric = await getLyric(item.id);
                    console.log(`正在处理第${index}条数据的lyric,请耐心等候。。。`);
                    resolve({
                        [item.id]: lyric
                    });
                }
                catch (err) {
                    resolve();
                }
            }, 300 * Math.random() + 50);
        });
    })).then((arr) => {
        let lyric = {}; // 处理歌词,把数组转换为单个对象 eg:[{}] => {}
        arr.map((item) => {
            Object.assign(lyric, item);
        });
        fs.writeFileSync(path.resolve(__dirname, '../server/data/musicLyric.json'), JSON.stringify(lyric));
    });

})();

注意:在上面的函数,其中用到了几个延时函数,用于避免过快的过快访问网易云音乐api接口,导致ip被锁。

--发表评论--

电子邮件地址不会被公开。必填项已用*标注