机翻话小工具
最近看网上有的那些机翻话(外国人在中国的评论区留言时翻译的中文)非常有意思,于是我也写了一款这样一款工具原理就是把中文经过多种语言的翻译之后,再翻译回中文,话不多说上代码:
import execjs
import json
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50",
"Accept-Encoding": "gzip, deflate, br",
"Cookie": "这里写上你自己的百度翻译的cookie",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
}
def getSign(content):
with open("test.js","r",encoding="utf-8") as f:
js_code = f.read()
ctx = execjs.compile(js_code)
result = ctx.call("e",content)
return result
def translate(content, from_, to):
url = "https://fanyi.baidu.com/v2transapi"
data = {
"from": from_,
"to": to,
"query": content,
"transtype": "realtime",
"simple_means_flag": "3",
"sign": getSign(content),
"token": "030ba72434cb1f254df4c383e0cfe86b",
"domain": "common",
}
response = requests.post(url,data=data,headers=headers)
result = json.loads(response.text)
data = result["trans_result"]["data"]
return data["dst"]
if __name__ == "__main__":
# language_list = ["en", "ara", "de", "ru", "fra", "kor"]
while True:
content = input(">")
con_en = translate(content=content, to="en", from_="zh")
con_ara = translate(content=con_en, to="ara", from_="en")
con_de = translate(content=con_ara, to="de", from_="ara")
con_ru = translate(content=con_de, to="ru", from_="de")
con_fra = translate(content=con_ru, to="fra", from_="ru")
con_kor = translate(content=con_fra, to="kor", from_="fra")
content_zh = translate(content=con_kor, to="zh", from_="kor")
print(content_zh)
其中还用到了百度翻译的sign值破解方法,我先把代码放在这:
var i="320305.131321201"
function n(r, o) {
for (var t = 0; t < o.length - 2; t += 3) {
var a = o.charAt(t + 2);
a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
a = "+" === o.charAt(t + 1) ? r >>> a : r << a,
r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
}
return r
}
function e(r) {
var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
if (null === o) {
var t = r.length;
t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
} else {
for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)
"" !== e && f.push.apply(f, a(e.split(""))),
C !== h - 1 && f.push(o);
var g = f.length;
g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
}
var u = void 0
, l = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
u = null !== i ? i : (i = window || "") || "";
for (var d = u.split("."), m = Number(d) || 0, s = Number(d) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
var A = r.charCodeAt(v);
128 > A ? S = A : (2048 > A ? S = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)),
S = A >> 18 | 240,
S = A >> 12 & 63 | 128) : S = A >> 12 | 224,
S = A >> 6 & 63 | 128),
S = 63 & A | 128)
}
for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++)
p += S,
p = n(p, F);
return p = n(p, D),
p ^= s,
0 > p && (p = (2147483647 & p) + 2147483648),
p %= 1e6,
ans = p.toString() + "." + (p ^ m)
return ans
}
用法:将这两个文件放在同一目录下,保证js文件名为test.js ,然后打开python文件即可使用。
如果大家对破解这个sign值感兴趣的,可以在网上百度看看,有很多大佬给出了解法
给个免费的评分谢谢{:301_991:}
其实不用最后一行的ans,直接就出来了,https://fanyi-cdn.cdn.bcebos.com/static/translation/pkg/index的js中直接复制出来就有了
另外,i也不用申明,提前获取一下网页,直接从网页中可以取到,免得哪天换了找不到地方 虽然看不懂,但是感觉好厉害。。。 牛!!!!谢谢分享! 感谢好人分享 这个能不能翻译俄语 做得好!:keai 谢谢你! 感谢分享 写的好,拿去学习了。 百度翻译不就是现成的了吗
页:
[1]
2