Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Exerra committed Sep 20, 2021
1 parent ac9f8ba commit a5313ef
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# node-musickit-api
A wrapper for the Apple Music API written entirely in node
A wrapper for the Apple Music API written in NodeJS
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./src/index");
88 changes: 88 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const jwt = require('jsonwebtoken')
const axios = require('axios')

const rootPath = "https://api.music.apple.com/v1"
let token

class MusicKit {
/**
*
* @param {object} credentials Apple Music credentials. Consists of a key containing MusicKit priviledges, the team ID of developer account and the ID of the key
*/
constructor(credentials) {
if (!credentials || !credentials.key || !credentials.teamId || !credentials.keyId) {
throw new Error("No credentials supplied")
}

token = jwt.sign({}, credentials.key, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: credentials.teamId,
header: {
alg: 'ES256',
kid: credentials.keyId
}
});
}

/**
*
* @param {string} country The storefront. How to fetch is in https://developer.apple.com/documentation/applemusicapi/fetching_resources_by_page
* @param {array} type The type of thing to search. Find about possible types in https://developer.apple.com/documentation/applemusicapi/search_for_catalog_resources
* @param {string} searchquery The search query
* @param {object} cb Callback
*/
search(country, type, searchquery, cb, limit = 1) {
let reqUrl = `${rootPath}/catalog/${country}/search`
let auth = `Bearer ${token}`

console.log(`country: ${country}, searchquery: ${searchquery}, limit: ${limit}`)
console.log(type)

axios({
"method": "GET",
"url": reqUrl,
"params": {
"term": searchquery,
"types": type,
"limit": limit
},
"headers": {
"Authorization": auth
}
}).then(res => cb(null, res.data)).catch(err => cb(err, null))
}


/**
*
* @param {string} country A country to look in
* @param {string} id ID of the song
* @param {function} cb Callback
* @returns Song info
*/
searchSong(country, id, cb) {

let reqUrl = `${rootPath}/catalog/${country}/songs/${id}`
let auth = `Bearer ${token}`
if (cb) {
axios({
"method": "GET",
"url": reqUrl,
"headers": {
"Authorization": auth
}
}).then(res => cb(null, res.data)).catch((err) => cb(err, null))
} else {
axios({
"method": "GET",
"url": reqUrl,
"headers": {
"Authorization": auth
}
}).then(res => { return res.data })
}
}
}

module.exports = MusicKit;

0 comments on commit a5313ef

Please sign in to comment.