genius-lyrics-ts
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -1,13 +0,8 @@ | ||
type Options = { | ||
apiKey: string; | ||
title?: string; | ||
type searchQuery = { | ||
title: string; | ||
artist?: string; | ||
optimizeQuery?: boolean; | ||
}; | ||
declare function getLyrics({ title, artist, optimizeQuery }: searchQuery): Promise<string | null>; | ||
/** | ||
* @param {({apiKey: string, title: string, artist: string, optimizeQuery: boolean}|string)} arg - options object, or Genius URL | ||
*/ | ||
declare function getLyrics(arg: Options): Promise<string | null>; | ||
export { getLyrics as default }; |
@@ -27,12 +27,6 @@ "use strict"; | ||
// src/getLyrics.ts | ||
var import_node_html_parser = require("node-html-parser"); | ||
// src/utils/index.ts | ||
var checkOptions = (options) => { | ||
let { apiKey, title, artist } = options; | ||
if (!apiKey) { | ||
throw '"apiKey" property is missing from options'; | ||
} | ||
if (!title && !artist) { | ||
throw '"title" and "artist" property is missing from options'; | ||
} | ||
}; | ||
var getTitle = (title, artist) => { | ||
@@ -46,74 +40,18 @@ const validateTitle = title ?? ""; | ||
}; | ||
function decodeHTMLEntities(element, str) { | ||
if (str && typeof str === "string") { | ||
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gim, ""); | ||
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gim, ""); | ||
element.innerHTML = str; | ||
str = element.textContent; | ||
element.textContent = ""; | ||
} | ||
return str; | ||
} | ||
// src/utils/extractsLyrics.ts | ||
var import_node_html_parser = require("node-html-parser"); | ||
async function extractsLyrics(url) { | ||
// src/getLyrics.ts | ||
async function getLyrics({ title, artist, optimizeQuery }) { | ||
try { | ||
const response = await fetch(url); | ||
const searchUrl = "https://www.google.com/search?q="; | ||
!artist ? artist = "" : null; | ||
const song = optimizeQuery ? getTitle(title, artist) : `${title} ${artist}`; | ||
const reqUrl = `${searchUrl}${encodeURIComponent( | ||
(song + " lyrics").trim() | ||
)}`; | ||
const response = await fetch(reqUrl); | ||
const html = await response.text(); | ||
const htmlDocument = (0, import_node_html_parser.parse)(html); | ||
let lyrics = htmlDocument.querySelector('div[class="lyrics"]')?.textContent?.trim(); | ||
if (!lyrics) { | ||
lyrics = ""; | ||
htmlDocument.querySelectorAll('div[class^="Lyrics__Container"]').forEach((elem) => { | ||
if (elem && elem.textContent?.length !== 0) { | ||
let snippet = elem.innerHTML?.replace(/<br>/g, "\n").replace(/<(?!\s*br\s*\/?)[^>]+>/gi, ""); | ||
snippet = decodeHTMLEntities(elem, snippet); | ||
lyrics += snippet; | ||
htmlDocument.querySelector("textarea")?.textContent + "\n\n"; | ||
} | ||
}); | ||
} | ||
let lyrics = htmlDocument.querySelector("[data-lyricid]")?.innerText?.trim(); | ||
if (!lyrics) | ||
return null; | ||
return lyrics.trim(); | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
var extractsLyrics_default = extractsLyrics; | ||
// src/utils/searchSong.ts | ||
var searchUrl = "https://api.genius.com/search?q="; | ||
async function searchSong(options) { | ||
try { | ||
checkOptions(options); | ||
let { apiKey, title, artist, optimizeQuery = true } = options; | ||
if (!title && !artist) | ||
return null; | ||
const song = optimizeQuery ? getTitle(title, artist) : `${title} ${artist}`; | ||
if (!song) | ||
return null; | ||
const reqUrl = `${searchUrl}${encodeURIComponent(song)}`; | ||
const data = await (await fetch(`${reqUrl}&access_token=${apiKey}`)).json(); | ||
const hits = data.response.hits; | ||
if (hits.length === 0) | ||
return null; | ||
const result = hits[0].result; | ||
const { full_title, song_art_image_url, id, url } = result; | ||
return { id, title: full_title, albumArt: song_art_image_url, url }; | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
var searchSong_default = searchSong; | ||
// src/getLyrics.ts | ||
async function getLyrics(arg) { | ||
try { | ||
checkOptions(arg); | ||
let result = await searchSong_default(arg); | ||
if (!result) | ||
return null; | ||
let lyrics = await extractsLyrics_default(result.url); | ||
return lyrics; | ||
@@ -120,0 +58,0 @@ } catch (e) { |
{ | ||
"name": "genius-lyrics-ts", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Get lyrics from google with Typescript, Javascript, or Node", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
# genius-lyrics-ts | ||
Get lyrics from genius-api with Typescript, Javascript, or Node | ||
Get lyrics from google with Typescript, Javascript, or Node | ||
## Got the idea and part of the code from [genius-lyrics-api](https://github.com/farshed/genius-lyrics-api) | ||
@@ -9,4 +10,4 @@ | ||
- Has a single method, `getLyrics` | ||
- Wrriten in typescript | ||
- Uses node-parsr for better performance intead of cheerio | ||
- Edge runtime compatible because it uses the native fetch | ||
- Written in typescript | ||
- Uses node-parser for better performance instead of cheerio | ||
- Edge runtime compatible |
import { parse } from "node-html-parser"; | ||
import { getTitle } from "./utils"; | ||
/** | ||
* @param {string} url - Genius URL | ||
*/ | ||
export type searchQuery = { | ||
@@ -9,0 +5,0 @@ title: string; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13
2
9202
166