cjvnjde / google-translate-api-browser

A free and unlimited API for Google Translate
https://www.npmjs.com/package/google-translate-api-browser
MIT License
213 stars 31 forks source link

Could you add a POST request from GET for more capacity? #20

Closed taishi55 closed 1 year ago

taishi55 commented 1 year ago

The maximum number of letter seems roughly 1850 per request.

Is it possible to add a POST request to increase the input text to 5000?

This is my suggestion below. (It didn't work sadly)

export const traslateToEng = async (sourceText: string): Promise<string> => {
  const queryParams = {
    client: translateOptions.client,
    sl: translateOptions.from,
    tl: translateOptions.to,
    hl: translateOptions.hl,
    ie: 'UTF-8',
    oe: 'UTF-8',
    otf: '1',
    ssel: '0',
    tsel: '0',
    kc: '7',
    q: sourceText,
    tk: sM(sourceText)
  };

  const url = `https://translate.google.${translateOptions.tld}/translate_a/single`;

  const options = {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: new URLSearchParams(queryParams).toString()
  };

  return new Promise((resolve, reject) => {
    https
      .request(url, options, (resp) => {
        let data = '';

        resp.on('data', (chunk) => {
          data += chunk;
        });

        resp.on('end', () => {
          const result = normaliseResponse(JSON.parse(data));
          resolve(result?.text || sourceText);
        });
      })
      .on('error', (err) => {
        console.log('Error: ' + err.message);
        reject(err);
      })
      .end();
  });
};
cjvnjde commented 1 year ago

I tried to figure out how to make a request using POST. And it seems possible. This example worked for me.

const traslateToEng = async (sourceText) => {
    const encodedValue = encodeURIComponent(`[[["MkEWBc","[[\\"${sourceText}\\",\\"ru\\",\\"en\\",true],[1]]",null,"generic"]]]`);
    return axios({
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data: `f.req=${encodedValue}&`,
        url: 'https://translate.google.com/_/TranslateWebserverUi/data/batchexecute?hl=en-US',
    })
};

traslateToEng('Привет').then((result) => {
    console.log(JSON.parse(result.data.replace(')]}\'', '').trim()));
});

But there is still a lot of work. Сurrent functions don't work very well for this example.

taishi55 commented 1 year ago

It looks perfect to me haha. What is the sM function doing in your current repo? I really wonder what it does

cjvnjde commented 1 year ago

sM generates necessary token. Here is the repo sM is based on. https://github.com/matheuss/google-translate-token

cjvnjde commented 1 year ago

I've updated the library. Try to use alpha version that uses POST requests.

npm install google-translate-api-browser@alpha

I tested it with this example https://github.com/cjvnjde/google-translate-api-browser/tree/v4.0.6-alpha/examples/server

But I'm not sure about the reliability of this version.

taishi55 commented 1 year ago

Thank you for the update. I will look into that. I got it! I will try to test if I can apply the approach to DeepL.

Asmedian commented 1 year ago

But I'm not sure about the reliability of this version.

image

Error when using in Node translate(string, {to: lang}) .then(res => { console.log(res.text) }) .catch(err => { console.error(err); });

cjvnjde commented 1 year ago

But I'm not sure about the reliability of this version.

image

Error when using in Node translate(string, {to: lang}) .then(res => { console.log(res.text) }) .catch(err => { console.error(err); });

Google has changed the structure of the response. In version 4.1 it should work.

Post requests should also work.