Last active
July 10, 2018 16:02
-
-
Save eyecatchup/812b50c0a1617b5d8f56132546a831ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get a tokenized(!) request URL for a Google Translate tts audio file; i.e.: | |
* https://translate.google.com/translate_tts?q=hello%20world&tl=en&tk=159027.275675 | |
* (URL shortened for better readability.) | |
* | |
* @author Stephan Schmitz <[email protected]> | |
* @copyright: Google Inc. | |
* @param urldecodedString {string} The string to be spoken in the audio file. | |
* @param hl {string} Optional two-letter country code for the output language. Default: English. | |
* @return {string} The fully-qualified request URL to the audio file (Content-type: audio/mpeg). | |
*/ | |
var GetTokenizedTtsUrl = function(urldecodedString, hl) { | |
"use strict"; | |
hl = hl ? hl : "en"; // default output language (two-letter ISO code) | |
var host = "https://translate.google.com", | |
queryStringBegin = "/translate_tts?ie=UTF-8&q=" + | |
encodeURIComponent(urldecodedString) + "&tl=" + | |
hl + "&total=1&idx=0&textlen=" + urldecodedString.length, | |
queryStringEnd = "&client=t&prev=input", | |
salt = "416234.2030088909", d; // window["TKK"] | |
var fn = function(a) { | |
return function() { | |
return a; | |
}; | |
}, | |
hash = function(a, c) { | |
for (var d = 0; d < c.length - 2; d += 3) { | |
var b = c.charAt(d + 2), | |
b = "a" <= b ? b.charCodeAt(0) - 87 : Number(b), | |
b = "+" == c.charAt(d + 1) ? a >>> b : a << b; | |
a = "+" == c.charAt(d) ? a + b & 4294967295 : a ^ b; | |
} | |
return a; | |
}, | |
tokenParamString = function(a) { | |
var c = salt; | |
var b = fn(String.fromCharCode(116)); | |
d = fn(String.fromCharCode(107)); | |
b = [b(), b()]; | |
b[1] = d(); | |
d = "&" + b.join("") + "="; | |
b = c.split("."); | |
c = Number(b[0]) || 0; | |
for (var g = [], f = 0, h = 0; h < a.length; h++) { | |
var e = a.charCodeAt(h); | |
128 > e ? g[f++] = e : (2048 > e ? g[f++] = e >> 6 | 192 : | |
(55296 == (e & 64512) && h + 1 < a.length && 56320 == | |
(a.charCodeAt(h + 1) & 64512) ? (e = 65536 + (( | |
e & 1023) << 10) + (a.charCodeAt(++h) & | |
1023), g[f++] = e >> 18 | 240, g[f++] = | |
e >> 12 & 63 | 128) : g[f++] = e >> 12 | | |
224, g[f++] = e >> 6 & 63 | 128), g[f++] = e & | |
63 | 128); | |
} | |
a = c; | |
for (f = 0; f < g.length; f++) { | |
a += g[f], a = hash(a, "+-a^+6"); | |
} | |
a = hash(a, "+-3^+b+-f"); | |
a ^= Number(b[1]) || 0; | |
0 > a && (a = (a & 2147483647) + 2147483648); | |
a %= 1E6; | |
return d + (a.toString() + "." + (a ^ c)); | |
}(urldecodedString); | |
return host + queryStringBegin + tokenParamString + queryStringEnd; | |
}; | |
// Example usage | |
var ttsUrl = GetTokenizedTtsUrl("Hallo, wie geht's?", "de"); | |
console.log(ttsUrl); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param l {string} The string to be spoken in the audio file. | |
* @param k {string} Optional two-letter country code for the output language. Default: English. | |
* @return {string} The fully-qualified request URL to the audio file (Content-type: audio/mpeg). | |
*/ | |
var GetTokenizedTtsUrl=function(l,k){k=k?k:"en";var p="/translate_tts?ie=UTF-8&q="+encodeURIComponent(l)+"&tl="+k+"&total=1&idx=0&textlen="+l.length,m=function(a){return function(){return a}},n=function(a,g){for(var c=0;c<g.length-2;c+=3){var b=g.charAt(c+2),b="a"<=b?b.charCodeAt(0)-87:Number(b),b="+"==g.charAt(c+1)?a>>>b:a<<b;a="+"==g.charAt(c)?a+b&4294967295:a^b}return a},q=function(a){var g="416234.2030088909",c=m(String.fromCharCode(116));d=m(String.fromCharCode(107));c=[c(),c()];c[1]=d();d="&"+ | |
c.join("")+"=";for(var c=g.split("."),g=Number(c[0])||0,b=[],f=0,h=0;h<a.length;h++){var e=a.charCodeAt(h);128>e?b[f++]=e:(2048>e?b[f++]=e>>6|192:(55296==(e&64512)&&h+1<a.length&&56320==(a.charCodeAt(h+1)&64512)?(e=65536+((e&1023)<<10)+(a.charCodeAt(++h)&1023),b[f++]=e>>18|240,b[f++]=e>>12&63|128):b[f++]=e>>12|224,b[f++]=e>>6&63|128),b[f++]=e&63|128)}a=g;for(f=0;f<b.length;f++)a+=b[f],a=n(a,"+-a^+6");a=n(a,"+-3^+b+-f");a^=Number(c[1])||0;0>a&&(a=(a&2147483647)+2147483648);a%=1E6;return d+(a.toString()+ | |
"."+(a^g))}(l);return"https://translate.google.com"+p+q+"&client=t&prev=input"}; | |
// Example usage | |
console.log(GetTokenizedTtsUrl("Get a tokenized request URL for a Google Translate tts audio file.")); | |
var charSequences = [ | |
"grün", | |
"rot", | |
"gelb", | |
"blau", | |
"schwarz", | |
"weiss", | |
"Das hat 14.99 Euro gekostet" | |
], | |
ttsUrls = []; | |
for (i in charSequences) { | |
var ttsUrl = GetTokenizedTtsUrl(charSequences[i], "de"); | |
if (!!ttsUrl && ttsUrl.length) { | |
ttsUrls.push(ttsUrl); | |
console.log(ttsUrl); | |
} | |
} | |
console.dir(ttsUrls); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment