UNPKG

genius.ts

Version:

Genius API Wrapper for Node.js

80 lines (79 loc) 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Track = void 0; const axios_1 = require("axios"); const {load} = require("cheerio"); class Track { id; fullTitle; title; url; trackArtwork; //private albumArtwork: string; artistNames; lyricsState; instrumental; path; pyongsCount; updatedByHumanAt; stats; primaryArtist; constructor(trackProps) { this.id = trackProps.id; this.fullTitle = trackProps.full_title; this.title = trackProps.title; this.url = trackProps.url; this.trackArtwork = trackProps.song_art_image_url || "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTu2fofi1u2qWyjhg0X4n0xI18pmHW1SSvbDGzGWtdFTGN8plymSIvKeb7Uud82LFhwxpM&usqp=CAU"; this.artistNames = trackProps.artist_names; this.lyricsState = trackProps.lyrics_state; this.instrumental = trackProps.instrumental || false; this.path = trackProps.path; this.pyongsCount = trackProps.pyongs_count; this.updatedByHumanAt = trackProps.updated_by_human_at; this.stats = { unreviewedAnnotations: trackProps.stats.unreviewed_annotations, hot: trackProps.stats.hot, pageviews: trackProps.stats.pageviews, }; this.primaryArtist = { id: trackProps.primary_artist.id, name: trackProps.primary_artist.name, url: trackProps.primary_artist.url, iq: trackProps.primary_artist.iq, }; } trackArtworkURL() { return this.trackArtwork; } /*public albumArtworkURL() { return this.albumArtwork; }*/ async getLyrics() { return await extractLyrics(this.url); } } exports.Track = Track; async function extractLyrics(url) { try { let { data } = await axios_1.default.get(url); const $ = load(data); let lyrics = $('div[class="lyrics"]').text().trim(); if (!lyrics) { lyrics = ''; $('div[data-lyrics-container="true"]').each((i, elem) => { if (elem !== null && $(elem).text().length !== 0) { let snippet = $(elem).html(); if (snippet) { snippet = snippet.replace(/<br>/g, '\n').replace(/<(?!\s*br\s*\/?)[^>]+>/gi, ''); } lyrics += $('<textarea/>').html(snippet || "No Lyrics found").text().trim() + '\n\n'; } }); } return !lyrics ? "No Lyrics found" : lyrics.trim(); } catch (e) { throw e; } } ;