genius-lyrics-ts
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "genius-lyrics-ts", | ||
"version": "0.1.0", | ||
"description": "Get lyrics from genius with Typescript, Javascript, or Node", | ||
"version": "0.2.0", | ||
"description": "Get lyrics from google with Typescript, Javascript, or Node", | ||
"main": "dist/index.js", | ||
@@ -11,2 +11,3 @@ "module": "dist/index.mjs", | ||
"lyrics-ts", | ||
"lyrics-js", | ||
"lyrics-searcher", | ||
@@ -17,2 +18,3 @@ "lyrics-finder", | ||
"typescript", | ||
"google", | ||
"lyrics" | ||
@@ -19,0 +21,0 @@ ], |
# genius-lyrics-ts | ||
Get lyrics from genius-api with Typescript, Javascript, or Node | ||
## Got the idea and part of the code from [genius-lyrics-api](https://github.com/farshed/genius-lyrics-api) | ||
### Differences | ||
- 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 |
@@ -1,15 +0,31 @@ | ||
import { Options, checkOptions } from "./utils"; | ||
import extractsLyrics from "./utils/extractsLyrics"; | ||
import searchSong from "./utils/searchSong"; | ||
import { parse } from "node-html-parser"; | ||
import { getTitle } from "./utils"; | ||
/** | ||
* @param {({apiKey: string, title: string, artist: string, optimizeQuery: boolean}|string)} arg - options object, or Genius URL | ||
* @param {string} url - Genius URL | ||
*/ | ||
async function getLyrics(arg: Options) { | ||
export type searchQuery = { | ||
title: string; | ||
artist?: string; | ||
optimizeQuery?: boolean; | ||
}; | ||
async function getLyrics({ title, artist, optimizeQuery }: searchQuery) { | ||
try { | ||
checkOptions(arg); | ||
let result = await searchSong(arg); | ||
if (!result) return null; | ||
let lyrics = await extractsLyrics(result.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 = parse(html); | ||
let lyrics = htmlDocument | ||
.querySelector("[data-lyricid]") | ||
?.innerText?.trim(); | ||
if (!lyrics) return null; | ||
return lyrics; | ||
@@ -16,0 +32,0 @@ } catch (e) { |
@@ -1,22 +0,1 @@ | ||
import { HTMLElement } from "node-html-parser"; | ||
export type Options = { | ||
apiKey: string; | ||
title?: string; | ||
artist?: string; | ||
optimizeQuery?: boolean; | ||
}; | ||
export const isServer = () => | ||
!(typeof window != "undefined" && window.document); | ||
export const checkOptions = (options: 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'; | ||
} | ||
}; | ||
export const getTitle = (title?: string, artist?: string) => { | ||
@@ -36,14 +15,1 @@ const validateTitle = title ?? ""; | ||
}; | ||
export function decodeHTMLEntities(element: HTMLElement, str: string) { | ||
if (str && typeof str === "string") { | ||
// strip script/html tags | ||
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gim, ""); | ||
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gim, ""); | ||
element.innerHTML = str; | ||
str = element.textContent as string; | ||
element.textContent = ""; | ||
} | ||
return str; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12
13402
14
295