1
0
Эх сурвалжийг харах

fix: minor error catching & readme stuff

Sv443 2 жил өмнө
parent
commit
6814a25aff
4 өөрчлөгдсөн 47 нэмэгдсэн , 45 устгасан
  1. 15 19
      .editorconfig
  2. 2 2
      README.md
  3. 1 1
      package.json
  4. 29 23
      src/songData.ts

+ 15 - 19
.editorconfig

@@ -1,21 +1,17 @@
-root = true
+# root = true
 
-[*]
-indent_style = space
-tab_width = 4
-indent_size = 4
-charset = utf-8
-trim_trailing_whitespace = false
-insert_final_newline = true
-curly_bracket_next_line = true
-spaces_around_operators = true
+# [*]
+# indent_style = space
+# tab_width = 4
+# indent_size = 4
+# charset = utf-8
+# trim_trailing_whitespace = false
+# insert_final_newline = true
+# curly_bracket_next_line = true
+# spaces_around_operators = true
 
-# 2-space indentation
-[{package.json,package-lock.json,**.yml}]
-indent_style = space
-indent_size = 2
-tab_width = 2
-
-# no final newline
-[{**.json}]
-insert_final_newline = false
+# # 2-space indentation
+# [{package.json,package-lock.json,**.yml}]
+# indent_style = space
+# indent_size = 2
+# tab_width = 2

+ 2 - 2
README.md

@@ -45,7 +45,7 @@ All routes support gzip and deflate compression.
 >
 > **URL Parameters:**  
 > `?q=search%20query`  
-> This parameter should contain both the song and artist name. For best result artist name should come first, separate with a hyphen (`-`)  
+> This parameter should contain both the song and artist name. For best result artist name should come first, separate with a whitespace or hyphen.  
 > Sometimes the song name alone might be enough but the results vary greatly.  
 > Using this parameter instead of `?artist` and `?song` means you will get slightly less accurate results.  
 > Make sure the search query is [percent/URL-encoded.](https://en.wikipedia.org/wiki/Percent-encoding)  
@@ -166,7 +166,7 @@ All routes support gzip and deflate compression.
 >
 > **URL Parameters:**  
 > `?q=search%20query`  
-> This parameter should contain both the song and artist name. For best result artist name should come first, separate with a hyphen (`-`)  
+> This parameter should contain both the song and artist name. For best result artist name should come first, separate with a whitespace or hyphen.  
 > Sometimes the song name alone might be enough but the results vary greatly.  
 > Using this parameter instead of `?artist` and `?song` means you will get slightly less accurate results.  
 > Make sure the search query is [percent/URL-encoded.](https://en.wikipedia.org/wiki/Percent-encoding)  

+ 1 - 1
package.json

@@ -1,7 +1,7 @@
 {
   "name": "geniurl",
   "version": "1.3.0",
-  "description": "Simple JSON and XML REST API to search for song metadata and the lyrics URL on genius.com",
+  "description": "Simple JSON and XML REST API to search for song metadata, the lyrics URL and lyrics translations on genius.com",
   "main": "src/index.ts",
   "scripts": {
     "start": "tsc && node --enable-source-maps out/src/index.js",

+ 29 - 23
src/songData.ts

@@ -136,9 +136,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[] = [];
 
-        // splice out preferredLang results and move them to the beginning of the array
         if(preferLang) {
             hits.forEach((hit, i) => {
                 if(hit.language === preferLang.toLowerCase())
@@ -163,30 +163,36 @@ export async function getMeta({
  * @param param1 URL parameters
  */
 export async function getTranslations(songId: number, { preferLang }: GetTranslationsArgs): Promise<SongTranslation[] | null> {
-    const accessToken = process.env.GENIUS_ACCESS_TOKEN ?? "ERR_NO_ENV";
-
-    const { data: { response: { song } }, status } = await axios.get<ApiSongResult>(`https://api.genius.com/songs/${songId}`, {
-        headers: { "Authorization": `Bearer ${accessToken}` },
-    });
-
-    if(status >= 200 && status < 300 && Array.isArray(song?.translation_songs))
-    {
-        const results = song.translation_songs
-            .map(({ language, id, path, title, url }) => ({ language, title, url, path, id }));
-
-        const preferredResults: SongTranslation[] = [];
-
-        // splice out preferredLang results and move them to the beginning of the array
-        if(preferLang) {
-            results.forEach((res, i) => {
-                if(res.language === preferLang.toLowerCase())
-                    preferredResults.push(results.splice(i, 1)[0]!);
-            });
+    try {
+        const accessToken = process.env.GENIUS_ACCESS_TOKEN ?? "ERR_NO_ENV";
+
+        const { data, status } = await axios.get<ApiSongResult>(`https://api.genius.com/songs/${songId}`, {
+            headers: { "Authorization": `Bearer ${accessToken}` },
+        });
+
+        if(status >= 200 && status < 300 && Array.isArray(data?.response?.song?.translation_songs))
+        {
+            const { response: { song } } = data;
+            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 preferredResults.concat(results);
+        return null;
+    }
+    catch(e) {
+        return null;
     }
-    return null;
 }
 
 /**