diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-01-18 22:01:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-18 22:01:08 -0500 |
commit | 21fce9f3d98e4381f8813cf9c63410ca1dbd7f91 (patch) | |
tree | 124a510e4f208bbd7516b2eae98454400e9b4c54 /ext/bg/js/audio-downloader.js | |
parent | 85c723b85f47ff3048ba7aca46a532aa1bc44064 (diff) |
Audio system refactoring (#1275)
* Simplify details
* Simplify audio creation
* Return an array of sources instead of a single item
* Use sourceIndex instead of index
* Rename APIs
* Return more info about the source
* Return source instead of sourceIndex
Diffstat (limited to 'ext/bg/js/audio-downloader.js')
-rw-r--r-- | ext/bg/js/audio-downloader.js | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/ext/bg/js/audio-downloader.js b/ext/bg/js/audio-downloader.js index 19e9c368..171f5944 100644 --- a/ext/bg/js/audio-downloader.js +++ b/ext/bg/js/audio-downloader.js @@ -34,7 +34,7 @@ class AudioDownloader { ]); } - async getInfo(source, expression, reading, details) { + async getExpressionAudioInfoList(source, expression, reading, details) { const handler = this._getInfoHandlers.get(source); if (typeof handler === 'function') { try { @@ -43,23 +43,22 @@ class AudioDownloader { // NOP } } - return null; + return []; } - async downloadAudio(sources, expression, reading, details) { + async downloadExpressionAudio(sources, expression, reading, details) { for (const source of sources) { - const info = await this.getInfo(source, expression, reading, details); - if (info === null) { continue; } - - switch (info.type) { - case 'url': - try { - const {details: {url}} = info; - return await this._downloadAudioFromUrl(url); - } catch (e) { - // NOP - } - break; + const infoList = await this.getExpressionAudioInfoList(source, expression, reading, details); + for (const info of infoList) { + switch (info.type) { + case 'url': + try { + return await this._downloadAudioFromUrl(info.url); + } catch (e) { + // NOP + } + break; + } } } @@ -90,7 +89,7 @@ class AudioDownloader { } const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`; - return {type: 'url', details: {url}}; + return [{type: 'url', url}]; } async _getInfoJpod101Alternate(expression, reading) { @@ -128,7 +127,7 @@ class AudioDownloader { const htmlReading = dom.getTextContent(htmlReadings[0]); if (htmlReading && (!reading || reading === htmlReading)) { url = this._normalizeUrl(url, response.url); - return {type: 'url', details: {url}}; + return [{type: 'url', url}]; } } catch (e) { // NOP @@ -159,7 +158,7 @@ class AudioDownloader { let url = dom.getAttribute(source, 'src'); if (url !== null) { url = this._normalizeUrl(url, response.url); - return {type: 'url', details: {url}}; + return [{type: 'url', url}]; } } } @@ -174,14 +173,14 @@ class AudioDownloader { if (!textToSpeechVoice) { throw new Error('No voice'); } - return {type: 'tts', details: {text: expression, voice: textToSpeechVoice}}; + return [{type: 'tts', text: expression, voice: textToSpeechVoice}]; } async _getInfoTextToSpeechReading(expression, reading, {textToSpeechVoice}) { if (!textToSpeechVoice) { throw new Error('No voice'); } - return {type: 'tts', details: {text: reading || expression, voice: textToSpeechVoice}}; + return [{type: 'tts', text: reading || expression, voice: textToSpeechVoice}]; } async _getInfoCustom(expression, reading, {customSourceUrl}) { @@ -190,7 +189,7 @@ class AudioDownloader { } const data = {expression, reading}; const url = customSourceUrl.replace(/\{([^}]*)\}/g, (m0, m1) => (Object.prototype.hasOwnProperty.call(data, m1) ? `${data[m1]}` : m0)); - return {type: 'url', details: {url}}; + return [{type: 'url', url}]; } async _downloadAudioFromUrl(url) { |