matheuss / google-translate-api

A free and unlimited API for Google Translate :dollar::no_entry_sign:
https://npmjs.com/package/google-translate-api
MIT License
2.91k stars 608 forks source link

Here's how to fix the BAD_REQUEST error on the npm package #118

Open Iru21 opened 3 years ago

Iru21 commented 3 years ago

Find the google-translate-api in your node_modules folder and replace tk.js file content with this (it is literally this but I fixed the export)

var got = require("got"), Configstore = require("configstore"); function sM(r) { var e; if (null !== yr) e = yr; else { e = wr(String.fromCharCode(84)); var o = wr(String.fromCharCode(75)); ((e = [e(), e()])[1] = o()), (e = (yr = window[e.join(o())] || "") || ""); } var t = wr(String.fromCharCode(116)); o = wr(String.fromCharCode(107)); ((t = [t(), t()])[1] = o()), (o = "&" + t.join("") + "="), (t = e.split(".")), (e = Number(t[0]) || 0); for (var n = [], a = 0, c = 0; c < r.length; c++) { var i = r.charCodeAt(c); 128 > i ? (n[a++] = i) : (2048 > i ? (n[a++] = (i >> 6) | 192) : (55296 == (64512 & i) && c + 1 < r.length && 56320 == (64512 & r.charCodeAt(c + 1)) ? ((i = 65536 + ((1023 & i) << 10) + (1023 & r.charCodeAt(++c))), (n[a++] = (i >> 18) | 240), (n[a++] = ((i >> 12) & 63) | 128)) : (n[a++] = (i >> 12) | 224), (n[a++] = ((i >> 6) & 63) | 128)), (n[a++] = (63 & i) | 128)); } for (r = e, a = 0; a < n.length; a++) (r += n[a]), (r = xr(r, "+-a^+6")); return (r = xr(r, "+-3^+b+-f")), 0 > (r ^= Number(t[1]) || 0) && (r = 2147483648 + (2147483647 & r)), o + ((r %= 1e6).toString() + ".") + (r ^ e); } var yr = null, wr = function (r) { return function () { return r; }; }, xr = function (r, e) { for (var o = 0; o < e.length - 2; o += 3) { var t = "a" <= (t = e.charAt(o + 2)) ? t.charCodeAt(0) - 87 : Number(t); t = "+" == e.charAt(o + 1) ? r >>> t : r << t; r = "+" == e.charAt(o) ? (r + t) & 4294967295 : r ^ t; } return r; }, config = new Configstore("google-translate-api"), window = { TKK: config.get("TKK") || "0" }; function updateTKK() { return new Promise(function (resolve, reject) { var now = Math.floor(Date.now() / 36e5); Number(window.TKK.split(".")[0]) === now ? resolve() : got("https://translate.google.com") .then(function (res) { var code = res.body.match(/tkk:\'\d+\.\d+\'/g); if (code) { eval(code[0]); var TKK = code[0].match(/\d+\.\d+/g)[0]; void 0 !== TKK && ((window.TKK = TKK), config.set("TKK", TKK)); } resolve(); }) .catch(function (r) { var e = new Error(); (e.code = "BAD_NETWORK"), (e.message = r.message), reject(e); }); }); } function get(r) { return updateTKK() .then(function () { var e = sM(r); return { name: "tk", value: (e = e.replace("&tk=", "")) }; }) .catch(function (r) { throw r; }); } module.exports = get;

This repo's been dead so just making this issue here so people don't make any more of these

tyt34 commented 2 years ago

I use command npm install @vitalets/google-translate-api this error has been fixed