Bladeren bron

fix!: remove ?preferLang param

Sven 1 jaar geleden
bovenliggende
commit
1fd9a925a6
5 gewijzigde bestanden met toevoegingen van 9 en 62 verwijderingen
  1. 0 15
      README.md
  2. 2 7
      src/routes/search.ts
  3. 2 5
      src/routes/translations.ts
  4. 5 29
      src/songData.ts
  5. 0 6
      src/types.ts

+ 0 - 15
README.md

@@ -64,11 +64,6 @@ These are the available routes:
 > <br>
 > 
 > **Optional URL Parameters:**  
-> `?preferLang=en`  
-> Sometimes the genius API and geniURL's filtering will rank song translations or remixes higher than the original song. This optional parameter can help with that.  
-> Specify a [two-character ISO 639-1 language code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) here to tell geniURL to prefer results of that language.  
-> Note that this only changes the ranking. Filtering will stay the same and results of other languages will still be returned.
->   
 > `?format=json/xml`  
 > Use this optional parameter to change the response format from the default (`json`) to `xml`  
 > The structure of the XML data is similar to the shown JSON data.
@@ -182,11 +177,6 @@ These are the available routes:
 > <br><br>
 > 
 > **Optional URL Parameters:**  
-> `?preferLang=en`  
-> Sometimes the genius API and geniURL's filtering will rank song translations or remixes higher than the original song. This optional parameter can help with that.  
-> Specify a [two-character ISO 639-1 language code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) here to tell geniURL to prefer results of that language.  
-> Note that this only changes the ranking. Filtering will stay the same and results of other languages will still be returned.
->   
 > `?format=json/xml`  
 > Use this optional parameter to change the response format from the default (`json`) to `xml`  
 > The structure of the XML data is similar to the shown JSON data.
@@ -279,11 +269,6 @@ These are the available routes:
 > <br>
 >
 > **Optional URL Parameters:**  
-> `?preferLang=en`  
-> The filtering done by the genius API and geniURL will sometimes produce results that are ranked inconsistently. This optional parameter can help with that.  
-> Specify a [two-character ISO 639-1 language code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) here to tell geniURL to prefer results of that language.  
-> Note that this only changes the ranking. Filtering will stay the same and results of other languages will still be returned.
->   
 > `?format=json/xml`  
 > Use this optional parameter to change the response format from the default (`json`) to `xml`  
 > The structure of the XML data is similar to the shown JSON data.

+ 2 - 7
src/routes/search.ts

@@ -1,16 +1,14 @@
 import { Router } from "express";
 import { paramValid, respond } from "../utils";
 import { getMeta } from "../songData";
-import { langCodes } from "../constants";
 
 export function initSearchRoutes(router: Router) {
   router.get("/search", async (req, res) => {
     try {
-      const { q, artist, song, format: fmt, threshold: thr, preferLang: prLang } = req.query;
+      const { q, artist, song, format: fmt, threshold: thr } = req.query;
 
       const format: string = fmt ? String(fmt) : "json";
       const threshold = isNaN(Number(thr)) ? undefined : Number(thr);
-      const preferLang = paramValid(prLang) && langCodes.has(prLang.toLowerCase()) ? prLang.toLowerCase() : undefined;
 
       if(paramValid(q) || (paramValid(artist) && paramValid(song))) {
         const meta = await getMeta({
@@ -21,7 +19,6 @@ export function initSearchRoutes(router: Router) {
             song: String(song),
           }),
           threshold,
-          preferLang,
         });
 
         if(!meta || meta.all.length < 1)
@@ -42,11 +39,10 @@ export function initSearchRoutes(router: Router) {
 
   router.get("/search/top", async (req, res) => {
     try {
-      const { q, artist, song, format: fmt, threshold: thr, preferLang: prLang } = req.query;
+      const { q, artist, song, format: fmt, threshold: thr } = req.query;
 
       const format: string = fmt ? String(fmt) : "json";
       const threshold = isNaN(Number(thr)) ? undefined : Number(thr);
-      const preferLang = paramValid(prLang) && langCodes.has(prLang.toLowerCase()) ? prLang.toLowerCase() : undefined;
 
       if(paramValid(q) || (paramValid(artist) && paramValid(song))) {
         const meta = await getMeta({
@@ -57,7 +53,6 @@ export function initSearchRoutes(router: Router) {
             song: String(song),
           }),
           threshold,
-          preferLang,
         });
 
         if(!meta || !meta.top)

+ 2 - 5
src/routes/translations.ts

@@ -1,7 +1,6 @@
 import { Router } from "express";
 import { paramValid, respond } from "../utils";
 import { getTranslations } from "../songData";
-import { langCodes } from "../constants";
 
 export function initTranslationsRoutes(router: Router) {
   router.get("/translations", (req, res) => {
@@ -11,18 +10,16 @@ export function initTranslationsRoutes(router: Router) {
   });
 
   router.get("/translations/:songId", async (req, res) => {
-    const { format: fmt, preferLang: prLang } = req.query;
+    const { format: fmt } = req.query;
     const format: string = fmt ? String(fmt) : "json";
 
     try {
       const { songId } = req.params;
 
-      const preferLang = paramValid(prLang) && langCodes.has(prLang.toLowerCase()) ? prLang.toLowerCase() : undefined;
-
       if(!paramValid(songId) || isNaN(Number(songId)))
         return respond(res, "clientError", "Provided song ID is invalid", format);
 
-      const translations = await getTranslations(Number(songId), { preferLang });
+      const translations = await getTranslations(Number(songId));
 
       if(translations === null || (Array.isArray(translations) && translations.length === 0))
         return respond(res, "clientError", "Couldn't find translations for this song", format, 0);

+ 5 - 29
src/songData.ts

@@ -6,7 +6,7 @@ import { clamp } from "svcorelib";
 
 import { axios, baseAxiosOpts } from "./axios";
 import { charReplacements } from "./constants";
-import type { Album, ApiSearchResult, ApiSongResult, GetMetaArgs, GetMetaResult, GetTranslationsArgs, MetaSearchHit, SongMeta, SongTranslation } from "./types";
+import type { Album, ApiSearchResult, ApiSongResult, GetMetaArgs, GetMetaResult, MetaSearchHit, SongMeta, SongTranslation } from "./types";
 
 const defaultFuzzyThreshold = 0.65;
 
@@ -19,7 +19,6 @@ export async function getMeta({
   artist,
   song,
   threshold,
-  preferLang,
 }: GetMetaArgs): Promise<GetMetaResult | null>
 {
   const query = q ? q : `${artist} ${song}`;
@@ -46,7 +45,6 @@ export async function getMeta({
       .map(({ result }) => ({
         url: result.url,
         path: result.path,
-        language: result.language ?? null,
         meta: {
           title: normalize(result.title),
           fullTitle: normalize(result.full_title),
@@ -142,21 +140,9 @@ export async function getMeta({
     if(hits.length === 0)
       return null;
 
-    // splice out preferredLang results and move them to the beginning of the array, while keeping their original order:
-    const preferredBestMatches: MetaSearchHit[] = [];
-
-    if(preferLang) {
-      hits.forEach((hit, i) => {
-        if(hit.language === preferLang.toLowerCase())
-          preferredBestMatches.push(hits.splice(i, 1)[0]!);
-      });
-    }
-
-    const reorderedHits = preferredBestMatches.concat(hits);
-
     return {
-      top: reorderedHits[0]!,
-      all: reorderedHits.slice(0, 10),
+      top: hits[0]!,
+      all: hits.slice(0, 10),
     };
   }
 
@@ -169,7 +155,7 @@ export async function getMeta({
  * @param param1 URL parameters
  * @returns An array of translation objects, null if the song doesn't have any or undefined if an error occurred (like the song doesn't exist)
  */
-export async function getTranslations(songId: number, { preferLang }: GetTranslationsArgs): Promise<SongTranslation[] | null | undefined> {
+export async function getTranslations(songId: number): Promise<SongTranslation[] | null | undefined> {
   try {
     const { data, status } = await axios.get<ApiSongResult>(
       `https://api.genius.com/songs/${songId}`,
@@ -184,17 +170,7 @@ export async function getTranslations(songId: number, { preferLang }: GetTransla
       const results = song.translation_songs
         .map(({ language, id, path, title, url }) => ({ language, title, url, path, id }));
 
-      // splice out preferredLang results and move them to the beginning of the array, while keeping their original order:
-      const preferredResults: SongTranslation[] = [];
-
-      if(preferLang) {
-        results.forEach((res, i) => {
-          if(res.language === preferLang.toLowerCase())
-            preferredResults.push(results.splice(i, 1)[0]!);
-        });
-      }
-
-      return preferredResults.concat(results);
+      return results;
     }
     return null;
   }

+ 0 - 6
src/types.ts

@@ -26,7 +26,6 @@ interface Artist {
 export interface SongMeta {
   url: string;
   path: string;
-  language: string | null;
   meta: {
     title: string;
     fullTitle: string;
@@ -54,7 +53,6 @@ export interface GetMetaArgs {
   artist?: string;
   song?: string;
   threshold?: number;
-  preferLang?: string;
 }
 
 export type SearchFilterArgs = GetMetaArgs;
@@ -79,10 +77,6 @@ export interface SongTranslation {
   url: string;
 }
 
-export interface GetTranslationsArgs {
-  preferLang?: string;
-}
-
 //#SECTION server
 
 export type ResponseType = "serverError" | "clientError" | "success";