npm 包 musicbrainz-api 使用教程

前言

在前端开发中,经常会遇到获取音乐数据需要调用 API 的问题。而 MusicBrainz 是一个开放的音乐数据库,提供了丰富的音乐信息查询服务。这里介绍使用 musicbrainz-api npm 包,通过 API 查询音乐数据。

安装和导入

通过 npm 安装 musicbrainz-api

npm install musicbrainz-api

在代码中导入:

const MusicBrainzApi = require('musicbrainz-api');

查询音乐数据

musicbrainz-api 提供了多种查询方式,例如按照 artist,release,recording 等操作进行查询。

通过 artist 查询

const artistName = 'Radiohead';
const params = { inc: 'url-rels', limit: 10 };

const searchParams = { artist: artistName, ...params };

MusicBrainzApi.search('artist', searchParams, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }

  console.log(result);
});

通过 release 查询

const releaseTitle = 'OK Computer';
const params = { inc: 'recordings', limit: 10 };

const searchParams = { release: releaseTitle, ...params };

MusicBrainzApi.search('release', searchParams, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }

  console.log(result);
});

通过 recording 查询

const recordingTitle = 'Paranoid Android';
const params = { inc: 'artists', limit: 10 };

const searchParams = { recording: recordingTitle, ...params };

MusicBrainzApi.search('recording', searchParams, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }

  console.log(result);
});

结果解析

查询结果是一个 JSON 对象,其中包含了丰富的音乐信息。下面介绍一些常用字段的含义。

artist

{
  "artist": {
    "id": "a74b1b7f-71a5-4011-9441-d0b5e4122711",
    "type": "Group",
    "name": "Radiohead",
    "country": "GB",
    "disambiguation": "English rock band",
    "life-span": {
      "begin": "1985-06",
      "end": null
    },
    "score": "100",
    "url-relation-list": [],
    "tags": [
      {
        "count": 62,
        "name": "alternative rock"
      }
    ]
  }
}
  • artist.id: 此 artist 的 MBID (MusicBrainz Identifier)
  • artist.name: 此 artist 的名称
  • artist.country: 此 artist 所属国家
  • artist.score: 此 artist 搜索得分
  • artist.tags: 此 artist 的标签列表,包含 name 字段(标签名称)和 count 字段(此 artist 被打上该标签的次数)

release

{
  "release": {
    "id": "c702b8d2-eb6a-48aa-bb21-b50f8d60e697",
    "title": "OK Computer",
    "status": "Official",
    "release-group": {
      "id": "8fb7e825-b08b-3afb-b31e-014a98e662cd",
      "title": "OK Computer",
      "type": "Album"
    },
    "date": "1997-05-28",
    "country": "GB",
    "disambiguation": "",
    "barcode": "724385479324",
    "asin": "",
    "quality": "high",
    "packaging": "Jewel Case",
    "text-representation": {
      "language": "eng",
      "script": "Latn"
    },
    "score": "100",
    "url-relation-list": [],
    "artist-credit": [
      {
        "artist": {
          "id": "a74b1b7f-71a5-4011-9441-d0b5e4122711",
          "type": "Group",
          "name": "Radiohead",
          "sort-name": "Radiohead",
          "disambiguation": "English rock band"
        }
      }
    ],
    "media": [
      {
        "format": "CD",
        "disc-count": 1,
        "track-count": 12,
        "track-offset": 0
      }
    ],
    "tracks": [
      {
        "recording": {
          "id": "0f0d71f4-4c45-4ff9-930b-6a114dea08d8",
          "title": "Airbag",
          "length": 297306
        },
        "position": "1",
        "number": "1",
        "length": "297306",
        "artist-credit": [
          {
            "artist": {
              "id": "a74b1b7f-71a5-4011-9441-d0b5e4122711",
              "type": "Group",
              "name": "Radiohead",
              "sort-name": "Radiohead",
              "disambiguation": "English rock band"
            }
          }
        ]
      }
    ]
  }
}
  • release.id: 此 release 的 MBID
  • release.title: 此 release 的名称
  • release.release-group.title: 此 release 所属 release-group 的名称
  • release.date: 此 release 的发行日期
  • release.artist-credit: 此 release 的 artist-credit 列表,包含所有参与制作此 release 的 artist 相关信息,例如 id、type、name 和 disambiguation 等等
  • release.tracks: 此 release 的 tracks 列表,包含所有 track 相关信息。每个 track 包含了 recording 相关信息(具体含义见下)

recording

{
  "recording": {
    "id": "b0256dad-9a12-411a-8ff6-2c6fad192303",
    "title": "Paranoid Android",
    "length": "383000",
    "artist-credit": [
      {
        "artist": {
          "id": "a74b1b7f-71a5-4011-9441-d0b5e4122711",
          "type": "Group",
          "name": "Radiohead",
          "sort-name": "Radiohead",
          "disambiguation": "English rock band"
        }
      }
    ],
    "tags": [
      {
        "count": 23,
        "name": "alternative rock"
      }
    ]
  }
}
  • recording.id: 此 recording 的 MBID
  • recording.title: 此 recording 的名称
  • recording.length: 此 recording 的时长,单位是毫秒
  • recording.artist-credit: 此 recording 的 artist-credit 列表,包含所有参与制作此 recording 的 artist 相关信息,例如 id、type、name 和 disambiguation 等等
  • recording.tags: 此 recording 的标签列表,包含 name 字段(标签名称)和 count 字段(此 recording 被打上该标签的次数)

示例代码

const MusicBrainzApi = require('musicbrainz-api');

// artist 查询
const artistName = 'Radiohead';
const params = { inc: 'url-rels', limit: 10 };
const searchParams = { artist: artistName, ...params };
MusicBrainzApi.search('artist', searchParams, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
});

// release 查询
const releaseTitle = 'OK Computer';
const params = { inc: 'recordings', limit: 10 };
const searchParams = { release: releaseTitle, ...params };
MusicBrainzApi.search('release', searchParams, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
});

// recording 查询
const recordingTitle = 'Paranoid Android';
const params = { inc: 'artists', limit: 10 };
const searchParams = { recording: recordingTitle, ...params };
MusicBrainzApi.search('recording', searchParams, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
});

总结

本文介绍了使用 musicbrainz-api npm 包查询音乐数据的方法,包括了多种查询方式和解析返回结果的常用字段。希望通过本文的学习,可以帮助读者更好地了解和应用音乐查询 API。

来源:JavaScript中文网 ,转载请注明来源 本文地址:https://www.javascriptcn.com/post/600673ddfb81d47349e53b6e


纠错反馈