Przeglądaj źródła

fix: broken lyrics cache entry adding & penalization

Sven 1 rok temu
rodzic
commit
2b6e25be40
2 zmienionych plików z 26 dodań i 7 usunięć
  1. 7 3
      src/features/lyrics.ts
  2. 19 4
      src/features/lyricsCache.ts

+ 7 - 3
src/features/lyrics.ts

@@ -4,7 +4,7 @@ import { constructUrlString, error, getResourceUrl, info, log, onSelectorOld, wa
 import { emitInterface } from "../interface";
 import { mode, scriptInfo } from "../constants";
 import { getFeatures } from "../config";
-import { addLyricsCacheEntryPenalized, getLyricsCacheEntry } from "./lyricsCache";
+import { addLyricsCacheEntryBest, addLyricsCacheEntryPenalized, getLyricsCacheEntry } from "./lyricsCache";
 import type { LyricsCacheEntry } from "../types";
 
 /** Ratelimit budget timeframe in seconds - should reflect what's in geniURL's docs */
@@ -281,12 +281,16 @@ export async function fetchLyricsUrls(artist: string, song: string): Promise<Omi
         url,
       }));
 
-    if(!getFeatures().advancedLyricsFilter)
+    if(!getFeatures().advancedLyricsFilter) {
+      const topRes = allResultsSan[0];
+      topRes && addLyricsCacheEntryBest(topRes.meta.artists, topRes.meta.title, topRes.url);
+
       return allResultsSan.map(r => ({
         artist: r.meta.primaryArtist.name,
         song: r.meta.title,
         url: r.url,
       }));
+    }
     
     const exactish = (input: string) => input.toLowerCase()
       .replace(/[\s\-_&,.()[\]]+/gm, "");
@@ -342,7 +346,7 @@ export async function fetchLyricsUrls(artist: string, song: string): Promise<Omi
 
     // add top 3 results to the cache with a penalty to their time to live
     // so every entry is deleted faster if it's not considered as relevant
-    finalResults.slice(1, 3).forEach(({ meta: { artists, title }, url }, i) => {
+    finalResults.slice(0, 3).forEach(({ meta: { artists, title }, url }, i) => {
       const penaltyFraction = hasExactMatch
         // if there's an exact match, give it 0 penalty and penalize all other results with the full value
         ? i === 0 ? 0 : 1

+ 19 - 4
src/features/lyricsCache.ts

@@ -53,16 +53,19 @@ export function getLyricsCacheEntry(artist: string, song: string, refreshEntry =
   return entry;
 }
 
+/** Updates the "last viewed" timestamp of the cache entry for the passed artist and song */
 function updateLyricsCacheEntry(artist: string, song: string) {
   const { cache } = lyricsCacheMgr.getData();
   const idx = cache.findIndex(e => e.artist === artist && e.song === song);
   if(idx !== -1) {
     const newEntry = cache.splice(idx, 1)[0]!;
     newEntry.viewed = Date.now();
+    log("Updating cache entry for", artist, "-", song, "to", newEntry);
     lyricsCacheMgr.setData({ cache: [ newEntry, ...cache ] });
   }
 }
 
+/** Deletes the cache entry for the passed artist and song */
 function deleteLyricsCacheEntry(artist: string, song: string) {
   const { cache } = lyricsCacheMgr.getData();
   const idx = cache.findIndex(e => e.artist === artist && e.song === song);
@@ -90,10 +93,15 @@ export function getLyricsCache() {
 }
 
 /**
- * Adds the provided entry into the lyrics URL cache, synchronously to RAM and asynchronously to GM storage  
+ * Adds the provided "best" (non-penalized) entry into the lyrics URL cache, synchronously to RAM and asynchronously to GM storage  
  * {@linkcode artist} and {@linkcode song} need to be sanitized first!
  */
-export function addLyricsCacheEntry(artist: string, song: string, url: string) {
+export function addLyricsCacheEntryBest(artist: string, song: string, url: string) {
+  // refresh entry if it exists and don't overwrite / duplicate it
+  const cachedEntry = getLyricsCacheEntry(artist, song, true);
+  if(cachedEntry)
+    return;
+
   const { cache } = lyricsCacheMgr.getData();
   const entry = {
     artist, song, url, viewed: Date.now(), added: Date.now(),
@@ -105,8 +113,9 @@ export function addLyricsCacheEntry(artist: string, song: string, url: string) {
   if(cache.length > getFeatures().lyricsCacheMaxSize)
     cache.pop();
 
-  emitInterface("bytm:lyricsCacheEntryAdded", { entry, type: "best" });
+  log("Added cache entry for best result", artist, "-", song, "\n", entry);
 
+  emitInterface("bytm:lyricsCacheEntryAdded", { entry, type: "best" });
   return lyricsCacheMgr.setData({ cache });
 }
 
@@ -118,6 +127,11 @@ export function addLyricsCacheEntry(artist: string, song: string, url: string) {
  * @param penaltyFr Fraction to remove from the timestamp values - has to be between 0 and 1 - default is 0 (no penalty) - (0.25 = only penalized by a quarter of the predefined max penalty)
  */
 export function addLyricsCacheEntryPenalized(artist: string, song: string, url: string, penaltyFr = 0) {
+  // refresh entry if it exists and don't overwrite / duplicate it
+  const cachedEntry = getLyricsCacheEntry(artist, song, true);
+  if(cachedEntry)
+    return;
+
   const { cache } = lyricsCacheMgr.getData();
 
   penaltyFr = clamp(penaltyFr, 0, 1);
@@ -139,7 +153,8 @@ export function addLyricsCacheEntryPenalized(artist: string, song: string, url:
   if(cache.length > getFeatures().lyricsCacheMaxSize)
     cache.pop();
 
-  emitInterface("bytm:lyricsCacheEntryAdded", { entry, type: "penalized" });
+  log("Added penalized cache entry for", artist, "-", song, "with penalty fraction", penaltyFr, "\n", entry);
 
+  emitInterface("bytm:lyricsCacheEntryAdded", { entry, type: "penalized" });
   return lyricsCacheMgr.setData({ cache });
 }