UNPKG

node-musixmatch-api

Version:

Advanced API library for seamless Musixmatch integration.

694 lines 31.6 kB
'use strict'; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; exports.__esModule = true; exports.Musixmatch = void 0; if (typeof Promise === 'undefined' || !Promise) { require('es6-promise').Promise; } var axios_1 = __importDefault(require("axios")); var expectations = __importStar(require("./expectations")); var MXMException = expectations.MXMException, MusixmatchError = expectations.MusixmatchError; /** * Represents the Musixmatch API wrapper. */ var Musixmatch = /** @class */ (function () { /** * Constructs a new instance of the Musixmatch class. * @param apiKey - The API Key. */ function Musixmatch(apiKey) { this.baseUrl = 'https://api.musixmatch.com/ws/1.1/'; this.apikey = apiKey; } /** * Sets the API Key. * @param apiKey - The API Key. */ Musixmatch.prototype.setApiKey = function (apiKey) { this.apikey = apiKey; }; /** * Calls the Musixmatch API. * @param method - The HTTP method (get, post, or others). * @param apiMethod - The API method. * @param params - The parameters. * @returns A promise that resolves to the API response. */ Musixmatch.prototype._apiCall = function (method, apiMethod, params) { return __awaiter(this, void 0, void 0, function () { var url, options, response, status_code, hint, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: url = "".concat(this.baseUrl).concat(apiMethod); options = { params: params ? __assign(__assign({}, params), { apikey: this.apikey }) : { apikey: this.apikey } }; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, axios_1["default"].request({ method: method, url: url, params: options.params })]; case 2: response = _a.sent(); status_code = response.data.message.header.status_code; if (status_code === 200) { return [2 /*return*/, response.data]; } else { hint = response.data.message.header.hint || null; throw new MXMException(status_code, "".concat(response.config.url), hint); } return [3 /*break*/, 4]; case 3: error_1 = _a.sent(); if (error_1.response) { throw new MusixmatchError("Status code: ".concat(error_1.response.status, "\nHTTP error occurred: ").concat(error_1.response.data)); } else if (error_1.code === 'ECONNREFUSED') { throw new MusixmatchError('Connection refused'); } else { throw new MusixmatchError(error_1.message); } return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; /** * Get a track info from Musixmatch database: title, artist, isrc(s), instrumental flag. * @param params - The parameters. * @returns A promise that resolves to the track information. * * Parameters: * - **`track_isrc`** - A valid ISRC identifier * - **`commontrack_id`** - The Musixmatch commontrack id */ Musixmatch.prototype.trackGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.get', queryParams)]; }); }); }; /** * Search for track in Musixmatch database. * @param params - The parameters. * @returns A promise that resolves to the search results. * * parameters: * - q_track - The song title * - q_artist - The song artist * - q_lyrics - Any word in the lyrics * - q_track_artist - Any word in the song title or artist name * - q_writer Search among writers * - q Any word in the song title or artist name or lyrics * - f_artist_id When set, filter by this artist id * - f_music_genre_id When set, filter by this music category id * - f_lyrics_language Filter by the lyrics language (en,it,..) * - f_has_lyrics When set, filter only contents with lyrics * - f_track_release_group_first_release_date_min When set, filter the tracks with release date newer than value, format is YYYYMMDD * - f_track_release_group_first_release_date_max When set, filter the tracks with release date older than value, format is YYYYMMDD * - s_artist_rating Sort by our popularity index for artists (asc|desc) * - s_track_rating Sort by our popularity index for tracks (asc|desc) * - quorum_factor Search only a part of the given query string.Allowed range is (0.1 – 0.9) * - page Define the page number for paginated results * - page_size Define the page size for paginated results. Range is 1 to 100. */ Musixmatch.prototype.trackSearch = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.search', queryParams)]; }); }); }; /** * This function provides you the list of the top artists of a given country. * @param params - The parameters * @returns A promise that resolves to the chart artist. * * parameters: * - country - A valid country code (default US) * - page - Define the page number for paginated results * - page_size - Define the page size for paginated results. Range is 1 to 100. * - format - Decide the output type (json or xml) NOTE: JSON has autocomplete. */ Musixmatch.prototype.chartArtistGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'chart.artists.get', queryParams)]; }); }); }; /** * - This api provides you the list of the top songs of a given country. * @param params - The parameters * @returns A promise that resolves to the chart tracks. * * parameters: * countryA valid 2 letters country code (default US). Set XW as worldwide * - pageDefine the page number for paginated results * - page_size - Define the page size for paginated results. Range is 1 to 100. * - chart_name Select among available charts: * top : editorial chart * * hot : Most viewed lyrics in the last 2 hours * * mxmweekly : Most viewed lyrics in the last 7 days * * mxmweekly_new : Most viewed lyrics in the last 7 days limited to new releases only * - f_has_lyrics When set, filter only contents with lyrics */ Musixmatch.prototype.chartTracksGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'chart.tracks.get', queryParams)]; }); }); }; /** * Get the lyrics of a track. * * Make sure the fullfil the country restriction you recieve within every copyrighted content> * @param params - The parameters * @returns A promise that resolves to the track lyrics. * * parameters: * - track_id - The Musixmatch track id * - commontrack_id - The Musixmatch commontrack id */ Musixmatch.prototype.trackLyricsGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.lyrics.get', queryParams)]; }); }); }; /** * Submit a lyrics to Musixmatch database. * Use this api to submit lyrics to Musixmatch database. Musixmatch can only add lyrics if we already have the song meta-data. Musixmatch will validate every submission and in case, make it available through Musixmatch api to Musixmatch customers. The lyrics have to be submitted according to [Musixmatch Guidelines](https://community.musixmatch.com/guidelines?lng=en-US) * @param params - The parameters * @returns - A promise that resolves to the lyrics publish. * * parameters: * - commontrack_id - A valid commontrack_id * - track_isrc - A valid isrc * - lyrics_body - The lyrics */ Musixmatch.prototype.trackLyricsPost = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('post', 'track.lyrics.post', queryParams)]; }); }); }; /** * Get the mood list (and raw value that generated it) of a lyrics * @param params - The parameters * @returns - A promise that resolves to the track mood. * * parameters: * - commontrack_id - The Musixmatch track id * - track_isrc - A valid ISRC identifier */ Musixmatch.prototype.trackLyricsMoodGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.lyrics.mood.get', queryParams)]; }); }); }; /** * Get the snippet for a given track. * * A lyrics snippet is a very short representation of a song lyrics. It’s usually twenty to a hundred characters long and it’s calculated extracting a sequence of words from the lyrics. * @param params - The parameters * @returns - A promise that resolves to the track snippet * * parameters: * - track_id - The musixmatch track id */ Musixmatch.prototype.trackSnippetGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.snippet.get', queryParams)]; }); }); }; /** * Retreive the subtitle of a track. * * Return the subtitle of a track in LRC or DFXP format. * * Refer to Wikipedia LRC format page or DFXP format on W3c for format specifications. * * Make sure the fullfil the country restriction you recieve within every copyrighted content. * @param params - The parameters * @returns A promise that resolves to the track subtitle. * * parameters: * - commontrack_id - The Musixmatch commontrack id * - subtitle_format - The format of the subtitle (lrc,dfxp,stledu). Default to lrc * - f_subtitle_length - The desired length of the subtitle (seconds) * - f_subtitle_length_max_deviation - The maximum deviation allowed from the f_subtitle_length (seconds) */ Musixmatch.prototype.trackSubtitleGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.subtitle.get', queryParams)]; }); }); }; /** * Get the Rich sync for a track * * A rich sync is an enhanced version of the standard sync which allows: * * position offset by single characther * - endless formatting options at single char level * - multiple concurrent voices * - multiple scrolling direction * @param params - The parameters * @returns A promise that resolves to the track rich sync. * * parameters: * - track_id - The musixmatch track id * - f_richsync_length - The desired length of the sync (seconds) * - f_richsync_length_max_deviation - The maximum deviation allowed from the f_sync_length (seconds) */ Musixmatch.prototype.trackRichSyncGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.richsync.get', queryParams)]; }); }); }; /** * Get a translated lyrics for a given language * @param params - The parameters * @returns - A promise that resolves to the Lyrics Translation * * parameters: * - selected_languageThe language of the translated lyrics (ISO 639-1) * - min_completed - Teal from 0 to 1. If present, only the tracks with a translation ratio over this specific value, for a given language, are returned Set it to 1 for completed translation only, to 0.7 for a mimimum of 70% complete translation. * - commontrack_id - The Musixmatch commontrack id * - track_id - The Musixmatch track id * - track_isrc - A valid ISRC identifier * - track_mbid - The musicbrainz recording id */ Musixmatch.prototype.trackLyricsTranslationGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.lyrics.translation.get', queryParams)]; }); }); }; /** * Get a translated subtitle for a given language * @param params - The parameters * @returns - A promise that resolves to the track subtitle translation. * * parameters: * - selected_language - The language of the translated lyrics (ISO 639-1) * - min_completed - Teal from 0 to 1. If present, only the tracks with a translation ratio over this specific value, for a given language, are returned Set it to 1 for completed translation only, to 0.7 for a mimimum of 70% complete translation. * - commontrack_id - The Musixmatch commontrack id * - track_isrc - A valid ISRC identifier * - f_subtitle_length - The desired length of the subtitle (seconds) * - f_subtitle_length_max_deviation - The maximum deviation allowed from the f_subtitle_length (seconds) */ Musixmatch.prototype.trackSubttileTranslationGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'track.subtitle.translation.get', queryParams)]; }); }); }; /** * Get the list of the music genres of our catalogue. * @returns - A promise that resolves to the all music genres. */ Musixmatch.prototype.musicGenresGet = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this._apiCall('get', 'music.genres.get')]; }); }); }; /** * Get the lyrics for track based on title and artist * @param params - The parameters * @returns - A promise that resolves to the matcher lyrics. * * parameters: * - q_track - The song title * - q_artist - The song artist * - track_isrc - If you have an available isrc id in your catalogue you can query using this id only (optional) */ Musixmatch.prototype.matcherLyricsGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'matcher.lyrics.get', queryParams)]; }); }); }; /** * Match your song against Musixmatch database. * * In some cases you already have some informations about the track title, artist name, album etc. * A possible strategy to get the corresponding lyrics could be: * - search our catalogue with a perfect match, * - maybe try using the fuzzy search, * - maybe try again using artist aliases, and so on. * * The matcher.track.get method does all the job for you in a single call. This way you dont’t need to worry about the details, and you’ll get instant benefits for your application without changing a row in your code, while we take care of improving the implementation behind. Cool, uh? * @param params - The parameters * @returns A promise that resolves to the matcher track. * * parameters: * - q_track - The song title * - q_artist - The song artist * - q_albumThe song album */ Musixmatch.prototype.matcherTrackGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'matcher.track.get', queryParams)]; }); }); }; /** * Get the subtitles for a song given his title,artist and duration. * @param params - The parameters * @returns A promise that resolves to the matcher subtitle * * parameters: * - q_track - The song title * - q_artist - The song artist * f_subtitle_length - Filter by subtitle length in seconds * f_subtitle_length_max_deviation - Max deviation for a subtitle length in seconds * track_isrc If you have an available isrc id in your catalogue you can query using this id only (optional) */ Musixmatch.prototype.matcherSubtitleGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'matcher.subtitle.get', queryParams)]; }); }); }; /** * Get the artist data from Musixmatch database. * @param params - The parameters * @returns - A promise that resolves to the artist get * * parameters: * - artist_id - Musixmatch artist id * - artist_mbid - Musicbrainz artist id */ Musixmatch.prototype.artistGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'artist.get', queryParams)]; }); }); }; /** * Search for artists in Musixmatch database. * @param params - The parameters. * @returns A promise that resolves to the artist search. * * parameters: * - q_artist - The song artist * - f_artist_id - When set, filter by this artist id * - f_artist_mbid - When set, filter by this artist musicbrainz id * - page - Define the page number for paginated results * - page_size - Define the page size for paginated results. Range is 1 to 100. * - format - Decide the output type (json or xml) */ Musixmatch.prototype.artistSearch = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'artist.search', queryParams)]; }); }); }; /** * Get the album discography of an artist * @param params - The parameters * @returns A promise that resolves to the artist albums * * parameters: * - artist_id - Musixmatch artist id * - artist_mbid - Musicbrainz artist id * - g_album_name - Group by Album Name * - s_release_date - Sort by release date (asc|desc) * - page - Define the page number for paginated results * - page_size - Define the page size for paginated results. Range is 1 to 100. */ Musixmatch.prototype.artistAlbumsGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'artist.albums.get', queryParams)]; }); }); }; /** * Get a list of artists somehow related to a given one. * @param params - The parameters * @returns A promise that resolves to the related artists. * * parameters: * - artist_id - The Musixmatch artist id * - artist_mbid - The Musicbrainz artist id * - page - Define the page number for paginated results * - page_size - Define the page size for paginated results. Range is 1 to 100. * format - Decide the output type (json or xml) */ Musixmatch.prototype.artistRelatedGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'artist.related.get', queryParams)]; }); }); }; Musixmatch.prototype.albumGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'album.get', queryParams)]; }); }); }; Musixmatch.prototype.albumTracksGet = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { var queryParams; return __generator(this, function (_a) { queryParams = Object.fromEntries(params.map(function (param) { return param.split('='); })); return [2 /*return*/, this._apiCall('get', 'album.tracks.get', queryParams)]; }); }); }; return Musixmatch; }()); exports.Musixmatch = Musixmatch; //# sourceMappingURL=musixmatch.js.map