pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Cleanup translation console logs

vlOd2 9ad28631 edb9cad8

+46 -51
+3 -4
src/stores/player/slices/source.ts
··· 446 446 447 447 clearTranslateTask() { 448 448 set((s) => { 449 - console.log("Clearing translate task"); 450 449 if (s.caption.translateTask) { 451 - console.log("Cancelling ongoing translate task"); 452 450 s.caption.translateTask.cancel(); 453 451 } 454 452 s.caption.translateTask = null; ··· 475 473 done: false, 476 474 error: false, 477 475 cancel() { 478 - console.log("Translation task cancelled by user"); 476 + if (!this.done || !this.error) { 477 + console.log("Translation task was cancelled"); 478 + } 479 479 cancelled = true; 480 480 }, 481 481 }; ··· 535 535 }; 536 536 s.caption.translateTask.done = true; 537 537 s.caption.translateTask.translatedCaption = translatedCaption; 538 - console.log("Caption translation completed", s.caption.translateTask); 539 538 }); 540 539 } catch (err) { 541 540 handleError(err);
+43 -47
src/stores/player/utils/captionstranslation.ts
··· 2 2 import { Caption, ContentCaption } from "subsrt-ts/dist/types/handler"; 3 3 4 4 const API_URL = 5 - "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&oe=UTF-8&sl=auto&tl={TARGET_LANG}&q="; 5 + "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&oe=UTF-8&sl=auto"; 6 6 const RETRY_COUNT = 3; 7 7 const FETCH_RATE = 100; 8 8 const SUBTITLES_CACHE: Map<string, ArrayBuffer> = new Map< ··· 29 29 }); 30 30 } 31 31 32 + function tryUseCachedCaption( 33 + caption: ContentCaption, 34 + cache: Map<string, string>, 35 + ): boolean { 36 + const text: string | undefined = cache.get(caption.text); 37 + if (text) { 38 + caption.text = text; 39 + return true; 40 + } 41 + return false; 42 + } 43 + 32 44 async function translateText( 33 45 text: string, 34 46 targetLang: string, ··· 38 50 } 39 51 40 52 const response = await ( 41 - await fetch( 42 - `${API_URL.replace("{TARGET_LANG}", targetLang)}${encodeURIComponent(text)}`, 43 - { 44 - method: "GET", 45 - headers: { 46 - Accept: "application/json", 47 - }, 53 + await fetch(`${API_URL}&tl=${targetLang}&q=${encodeURIComponent(text)}`, { 54 + method: "GET", 55 + headers: { 56 + Accept: "application/json", 48 57 }, 49 - ) 58 + }) 50 59 ).json(); 51 60 52 61 if (!response) { ··· 75 84 break; 76 85 } 77 86 } catch (error) { 78 - console.warn("[CTR] Re-trying caption translation", caption, error); 87 + console.warn("Re-trying caption translation", caption, error); 79 88 } 80 89 } 81 90 if (!text) { 82 - console.error("[CTR] Failed to translate caption"); 91 + console.error("Failed to translate caption"); 83 92 caption.text = `(CAPTION COULD NOT BE TRANSLATED)\n${caption.text}}`; 84 93 return false; 85 94 } ··· 91 100 captions: ContentCaption[], 92 101 targetLang: string, 93 102 ): Promise<boolean> { 94 - console.log("[CTR] Translating", captions.length, "captions"); 103 + // console.log("Translating", captions.length, "captions"); 95 104 try { 96 105 const results: boolean[] = await Promise.all( 97 106 captions.map((c) => translateCaption(c, targetLang)), ··· 101 110 const failedCount = results.length - successCount; 102 111 const successPercentange = (successCount / results.length) * 100; 103 112 const failedPercentange = (failedCount / results.length) * 100; 104 - console.log( 105 - "[CTR] Done translating captions", 106 - results.length, 107 - successCount, 108 - failedCount, 109 - successPercentange, 110 - failedPercentange, 111 - ); 113 + // console.log( 114 + // "Done translating captions", 115 + // results.length, 116 + // successCount, 117 + // failedCount, 118 + // successPercentange, 119 + // failedPercentange, 120 + // ); 112 121 113 122 if (failedPercentange > successPercentange) { 114 123 throw new Error("Success percentage is not acceptable"); 115 124 } 116 125 } catch (error) { 117 - console.error( 118 - "[CTR] Could not translate", 119 - captions.length, 120 - "captions", 121 - error, 122 - ); 126 + console.error("Could not translate", captions.length, "captions", error); 123 127 return false; 124 128 } 125 129 return true; 126 - } 127 - 128 - function tryUseCached( 129 - caption: ContentCaption, 130 - cache: Map<string, string>, 131 - ): boolean { 132 - const text: string | undefined = cache.get(caption.text); 133 - if (text) { 134 - caption.text = text; 135 - return true; 136 - } 137 - return false; 138 130 } 139 131 140 132 async function translateSRTData( ··· 145 137 try { 146 138 captions = subsrt.parse(data); 147 139 } catch (error) { 148 - console.error("[CTR] Failed to parse subtitle data", error); 140 + console.error("Failed to parse subtitle data", error); 149 141 return undefined; 150 142 } 151 143 ··· 166 158 } 167 159 168 160 for (let i = 0; i < contentCaptions.length; i += 1) { 169 - if (tryUseCached(contentCaptions[i], translatedCache)) { 161 + if (tryUseCachedCaption(contentCaptions[i], translatedCache)) { 170 162 continue; 171 163 } 172 164 const batch: ContentCaption[] = [contentCaptions[i]]; ··· 176 168 if (i + j >= contentCaptions.length) { 177 169 break; 178 170 } 179 - if (tryUseCached(contentCaptions[i + j], translatedCache)) { 171 + if (tryUseCachedCaption(contentCaptions[i + j], translatedCache)) { 180 172 continue; 181 173 } 182 174 batch.push(contentCaptions[i + j]); ··· 196 188 : undefined; 197 189 } 198 190 191 + // TODO: make this support multiple providers rather than just google translate 199 192 export async function translateSubtitle( 200 193 id: string, 201 194 srtData: string, 202 195 targetLang: string, 203 196 ): Promise<string | undefined> { 204 - id = `${id}_${targetLang}`; 205 - const cachedData: ArrayBuffer | undefined = SUBTITLES_CACHE.get(id); 197 + const cacheID = `${id}_${targetLang}`; 198 + 199 + const cachedData: ArrayBuffer | undefined = SUBTITLES_CACHE.get(cacheID); 206 200 if (cachedData) { 207 - console.log("[CTR] Using cached translation for", id); 201 + // console.log("Using cached translation for", id, cacheID); 208 202 return decompressStr(cachedData); 209 203 } 210 - console.log("[CTR] Translating", id); 204 + 205 + // console.log("Translating", id); 211 206 const translatedData: string | undefined = await translateSRTData( 212 207 srtData, 213 208 targetLang, ··· 215 210 if (!translatedData) { 216 211 return undefined; 217 212 } 218 - console.log("[CTR] Caching translation for", id); 219 - SUBTITLES_CACHE.set(id, await compressStr(translatedData)); 213 + 214 + // console.log("Caching translation for", id, cacheID); 215 + SUBTITLES_CACHE.set(cacheID, await compressStr(translatedData)); 220 216 return translatedData; 221 217 }