Update how audio URLs are constructed
This commit is contained in:
parent
7ce54864f3
commit
60a80418d7
@ -17,8 +17,8 @@
|
||||
*/
|
||||
|
||||
|
||||
async function audioBuildUrl(definition, mode, cache={}) {
|
||||
if (mode === 'jpod101') {
|
||||
const audioUrlBuilders = {
|
||||
'jpod101': async (definition) => {
|
||||
let kana = definition.reading;
|
||||
let kanji = definition.expression;
|
||||
|
||||
@ -35,37 +35,18 @@ async function audioBuildUrl(definition, mode, cache={}) {
|
||||
params.push(`kana=${encodeURIComponent(kana)}`);
|
||||
}
|
||||
|
||||
const url = `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`;
|
||||
return Promise.resolve(url);
|
||||
} else if (mode === 'jpod101-alternate') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const response = cache[definition.expression];
|
||||
if (response) {
|
||||
resolve(response);
|
||||
} else {
|
||||
const data = {
|
||||
post: 'dictionary_reference',
|
||||
match_type: 'exact',
|
||||
search_query: definition.expression
|
||||
};
|
||||
|
||||
const params = [];
|
||||
for (const key in data) {
|
||||
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`);
|
||||
}
|
||||
|
||||
return `https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?${params.join('&')}`;
|
||||
},
|
||||
'jpod101-alternate': async (definition) => {
|
||||
const response = await new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post');
|
||||
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||||
xhr.addEventListener('error', () => reject(new Error('Failed to scrape audio data')));
|
||||
xhr.addEventListener('load', () => {
|
||||
cache[definition.expression] = xhr.responseText;
|
||||
resolve(xhr.responseText);
|
||||
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
||||
xhr.send(`post=dictionary_reference&match_type=exact&search_query=${encodeURIComponent(definition.expression)}`);
|
||||
});
|
||||
|
||||
xhr.send(params.join('&'));
|
||||
}
|
||||
}).then(response => {
|
||||
const dom = new DOMParser().parseFromString(response, 'text/html');
|
||||
for (const row of dom.getElementsByClassName('dc-result-row')) {
|
||||
try {
|
||||
@ -78,28 +59,22 @@ async function audioBuildUrl(definition, mode, cache={}) {
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
});
|
||||
} else if (mode === 'jisho') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const response = cache[definition.expression];
|
||||
if (response) {
|
||||
resolve(response);
|
||||
} else {
|
||||
|
||||
throw new Error('Failed to find audio URL');
|
||||
},
|
||||
'jisho': async (definition) => {
|
||||
const response = await new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', `https://jisho.org/search/${definition.expression}`);
|
||||
xhr.addEventListener('error', () => reject(new Error('Failed to scrape audio data')));
|
||||
xhr.addEventListener('load', () => {
|
||||
cache[definition.expression] = xhr.responseText;
|
||||
resolve(xhr.responseText);
|
||||
xhr.addEventListener('load', () => resolve(xhr.responseText));
|
||||
xhr.send();
|
||||
});
|
||||
|
||||
xhr.send();
|
||||
}
|
||||
}).then(response => {
|
||||
try {
|
||||
const dom = new DOMParser().parseFromString(response, 'text/html');
|
||||
try {
|
||||
const audio = dom.getElementById(`audio_${definition.expression}:${definition.reading}`);
|
||||
if (audio) {
|
||||
if (audio !== null) {
|
||||
const url = audio.getElementsByTagName('source').item(0).getAttribute('src');
|
||||
if (url) {
|
||||
return audioUrlNormalize(url, 'https://jisho.org', '/search/');
|
||||
@ -108,11 +83,27 @@ async function audioBuildUrl(definition, mode, cache={}) {
|
||||
} catch (e) {
|
||||
// NOP
|
||||
}
|
||||
});
|
||||
|
||||
throw new Error('Failed to find audio URL');
|
||||
}
|
||||
else {
|
||||
return Promise.resolve();
|
||||
};
|
||||
|
||||
async function audioBuildUrl(definition, mode, cache={}) {
|
||||
const cacheKey = `${mode}:${definition.expression}`;
|
||||
if (cache.hasOwnProperty(cacheKey)) {
|
||||
return Promise.resolve(cache[cacheKey]);
|
||||
}
|
||||
|
||||
if (audioUrlBuilders.hasOwnProperty(mode)) {
|
||||
const handler = audioUrlBuilders[mode];
|
||||
return handler(definition).then(
|
||||
(url) => {
|
||||
cache[cacheKey] = url;
|
||||
return url;
|
||||
},
|
||||
() => null);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function audioUrlNormalize(url, baseUrl, basePath) {
|
||||
|
Loading…
Reference in New Issue
Block a user