# Deepl 注册字符5000 测试失败
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from base import BaseCore
baseCore = BaseCore.BaseCore()
class Translate():
    def __init__(self):

        self.url = "https://www.deepl.com/translator"
        self.header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1"}

        self.chrome_driver = r'D:\cmd100\chromedriver.exe'
        self.path = Service(self.chrome_driver)
        self.chrome_options = webdriver.ChromeOptions()
        self.chrome_options.binary_location = r'D:\Google\Chrome\Application\chrome.exe'
        self.chrome_options.add_argument('--disable-gpu')
        self.chrome_options.add_argument('--ignore-certificate-errors')
        self.chrome_options.add_argument("--disable-blink-features=AutomationControlled")
        self.chrome_options.add_argument("--start-maximized")
        # chrome_options.add_argument(
        #     'user-agent=' + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
        # chrome_options.add_argument('--headless')
        self.browser = webdriver.Chrome(service=self.path, chrome_options=self.chrome_options)
        self.wait = WebDriverWait(self.browser, 20)

    def translate(self):
        word = input("Input your word: ")
        self.get_input_language_type(word)
        time.sleep(5)
        self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="headlessui-tabs-panel-7"]/div/div[1]/section/div/div[2]/div[3]/section/div[1]/d-textarea/div')))
        result = self.browser.find_element(By.XPATH, '//*[@id="headlessui-tabs-panel-7"]/div/div[1]/section/div/div[2]/div[3]/section/div[1]/d-textarea/div')
        print(result.text)
        return

    def get_input_language_type(self,word):
        self.browser.get("https://www.deepl.com/translator")
        time.sleep(2)
        self.browser.refresh()
        self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="headlessui-tabs-panel-7"]/div/div[1]/section/div/div[2]/div[1]/section/div/div[1]/d-textarea')))
        input_word = self.browser.find_element(By.XPATH, '//*[@id="headlessui-tabs-panel-7"]/div/div[1]/section/div/div[2]/div[1]/section/div/div[1]/d-textarea')
        input_word.send_keys(word)
        return


if __name__ == "__main__":
    # test = Translate()
    # test.translate()
    # test.browser.close()
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Content-Length': '914',
        'Content-Type': 'application/json',
        'Cookie': 'INGRESSCOOKIE=5af7d91f1e42627f47a49d13c8df6af3|a6d4ac311669391fc997a6a267dc91c0; userCountry=CN; releaseGroups=5418.AAEXP-4408.1.1_5421.AAEXP-4411.1.1_863.DM-601.2.2_2413.DWFA-524.2.4_2465.DWFA-636.2.3_4476.DWFA-695.2.2_5408.AAEXP-4398.2.1_975.DM-609.2.3_5410.AAEXP-4400.1.1_5424.AAEXP-4414.1.1_4121.WDW-356.1.3_4475.DWFA-693.2.2_5409.AAEXP-4399.2.1_5431.AAEXP-4421.1.1_5436.AAEXP-4426.1.1_5560.DWFA-638.2.1_976.DM-667.2.3_1577.DM-594.2.3_2459.TC-850.2.3_5422.AAEXP-4412.1.1_5425.AAEXP-4415.1.1_220.DF-1925.1.9_1119.B2B-251.2.4_5407.AAEXP-4397.1.1_5411.AAEXP-4401.2.1_5419.AAEXP-4409.1.1_3939.B2B-596.1.1_4297.DF-3763.2.2_4322.DWFA-689.2.2_1444.DWFA-362.2.2_1483.DM-821.2.2_2455.DPAY-2828.2.2_2964.DWFA-616.2.2_3283.DWFA-661.2.2_5412.AAEXP-4402.2.1_5428.AAEXP-4418.1.1_5435.AAEXP-4425.1.1_4478.SI-606.2.1_5375.WDW-385.2.1_5416.AAEXP-4406.2.1_866.DM-592.2.2_1583.DM-807.2.5_2962.DF-3552.1.5_3127.DM-1032.2.2_3613.WDW-267.2.2_5432.AAEXP-4422.1.1_5562.DWFA-732.1.1_5561.WDW-395.1.1_1780.DM-872.2.2_2345.DM-1001.2.2_2373.DM-1113.2.4_3614.DWFA-95.2.2_5420.AAEXP-4410.1.1_5433.AAEXP-4423.1.1_1571.DM-791.2.4_1585.DM-900.2.3_1997.DM-941.2.3_2973.DWFA-588.2.2_5426.AAEXP-4416.1.1_2393.DWFA-595.2.4_3788.TACO-41.2.2_5413.AAEXP-4403.2.1_5414.AAEXP-4404.2.1_5430.AAEXP-4420.1.1_5417.AAEXP-4407.1.1_5423.AAEXP-4413.1.1_2274.DM-952.2.2_2399.WDW-164.1.1_2499.DWFA-657.2.3_2974.DWFA-587.2.3_3585.ACL-523.1.1_2055.DM-814.2.3_2464.DM-1175.2.2_2656.DM-1177.2.2_5434.AAEXP-4424.1.1_5427.AAEXP-4417.1.1_2356.B2B-515.2.2_3586.DF-3635.1.4_4321.B2B-679.2.2_4828.TACO-68.2.3_4829.TACO-91.2.3_3587.DWFA-653.2.2_4831.WDW-341.2.2_4853.DF-3503.1.1_5376.WDW-360.2.1_5415.AAEXP-4405.2.1_2497.WDW-209.2.2_5429.AAEXP-4419.1.1; dapUid=c2d67210-6f35-4bc5-b9a1-40d512bbb878; dapVn=1; LMTBID=v2|27b68f9f-2191-450b-80fd-391db06237fc|bccba0a1bf17e78d03c14ada051c38bb; privacySettings=%7B%22v%22%3A%221%22%2C%22t%22%3A1704758400%2C%22m%22%3A%22LAX%22%2C%22consent%22%3A%5B%22NECESSARY%22%2C%22PERFORMANCE%22%2C%22COMFORT%22%2C%22MARKETING%22%5D%7D; __cf_bm=m_ZHpHuOYEKKo.9dwf6BGpiuajZvYkpB.XXH5YxuwtU-1704791184-1-AetRDdjia/Uo3ASo/W7XwDF9GDBTD9ysjZkssiUL5d2XR3gHF4cmFbldDC/k1TAxxwfiYoP4TAk4rTGw7QYkoXM=; dapSid=%7B%22sid%22%3A%2231048f73-f6fc-4804-8ffb-8897514b36e2%22%2C%22lastUpdate%22%3A1704791875%7D',
        'Origin': 'https://www.deepl.com',
        'Referer': 'https://www.deepl.com/',
        'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"Windows"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
    }
    text = '''In compliance with the provisions of the Financial Services '''
    aa = {"jsonrpc":"2.0","method": "LMT_handle_jobs","params":{"jobs":[{"kind":"default","sentences":[{"text":f"{text}","id":1,"prefix":" "}],"raw_en_context_before":[],"raw_en_context_after":[],"preferred_num_beams":4,"quality":"fast"}],"lang":{"target_lang":"ZH","preference":{"weight":{"DE":0.15016,"EN":5.99281,"ES":0.08125,"FR":0.12516,"IT":0.0273,"JA":0.03235,"NL":0.02295,"PL":0.01412,"PT":0.01891,"RU":0.01285,"ZH":0.32337,"BG":0,"CS":0.00495,"DA":0.00216,"EL":0,"ET":0.0067,"FI":0.0028,"HU":0.00912,"ID":0.00738,"LV":0.00172,"LT":0.00103,"RO":0.09677,"SK":0.00349,"SL":0.0124,"SV":0.01817,"TR":0.00596,"UK":0,"KO":0.00021,"NB":0.01005},"default":"default"},"source_lang_user_selected":"auto"},"priority":-1,"commonJobParams":{"mode":"translate","textType":"plaintext","browserType":1},"timestamp":1704791876600},"id":17330022}
    url = 'https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs'
    ip = baseCore.get_proxy()
    req = requests.post(url=url, headers=headers, data=aa, proxies=ip)
    bb = req.json()
    print(bb)
