# -*- coding: utf-8 -*-
# @Author: wangkun
# @Time: 2023/9/11
import requests
import hashlib
import time
import random

"""
有道
py3.6
ning
"""
"""
 var r = function(e) {
    var t = n.md5(navigator.appVersion)
      , r = "" + (new Date).getTime()
      , i = r + parseInt(10 * Math.random(), 10);
    return {
        ts: r,
        bv: t,
        salt: i,
        sign: n.md5("fanyideskweb" + e + i + "Ygy_4c=r#e#4EX^NUGUc5")
    }
};


"""


def p(text):
    print(text)
    t = int(time.time() * 1000)
    hl = hashlib.md5()
    i = str(t) + str(random.randint(0, 9))
    str_c = "fanyideskweb" + text + i + "Ygy_4c=r#e#4EX^NUGUc5"
    hl.update(str_c.encode(encoding='utf-8'))
    sign = hl.hexdigest()
    bv = hashlib.md5()
    bv.update(
        '5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
            .encode(encoding='utf-8'))
    bv = bv.hexdigest()
    data = {
        'i': text,
        'from': 'AUTO',
        'to': 'AUTO',
        'smartresult': 'dict',
        'client': 'fanyideskweb',
        'salt': i,
        'sign': sign,
        'lts': t,
        'bv': bv,
        'doctype': 'json',
        'version': '2.1',
        'keyfrom': 'fanyi.web',
        'action': 'FY_BY_REALTlME'
    }
    print(data)
    return data


def get_p(text):
    cookie = {
        'OUTFOX_SEARCH_USER_ID': '-412303195@10.112.57.88',
        'OUTFOX_SEARCH_USER_ID_NCOO': '257278283.1462334',
        '___rl__test__cookies': '{}'.format(int(time.time() * 1000))
    }
    header = {
        'User-Agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 105.0.0.0Safari / 537.36',
        'Referer': 'https://fanyi.youdao.com/'

    }
    my_json = requests.post('https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule',
                       data=p(text), headers=header, cookies=cookie).json()
    print(my_json['translateResult'][0][0]['tgt'])


if __name__ == '__main__':
    while True:
        str1 = input('请输入翻译的内容：')
        if str1 == "exit":
            break
        get_p(str1)