musixmatch-node-sdk v4.9.8
###MusixmatchSDK
The MusixmatchSDK uses the musixmatch module to access the api. It provides additional features, meta data and additional datasources.
The current version is 1.0.0.
###Module structure In the following the library modules structure.
- lib/
- api/
- api.js
- cache/
- persistent.js
- memory.js
- datasource/
- spotify/
- spotify.js
- track.js
- artist.js
- album.js
- playlist.js
- category.js
- spotify/
- config.js
- mxmapi.js
- mxmbase64.js
- mxmlogger.js
- mxmstringutil.js
mxmutil.js
###Import
To import the module the client module shall import musixmatch-sdk and public interfaces to be used.
// Musixmatch SDK
var MusixmatchSDK = require('../musixmatch-sdk');
var MXMUtil=MusixmatchSDK.Util;
var MXMLogger=MusixmatchSDK.Logger;
var MXMAPI=MusixmatchSDK.API;###Configuration There are several configuration options. Possibile configuration values are showed below:
MXMLogger.setLevel(MXMLogger.LEVEL_INFO);
MXMAPI.init({
logger : MXMLogger,
debug : false,
persistentCache : true,
memCache : false,
cacheFolderPath : './cache/'
});where the options are
{
logger : MXMLogger, // the default logger class
debug : false, // true | false to debug
persistentCache : true, // persistently store api responses
memCache : false, // temporary in-memory storage
cacheFolderPath : './cache/' // persistent storage cache folder
}###Examples
Here several usage examples. More examples in analysis/ folders and tests/ folders.
// Match a Track
var input = {q_track: 'shake it off', q_artist:'taylor swift'};
MXMAPI.GetAPITrack(input,
function(result) {
if(result.track) { // track found
var Track=result.track;
// Spotify Track
MXMAPI.GetAPISpotifyTrack( Track.track_id, function(result) {
MXMLogger.dump(result);
passed(true);
},
function(error) {
passed(false);
});
}
},
function(error) {
passed(false);
});A more complicated example shows how to nest more calls like GetAPITrackChart to retrieve tracks chart and then retrieve subtitles
for them with GetAPIAllSubtitles.
var params = {
type: "mxmweekly",
hasLyrics : true, // true to filter tracks with lyrics
country : "us", // possibly the client country
pageSize : 2, // num of items in a page
maxPages : 1, // each page will have pageSize items
}
MusixmatchAPI.GetAPITrackChart(params,function(result) {
console.log("Retrieved %s tracks", result.length);
var tracks=[],artists=[];
for(var idx in result) {
var t = result[idx];
console.log( t.track.track_name, t.track.artist_name, t.track.track_length );
tracks.push( t );
}
// Retrieve API subtitles
MXMAPI.GetAPIAllSubtitles(tracks,function(all) {
console.log("Retrieved weekly %s subtitles", all.length);
for(var idx in all) {
var subtitlesObj=all[idx];
if(subtitlesObj.subtitles) {
var meta = {
subtitle : subtitlesObj.subtitles
};
}
}
});//MusixmatchSDK.GetAPIAllSubtitles
});//MusixmatchSDK.GetAPITrackChartAnother example shows how to use a different datasource. Here a custom MusixmatchSDK initialization add a spotifyAccessToken
for Spotify data source and debug to active debubbing info. The SpotifyCategory and SpotifyPlaylist objects are used as models
for the data coming from Spotify.GetCategories and Spotify.GetPlaylists apis. The SpotifyPlaylist.getTracks is used to retrieve
spotify/track objects then.
// SDK Configuration
MusixmatchSDK.initialize({
spotifyAccessToken : oauth_refreshtoken_reply['access_token'],
debug : true
});
// spotify objects
var SpotifyCategory = new MusixmatchSDK.SpotifyCategory();
var SpotifyPlaylist = new MusixmatchSDK.SpotifyPlaylist();
// spotify lists
var Spotify = new MusixmatchSDK.Spotify();
// Get New Releases
Spotify.GetNewReleases({},function(results) {
console.log("Spotify New Releases");
var items=results.albums.items;
items.forEach(function(item) {
console.log("Album",item.name,item['album_type']);
});
},
function(error) {
console.log( error );
});
// Get Spotify Categories
Spotify.GetCategories({},
function(results) {
console.log("Spotify Categories");
var items=results.categories.items;
items.forEach(function(item) {
console.log("Category",item.name,item.id );
if(item.id == 'toplists') { // get this category playlists
console.log("Category "+item.name+" playlists");
Spotify.GetPlaylists({'id' : item.id},
function(results) {
var items=results.playlists.items;
var playlistsNorm=[];
items.forEach(function(item) {
console.log("Playlists",item.name,item.id);
var name=item.name.toLowerCase().replace(/ /g,'_');
playlistsNorm.push( { name : name, id : item.id } );
});
console.log(playlistsNorm);
// parse normalized playlists
playlistsNorm.forEach(function(item) {
if(item.name=='top_100_tracks_currently_on_spotify') {
SpotifyPlaylist.getTracks({ 'id' : item.id, 'limit' : 100 },
function(results) {
console.log("Playlist tracks");
var items=results.items;
items.forEach(function(item) {
console.log(item.track.name,item.track.uri);
});
},function(error) {
console.log( error );
});
}
});
},
function(error) {
console.log( error );
});
}
})
},
function(error) {
console.log( error );
});