Explorar el Código

feat: add threshold param

Sv443 hace 1 año
padre
commit
ff1d722739
Se han modificado 2 ficheros con 18 adiciones y 7 borrados
  1. 10 2
      src/features/lyrics.ts
  2. 8 5
      src/utils.ts

+ 10 - 2
src/features/lyrics.ts

@@ -1,11 +1,19 @@
 import { triesInterval, triesLimit } from "../constants";
-import { error, getAssetUrl, info, insertAfter, log } from "../utils";
+import { clamp, error, getAssetUrl, info, insertAfter, log } from "../utils";
 import "./lyrics.css";
 
 /** Base URL of geniURL */
 export const geniUrlBase = "https://api.sv443.net/geniurl";
 /** GeniURL endpoint that gives song metadata when provided with a `?q` or `?artist` and `?song` parameter - [more info](https://api.sv443.net/geniurl) */
 const geniURLSearchTopUrl = `${geniUrlBase}/search/top`;
+/**
+ * The threshold to pass to geniURL's fuzzy filtering.  
+ * The lower the number, the more strictly the top results will adhere to the query.  
+ * Set to undefined to use the default.
+ */
+const threshold = 0.55;
+
+const thresholdParam = threshold ? `&threshold=${clamp(threshold, 0, 1)}` : "";
 
 //#MARKER cache
 
@@ -172,7 +180,7 @@ export async function getCurrentLyricsUrl() {
 export async function getGeniusUrl(artist: string, song: string): Promise<string | undefined> {
   try {
     const startTs = Date.now();
-    const fetchUrl = `${geniURLSearchTopUrl}?artist=${encodeURIComponent(artist)}&song=${encodeURIComponent(song)}`;
+    const fetchUrl = `${geniURLSearchTopUrl}?artist=${encodeURIComponent(artist)}&song=${encodeURIComponent(song)}${thresholdParam}`;
 
     log(`Requesting URL from geniURL at '${fetchUrl}'`);
 

+ 8 - 5
src/utils.ts

@@ -14,11 +14,9 @@ export function setLogLevel(level: LogLevel) {
 function getLogLevel(args: unknown[]): number {
   const minLogLvl = 0, maxLogLvl = 1;
   if(typeof args.at(-1) === "number")
-    return Math.max(
-      Math.min(
-        args.splice(args.length - 1)[0] as number,
-        minLogLvl,
-      ),
+    return clamp(
+      args.splice(args.length - 1)[0] as number,
+      minLogLvl,
       maxLogLvl,
     );
   return 1;
@@ -239,3 +237,8 @@ export function autoPlural(word: string, num: number | unknown[]) {
     num = num.length;
   return `${word}${num === 1 ? "" : "s"}`;
 }
+
+/** Ensures the passed `value` always stays between `min` and `max` */
+export function clamp(value: number, min: number, max: number) {
+  return Math.max(Math.min(value, min), max);
+}