# -*- coding:utf-8 -*-
import requests
import re
import PyV8
import codecs
from fake_useragent import UserAgent

class GoogleTranslate:
    def __init__(self,content):
        self.content = content
        ua = UserAgent()
        self.headers = {
            'User-Agent':ua.random
        }

    # 从网页解析出TKK函数
    def get_TKK(self):
        weburl = 'http://translate.google.cn/'
        response = requests.get(weburl,headers=self.headers).text
        result = re.findall(r'(TKK.+?\);)',response)[0]
        result = re.findall(r"TKK=eval\('(.+)'\);",result)[0] + ';'
        return result

    # 破解tk,拿到tk值
    def get_tk(self):
        with PyV8.JSContext() as ctxt:
            # 恢复'\x'的转义功能
            TKK = codecs.getdecoder("unicode_escape")(self.get_TKK())[0]
            func = ctxt.eval(TKK)
            ctxt.eval("""
            var b = function (a, b) {
            for (var d = 0; d < b.length - 2; d += 3) {
                var c = b.charAt(d + 2),
                    c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
                    c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
                a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
            }
            return a
        }
        var tk =  function (a,TKK) {
            for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
                var c = a.charCodeAt(f);
                128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
            }
            a = h;
            for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
            a = b(a, "+-3^+b+-f");
            a ^= Number(e[1]) || 0;
            0 > a && (a = (a & 2147483647) + 2147483648);
            a %= 1E6;
            return a.toString() + "." + (a ^ h)
        }
            """)
            vars = ctxt.locals
            Tkk = vars.tk
            tk = Tkk(self.content,func)
            print(tk)
            return tk

    # 返回目标翻译结果,输入语言随机,翻译结果语言为中文
    def get_translated(self):
        tk = self.get_tk()
        url = "https://translate.google.cn/translate_a/single"
        params = {
        "client":"t",
        "sl":"auto",
        "tl":"zh-CN",
        "hl":"zh-CN",
        "dt":"at",
        "dt":"bd",
        "dt":"ex",
        "dt":"ld",
        "dt":"md",
        "dt":"qca",
        "dt":"rw",
        "dt":"rm",
        "dt":"ss",
        "dt":"t",
        "ie":"UTF-8",
        "oe":"UTF-8",
        "otf":"2",
        "ssel":"0",
        "tsel":"0",
        "kc":"1",
        "tk":tk,
        "q":self.content,
        }
        content = requests.get(url,params=params,headers=self.headers).text
        content = content.replace('null','None')
        print(content)
        li = eval(content)
        result = li[0]
        result_list = []
        for i in result:
            result_list.append(i[0])
        print(result_list)
        # 标题长度处理,大于6,小于30
        if len(result_list[0])<30:
            print(result_list[0])
            if len(result_list[0])>6:
                return result_list[0].strip().replace('.mp4','').replace('&',',')
            else:
                return (result_list[0]+result_list[1].split()[0]).replace('.mp4','').replace('&',',')
        else:
            # 首个翻译结果长度大于30,重新切割拼接
            result = result_list[0].split(' ')
            add_result = ''
            for i in result:
                if len(i)<6:
                    if len(add_result)<6:
                        add_result += i
                    else:
                        return add_result.replace('.mp4','').replace('&',',')
                else:
                    mark = [':',',','!','?','。','【','】','']
                    num = 0
                    for m in mark:
                        num += 1
                        if num == len(mark):
                            return i.strip().replace('.mp4','').replace('&',',')
                        if m in i:
                            result = i.split(m)
                            for r in result:
                                if len(r)>6 and len(r)<29:
                                    return r.replace('.mp4','').replace('&',',')


if __name__ == '__main__':
    content = '魔法能穿墻?Jonnes如何得到穿墙魔法呢?搞笑角色扮演 Jo短剧场(中英文字幕) Magic through the wall!(English Subtitle).mp4'
    go = GoogleTranslate(content)
    file_name = go.get_translated()+'.mp4'
    print(file_name)

本站以现代、古代情诗为主,情诗网创办于2013年,以原创爱情诗歌、经典情诗、现代情诗、古代情诗、英文情诗、情诗绝句为主并收集古诗、古诗词、诗歌大全、诗词名句的文学门户。方便您下次继续阅读;可以放在浏览器的收藏夹中(快捷键Ctrl+D);或者看到喜欢或者有趣的诗词可以通过分享按钮给你的好友分享;情诗网是目前最全情诗大全网站之一。并欢迎广大诗歌爱好者阅览投稿!喜欢本站的话请大家把本站告诉给你朋友哦!地址是 www.qingshiwang.com !