Giter VIP home page Giter VIP logo

applemusic.js's Introduction

Node.js Client for Apple Music

The Node.js client for Apple Music is a client library for Apple Music API built with TypeScript.

Currently this library provides only a feature to fetch a single resource such as album, artists, songs, playlists, music videos, or stations.

Note that this does not related to MusicKit JS, which is the official library of Apple Music for web apps.

Requirements

You need to be a member of the Apple Developer Program and obtain your Apple Music developer token with some tools such as apple-music-token-node.

Installation

Install the package with:

yarn add @statsfm/applemusic.js

Type definitions for TypeScript are also included in the package.

Usage

You need to instantiate a client with your developer token:

import { AppleMusicAPI } from '@statsfm/applemusic.js';

const client = new AppleMusicAPI({
  developerToken: 'YOUR_DEVELOPER_TOKEN',
  mediaUserToken: 'YOUR_MUT' // optional
});

Fetching Single Resource

You can fetch album, artists, songs, playlists, music videos, or stations with client.RESOURCES.get():

async function main() {
  const client = new AppleMusicAPI({ developerToken: 'YOUR_DEVELOPER_TOKEN' });

  const playlistID = 'pl.5ee8333dbe944d9f9151e97d92d1ead9';
  const response = await client.playlists.get(playlistID, { storefront: 'us' });
  const playlist = response.data[0];

  console.log(playlist.attributes!);
  // {
  //   artwork: {
  //     width: 4320,
  //     height: 1080,
  //     url: 'https://is1-ssl.mzstatic.com/image/thumb/Features124/v4/f7/25/2e/f7252e6c-921b-6475-7b34-754f3ca0ef1a/source/{w}x{h}cc.jpeg',
  //     bgColor: '0051cc',
  //     textColor1: 'e4fe63',
  //     textColor2: 'f3e8f1',
  //     textColor3: 'b6db78',
  //     textColor4: 'c2c9ea'
  //   },
  //   isChart: false,
  //   url: 'https://music.apple.com/us/playlist/a-list-pop/pl.5ee8333dbe944d9f9151e97d92d1ead9',
  //   lastModifiedDate: 2020-02-14T05:00:26.000Z,
  //   name: 'A-List Pop',
  //   playlistType: 'editorial',
  //   curatorName: 'Apple Music Pop',
  //   playParams: { id: 'pl.5ee8333dbe944d9f9151e97d92d1ead9', kind: 'playlist' },
  //   description: {
  //     standard: "The title track from Sam Smith’s forthcoming third album, “To Die For” was inspired by Abbot Kinney—the iconic beach-adjacent boulevard in Los Angeles’ Venice neighborhood. “I was walking down there on a Sunday and everyone was happy because everyone's happy on that road,” Smith tells Apple Music. “Just partners everywhere, kissing, and families. And it's basically about that—about feeling alone and feeling like you're on the outside watching everyone else together.” Add A-List Pop to your library to stay up on the latest and greatest pop music.",
  //     short: `Sam Smith's new song is inspired by "the most beautiful road in America."`
  //   }
  // }
}

main();

Fetching Many Resources

You can fetch playlists, artists, songs, playlists, music videos, or stations, library playlists and songs with client.RESOURCES.get():

async function main() {
  const client = new AppleMusicAPI({
    developerToken: 'YOUR_DEVELOPER_TOKEN',
    mediaUserToken: 'YOUR_MUT'
  });

  const response = await client.library.playlists.getMany({ storefront: 'us' });
  const playlists = response.data;

  console.log(playlists);
  // [
  //   {
  //     id: 'p.zp6KlXEiBvxMR8g',
  //     type: 'library-playlists',
  //     href: '/v1/me/library/playlists/p.zp6KlXOiBvdMR8g',
  //     attributes: {
  //       canEdit: false,
  //       name: 'My playlist',
  //       description: [Object],
  //       isPublic: false,
  //       artwork: [Artwork],
  //       hasCatalog: true,
  //       playParams: [PlayParameters],
  //       dateAdded: 2022-08-19T08:22:08.000Z
  //     }
  //   }
  // ]
}

main();

Handling Errors

You can handle API errors as:

async function main() {
  const client = new AppleMusicAPI({ developerToken: 'YOUR_DEVELOPER_TOKEN' });

  try {
    const response = await client.playlists.get('nosuchplaylist', { storefront: 'us' });
  } catch (error) {
    console.log(error.httpStatusCode);
    // 404

    console.log(error.response.errors[0])
    // {
    //   id: 'LVZEECBHZRR4HII432DE7VDF6E',
    //   title: 'Resource Not Found',
    //   detail: 'Resource with requested id was not found',
    //   status: '404',
    //   code: '40400'
    // }
    }
  }
}

main();

Specifying Storefront

You must specify storefront either:

  • defaultStorefront parameter in the constructor
  • storefront parameter in get()
const client = new AppleMusicAPI({
  developerToken: 'YOUR_DEVELOPER_TOKEN',
  defaultStorefront: 'us'
});

const response = await client.playlists.get(playlistID);
const client = new AppleMusicAPI({
  developerToken: 'YOUR_DEVELOPER_TOKEN'
});

const response = await client.playlists.get(playlistID, { storefront: 'us' });

Specifying Language Tag

You can specify language tag either:

  • Unspecify for the storefront's default language
  • defaultLanguageTag parameter in the constructor
  • languageTag parameter in get()
const client = new AppleMusicAPI({
  developerToken: 'YOUR_DEVELOPER_TOKEN',
  defaultStorefront: 'jp'
});

// The storefront's default
const response = await client.playlists.get(playlistID);
const client = new AppleMusicAPI({
  developerToken: 'YOUR_DEVELOPER_TOKEN',
  defaultStorefront: 'jp',
  defaultLanguageTag: 'en-US'
});

const response = await client.playlists.get(playlistID);
const client = new AppleMusicAPI({
  developerToken: 'YOUR_DEVELOPER_TOKEN',
  defaultStorefront: 'jp'
});

const response = await client.playlists.get(playlistID, { languageTag: 'en-US' });

License

Copyright (c) 2022 StatsFM B.V.

See the LICENSE.txt for details.

applemusic.js's People

Contributors

netlob avatar stijnvdkolk avatar veaceslav131 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.