Skip to content

Commit

Permalink
fix: set IBM API location instead of URL
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Dec 8, 2018
1 parent fdb4cca commit 0c9a824
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 45 deletions.
40 changes: 30 additions & 10 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,34 @@
"description": "Value of the option."
},

"inputLabel_url": {
"message": "URL",
"description": "Placeholder of the input."
"optionTitle_ibmSpeechApiLoc": {
"message": "API location",
"description": "Title of the option."
},

"optionValue_ibmSpeechApiLoc_frankfurt": {
"message": "Frankfurt",
"description": "Value of the option."
},

"optionValue_ibmSpeechApiLoc_dallas": {
"message": "Dallas",
"description": "Value of the option."
},

"optionValue_ibmSpeechApiLoc_washington": {
"message": "Washington DC",
"description": "Value of the option."
},

"optionValue_ibmSpeechApiLoc_sydney": {
"message": "Sydney",
"description": "Value of the option."
},

"optionValue_ibmSpeechApiLoc_tokyo": {
"message": "Tokyo",
"description": "Value of the option."
},

"inputLabel_apiKey": {
Expand Down Expand Up @@ -80,20 +105,15 @@
"description": "Error message."
},

"error_missingApiUrl": {
"message":
"API URL missing. Visit the options page to configure the service.",
"description": "Error message."
},

"error_missingApiKey": {
"message":
"API key missing. Visit the options page to configure the service.",
"description": "Error message."
},

"error_internalError": {
"message": "Something went wrong.",
"message":
"Something went wrong. Open the browser console for more details.",
"description": "Error message."
}
}
24 changes: 14 additions & 10 deletions src/options/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
:label="getText('inputLabel_apiKey')">
</v-textfield>
</div>
<div class="option text-field"
<div class="option select"
v-if="options.speechService === 'ibmSpeechApi'">
<v-textfield v-model="options.ibmSpeechApiUrl"
:label="getText('inputLabel_url')">
</v-textfield>
<v-select :label="getText('optionTitle_ibmSpeechApiLoc')"
v-model="options.ibmSpeechApiLoc"
:options="selectOptions.ibmSpeechApiLoc">
</v-select>
</div>
<div class="option text-field"
v-if="options.speechService === 'ibmSpeechApi'">
Expand Down Expand Up @@ -58,13 +59,20 @@ export default {
'googleSpeechApiDemo',
'googleSpeechApi',
'ibmSpeechApi'
],
ibmSpeechApiLoc: [
'frankfurt',
'dallas',
'washington',
'sydney',
'tokyo'
]
}),
options: {
speechService: '',
googleSpeechApiKey: '',
ibmSpeechApiUrl: '',
ibmSpeechApiLoc: '',
ibmSpeechApiKey: ''
}
};
Expand All @@ -80,11 +88,7 @@ export default {
for (const option of Object.keys(this.options)) {
this.options[option] = options[option];
this.$watch(`options.${option}`, async function(value) {
if (
['googleSpeechApiKey', 'ibmSpeechApiUrl', 'ibmSpeechApiKey'].includes(
option
)
) {
if (['googleSpeechApiKey', 'ibmSpeechApiKey'].includes(option)) {
value = value.trim();
}
await storage.set({[option]: value}, 'sync');
Expand Down
35 changes: 16 additions & 19 deletions src/solve/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import storage from 'storage/storage';
import {getText, waitForElement, arrayBufferToBase64} from 'utils/common';
import {
captchaGoogleSpeechApiLangCodes,
captchaIbmSpeechApiLangCodes
captchaIbmSpeechApiLangCodes,
ibmSpeechApiUrls
} from 'utils/data';

let solverWorking = false;
Expand Down Expand Up @@ -188,16 +189,9 @@ async function solve() {

if (speechService === 'ibmSpeechApi') {
const {
ibmSpeechApiUrl: apiUrl,
ibmSpeechApiLoc: apiLoc,
ibmSpeechApiKey: apiKey
} = await storage.get(['ibmSpeechApiUrl', 'ibmSpeechApiKey'], 'sync');
if (!apiUrl) {
browser.runtime.sendMessage({
id: 'notification',
messageId: 'error_missingApiUrl'
});
return;
}
} = await storage.get(['ibmSpeechApiLoc', 'ibmSpeechApiKey'], 'sync');
if (!apiKey) {
browser.runtime.sendMessage({
id: 'notification',
Expand All @@ -207,15 +201,18 @@ async function solve() {
}
const model = captchaIbmSpeechApiLangCodes[lang] || 'en-US_BroadbandModel';

const rsp = await fetch(`${apiUrl}?model=${model}&profanity_filter=false`, {
referrer: '',
mode: 'cors',
method: 'POST',
headers: {
Authorization: 'Basic ' + window.btoa('apiKey:' + apiKey)
},
body: new Blob([audioContent], {type: 'audio/wav'})
});
const rsp = await fetch(
`${ibmSpeechApiUrls[apiLoc]}?model=${model}&profanity_filter=false`,
{
referrer: '',
mode: 'cors',
method: 'POST',
headers: {
Authorization: 'Basic ' + window.btoa('apiKey:' + apiKey)
},
body: new Blob([audioContent], {type: 'audio/wav'})
}
);

if (rsp.status !== 200) {
throw new Error(`API response: ${rsp.status}, ${await rsp.text()}`);
Expand Down
4 changes: 2 additions & 2 deletions src/storage/versions/local/ONiJBs00o.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const storage = browser.storage.local;

async function upgrade() {
const changes = {
ibmSpeechApiUrl: '',
ibmSpeechApiLoc: 'frankfurt', // frankfurt, dallas, washington, sydney, tokyo
ibmSpeechApiKey: ''
};

Expand All @@ -19,7 +19,7 @@ async function upgrade() {

async function downgrade() {
const changes = {};
await storage.remove(['ibmSpeechApiUrl', 'ibmSpeechApiKey']);
await storage.remove(['ibmSpeechApiLoc', 'ibmSpeechApiKey']);

changes.storageVersion = downRevision;
return storage.set(changes);
Expand Down
4 changes: 2 additions & 2 deletions src/storage/versions/sync/ONiJBs00o.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const storage = browser.storage.sync;

async function upgrade() {
const changes = {
ibmSpeechApiUrl: '',
ibmSpeechApiLoc: 'frankfurt', // frankfurt, dallas, washington, sydney, tokyo
ibmSpeechApiKey: ''
};

Expand All @@ -19,7 +19,7 @@ async function upgrade() {

async function downgrade() {
const changes = {};
await storage.remove(['ibmSpeechApiUrl', 'ibmSpeechApiKey']);
await storage.remove(['ibmSpeechApiLoc', 'ibmSpeechApiKey']);

changes.storageVersion = downRevision;
return storage.set(changes);
Expand Down
18 changes: 16 additions & 2 deletions src/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import browser from 'webextension-polyfill';
const optionKeys = [
'speechService',
'googleSpeechApiKey',
'ibmSpeechApiUrl',
'ibmSpeechApiLoc',
'ibmSpeechApiKey'
];

Expand Down Expand Up @@ -103,8 +103,22 @@ const captchaIbmSpeechApiLangCodes = {
'es-419': 'es-ES_BroadbandModel'
};

// https://cloud.ibm.com/apidocs/speech-to-text#service-endpoint
const ibmSpeechApiUrls = {
frankfurt:
'https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize',
dallas: 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize',
washington:
'https://gateway-wdc.watsonplatform.net/speech-to-text/api/v1/recognize',
sydney:
'https://gateway-syd.watsonplatform.net/speech-to-text/api/v1/recognize',
tokyo:
'https://gateway-tok.watsonplatform.net/speech-to-text/api/v1/recognize'
};

export {
optionKeys,
captchaGoogleSpeechApiLangCodes,
captchaIbmSpeechApiLangCodes
captchaIbmSpeechApiLangCodes,
ibmSpeechApiUrls
};

0 comments on commit 0c9a824

Please sign in to comment.