Tianhe Gao

导出网易云歌单,生成网页

完成的结果:我的歌单

  1. 获取 likelist.json
  2. 生成 content/music.org

一、获取 likelist.json

使用 GitHub 仓库 Binaryify/NeteaseCloudMusicApi 提供的 API——获取歌单所有歌曲:

接口地址: /playlist/track/all

调用例子: /playlist/track/all?id=24381616

id 后的数字是歌单的 id。在浏览器输入 https://music.163.com ,登陆后进入“个人主页”,在“我创建的歌单”下会有“XX喜欢的音乐”,点击后地址栏是这样的: https://music.163.com/#/playlist?id=967686417 。最后的这串数字,就是上面调用例子中需要的数字。

按照文档部署运行在本地后,要先二维码登录,然后才能获取到列表;否则,会提示操作过于频繁之类的话语。我本想部署在在线平台——Vercel 或者腾讯云 Serverless。最后都不能像在本地一样正常工作。http://localhost:3000/playlist/track/all?id=967686417 是我要请求的本地地址,一切正常的话,页面会出现类 JSON 数据,保存到本地就是 likelist.json。

我打算,每隔一段时间,手动获取一次列表。

二、生成 content/music.org

我通过 phind(为开发者服务的 AI 搜索引擎),知道可以用 ejs 这样的模板语言,将类 JSON 数据转为 HTML 网页。

实现代码:

 1/// music.js
 2const fs = require('fs')
 3const ejs = require('ejs')
 4
 5// http://localhost:3000/playlist/track/all?id=967686417
 6// 获取我喜欢歌单全部歌曲详情
 7let songs = []
 8fs.readFile('./scripts/likelist.json', 'utf8', (err, data) => {
 9  if (err) console.log(err)
10  const jsonData = JSON.parse(data)
11  songs = jsonData.songs.map(song => {
12    if (song.ar.length == 2) {
13      return {
14        name: song.name,
15        artists: `${song.ar[0].name}, ${song.ar[1].name}`
16      }
17    }
18    if (song.ar.length == 3) {
19      return {
20        name: song.name,
21        artists: `${song.ar[0].name}, ${song.ar[1].name}, ${song.ar[2].name}`
22      }
23    }
24    return {
25      name: song.name,
26      artists: song.ar[0].name
27    }
28  })
29  // console.log(songs)
30  ejs.renderFile('./scripts/template.ejs', { songs }, { "rmWhitespace": true }, function (err, org) {
31    if (err) throw err;
32    fs.writeFile('./content/music.org', org, function (err) {
33      if (err) throw err;
34      console.log('Org-mode file generated!');
35    });
36  });
37})
1/// template.ejs
2#+TITLE: 我的歌单
3
4<% for(let i=0; i<songs.length; i++) { %>
5- <%= songs[i].name %> - <%= songs[i].artists -%>
6<% } %>

No notes link to this note

Welcome to tell me your thoughts via "email"
UP