lkuza2 / java-speech-api

The J.A.R.V.I.S. Speech API is designed to be simple and efficient, using the speech engines created by Google to provide functionality for parts of the API. Essentially, it is an API written in Java, including a recognizer, synthesizer, and a microphone capture utility. The project uses Google services for the synthesizer and recognizer. While this requires an Internet connection, it provides a complete, modern, and fully functional speech API in Java.
GNU General Public License v3.0
531 stars 304 forks source link

Google Translation API and Language Detection Endpoint Broken #69

Closed Skylion007 closed 8 years ago

Skylion007 commented 8 years ago

Google has once again changed their API for translation. Now, the service uses token generation to as a further authentication feature. However, the token itself changes daily so we will need workaround that issue.

I found a relevant discussion here is anyone wants to take it upon themselves to restore the Google Translation portion of the API. https://github.com/Stichoza/google-translate-php/issues/32

However, until we can get a reliable system for translation working, TTS auto-language detection and translation remain broken. I deprecated the default the constructor for Synthesizer for now to reflect this issue.

dean1510 commented 8 years ago

Use below code to calculate "tk" parameter. Tested on Ascii and diacritics (not CJK) input source. NB: "generateB" isn't precisely ported. However it's a noisebase so not significant (for now).

int generateB() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);

    Date start;
    Date now;
    try {
        start = sdf.parse("01/01/1970");
        now = new Date();
    } catch (ParseException e) {
        return 402890;
    }
    long diff = now.getTime() - start.getTime();
    long hours = diff / (60 * 60 * 1000) % 24;
    long days = diff / (24 * 60 * 60 * 1000);
    return (int) (hours + days * 24);
}

int shr32(int x, int bits) {
    if (x < 0) {
        long x_l = 0xffffffffl + x + 1;
        return (int) (x_l >> bits);
    }
    return x >> bits;
}

int charCodeAt(String str, int index) {
    return str.charAt(index);
}

int RL(int a, String b) {
    for (int c = 0; c < b.length() - 2; c += 3) {
        int d = b.charAt(c + 2);
        d = d >= 65 ? d - 87 : d - 48;
        d = b.charAt(c + 1) == '+' ? shr32(a, d) : (a << d);
        a = b.charAt(c) == '+' ? (a + d & 0xFFFFFFFF) : a ^ d;
    }
    return a;
}

String TL(String a) {
    int b = generateB();
    int e = 0;
    int f = 0;
    Vector<Integer> d = new Vector<Integer>();
    for (; f < a.length(); f++) {
        int g = charCodeAt(a, f);

        if (0x80 > g) {
            d.add(e++, g);
        } else {
            if (0x800 > g) {
                d.add(e++, g >> 6 | 0xC0);
            } else {
                if (0xD800 == (g & 0xFC00) && f + 1 < a.length() && 0xDC00 == (charCodeAt(a, f + 1) & 0xFC00)) {
                    g = 0x10000 + ((g & 0x3FF) << 10) + (charCodeAt(a, ++f) & 0x3FF);
                    d.add(e++, g >> 18 | 0xF0);
                    d.add(e++, g >> 12 & 0x3F | 0x80);
                } else {
                    d.add(e++, g >> 12 | 0xE0);
                    d.add(e++, g >> 6 & 0x3F | 0x80);
                }
            }
            d.add(e++, g & 63 | 128);
        }
    }

    int a_i = b;
    for (e = 0; e < d.size(); e++) {
        a_i += d.get(e);
        a_i = RL(a_i, "+-a^+6");
    }
    a_i = RL(a_i, "+-3^+b+-f");
    long a_l;
    if (0 > a_i) {
        a_l = 0x80000000l + a_i & 0x7FFFFFFF;
    } else {
        a_l = a_i;
    }
    a_l %= Math.pow(10, 6);
    return String.format(Locale.US, "%d.%d", a_l, a_l ^ b);
}
Skylion007 commented 8 years ago

@dean1510

http://translate.google.com/translate_a/t?client=t&hl=en&sl=en&tl=es&text=hello&ie=UTF-8&oe=UTF-8&multires=1&otf=0&pc=0&trs=1&ssel=0&tsel=0&sc=1&tk=795204.656777

Am I feeding in the wrong arguments somewhere I wonder? Did mispell or am I missing a parameter? Did I forget to encode something?

dean1510 commented 8 years ago

Small bugfix: if (0 > a_i) { a_l = 0x80000000l + (a_i & 0x7FFFFFFF); } else { a_l = a_i; }

so TK value for "hello" should be: 954118.568012

Skylion007 commented 8 years ago

Yep that fixed it!

Skylion007 commented 8 years ago

@dean1510 Scrap the previous comment there appears to be another bug. And this is one is time sensitive bizarrely.

java.io.IOException: Server returned HTTP response code: 503 for URL: http://translate.google.com/translate_a/t?client=t&hl=en&sl=en&tl=es&text=hello&multires=1&otf=0&pc=0&trs=1&ssel=0&tsel=0&sc=1&ie=UTF-8&oe=UTF-8&tk=287118.149916

The code was working at 10:30AM EST but when I was about push it at 10:50AM I reran the test after only updating comments and then I got the 503 error again. I reverted all changes via ctrl-Z to the working version, but it seems to not be working either. The only thing that has changed is the time so maybe there is another parenthetical or XOR bug somewhere?

At the current time I am I should be getting tk=945986.542554, but I am instead getting 287118.149916.

I committed the changes anyway so we can double check the code together. 4d1c6c10a86ebfbdf73c1d17248744c6fbf7db60

so here is the issue. When I use the URL that ends in /t I receive a 503. When I use /single it works, however, but it doesn't return any information unless I use the dt parameters which I do not know how to generate.

Skylion007 commented 8 years ago

@dean1510 So the new version is up and running. Thanks for all the help! Feel free to make pull requests in the future since we seem to be working on similar projects. Feel free to add any cool new features you find in the API.

dean1510 commented 8 years ago

Seems it's time to make a patch!

Skylion007 commented 8 years ago

Unfortunately, I am very preoccupied studying for my finals this week. :/ Will get on it as soon as I have time

dean1510 commented 8 years ago

You can open write access for me to make changes directly.

Skylion007 commented 8 years ago

I'd be glad to, unfortunately only @lkuza2 can do that at the moment. I can accept any pull requests you make though. I'll talk to him about getting you write access.

lkuza2 commented 8 years ago

I have added @dean1510 as a collaborator :)

dean1510 commented 8 years ago

Updated, please check. Initial hash values are hardcoded since their nature is still not explained.

Skylion007 commented 8 years ago

Seems to still be working, we should keep an eye on this issue to ensure that it continues to work in the future.