import threading
from msedge.selenium_tools import Edge
from msedge.selenium_tools import EdgeOptions

from dcs.tests.zhiwang import *
from loguru import logger


def translate(word):
    url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    data = {'i': word,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTIME',
            'typoResult': 'false'}
    r = requests.post(url, data)
    answer = r.json()
    result = answer['translateResult'][0][0]['tgt']
    return result


def crawl_zhiwang(word, pages_start=2, pages_end=3):
    edge_options = EdgeOptions()
    edge_options.use_chromium = True
    edge_options.add_argument('headless')
    driver = Edge(options=edge_options, executable_path=r'G:\Users\god\PycharmProjects\dcs\bin\msedgedriver.exe')
    soup = driver_open(driver, word)  # 搜索word
    papers = []  # 用于保存爬取到的论文
    spider(driver, soup, papers)
    for pn in range(pages_start, pages_end):
        content = change_page(driver, pn)
        spider(driver, content, papers)
    driver.close()


class Spider(threading.Thread):
    def __init__(self, word: str):
        super().__init__()
        self.word = word
        self.daemon = True
        pass

    def run(self) -> None:
        logger.info('crawling...')
        crawl_zhiwang(word=self.word)
