genius.ts
Version:
Genius API Wrapper for Node.js
91 lines (90 loc) • 3.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GeniusClient = void 0;
const axios_1 = require("axios");
const Track_1 = require("./Track");
class GeniusClient {
token;
baseUrl = 'https://api.genius.com';
constructor(ClientOption) {
if (!ClientOption.accessToken)
throw new Error("AccessToken must be provided");
this.token = ClientOption.accessToken;
this.baseUrl = ClientOption.baseSearchURL || this.baseUrl;
}
/**
* Method to search a track in Genius API
* @param searchTrackOptions
* @returns If a result is found it returns all the search hits, else it returns nothing
*/
async searchTrack(searchTrackOptions) {
const { title, artist, optimizeQuery, authHeader } = searchTrackOptions;
const song = optimizeQuery ? getTitle(title, artist) : artist ? `${title} ${artist}` : title;
const reqUrl = `${this.baseUrl}/search?q=${encodeURIComponent(song)}`;
const headers = {
Authorization: 'Bearer ' + this.token
};
let { data } = await axios_1.default.get(authHeader ? reqUrl : `${reqUrl}&access_token=${this.token}`, authHeader ? { headers } : undefined);
if (data.response.hits.length === 0)
return null;
const results = data.response.hits.map((val) => {
const { full_title, song_art_image_url, id, url } = val.result;
return { id, title: full_title, albumArt: song_art_image_url, url };
});
return results;
}
/**
* Method to get a Song by it's name
* @returns returns best hit song class
* @throws an excpetion when the song is not found
*/
async getSong(searchTrackOptions) {
const { title, artist, optimizeQuery, authHeader } = searchTrackOptions;
const song = optimizeQuery ? getTitle(title, artist) : (artist && artist.length > 0 ? `${title} ${artist}` : title);
const reqUrl = `${this.baseUrl}/search?q=${encodeURIComponent(song)}`;
const headers = {
Authorization: 'Bearer ' + this.token
};
let { data } = await axios_1.default.get(authHeader ? reqUrl : `${reqUrl}&access_token=${this.token}`, authHeader ? { headers } : undefined);
if (data.response.hits.length === 0 && !optimizeQuery)
throw new Error("Song not found, ensure that you entered correct song name or enable query optimization option");
else if (data.response.hits.length === 0)
return null;
const result = data.response.hits[0];
return new Track_1.Track(result.result);
}
/**
* Method to get the song by it's ID
* @returns the song class
* @throws an expecption if the provided song ID is not valid
*/
async getSongByID(id) {
const reqUrl = `${this.baseUrl}/songs/${id}`;
const headers = {
Authorization: 'Bearer ' + this.token
};
let { data } = await axios_1.default.get(reqUrl, { headers });
if (!data.response)
throw new Error("Song not found, ensure that you entered a valid song ID");
return new Track_1.Track(data.response.song);
}
}
exports.GeniusClient = GeniusClient;
const getTitle = (title, artist) => {
return artist && artist.length > 0 ?
(artist ? `${title} ${artist}` : title)
.toLowerCase()
.replace(/ *\([^)]*\) */g, '')
.replace(/ *\[[^\]]*]/, '')
.replace(/feat.|ft./g, '')
.replace(/\s+/g, ' ')
.trim()
:
`${title}`
.toLowerCase()
.replace(/ *\([^)]*\) */g, '')
.replace(/ *\[[^\]]*]/, '')
.replace(/feat.|ft./g, '')
.replace(/\s+/g, ' ')
.trim();
};