import random
import re
import sys
import threading
import time
from enum import verify
from pathlib import Path

import execjs
import requests
from DrissionPage import Chromium, ChromiumOptions
from loguru import logger

from app.utils.common import get_all_cookies
from app.utils.ydm_verify import YdmVerify

chrome_opts = ChromiumOptions()
chrome_opts.mute(True)  # 静音
chrome_opts.no_imgs(False)
chrome_opts.set_argument("--disable-gpu")
chrome_opts.set_argument('--ignore-certificate-errors')
chrome_opts.set_argument("--proxy-server=http://127.0.0.1:7890")
# chrome_opts.incognito(True)
chrome_opts.set_browser_path(r"C:\Program Files\Google\Chrome\Application\chrome.exe")
chrome_opts.auto_port()
browser = Chromium(addr_or_opts=chrome_opts)

# tab = browser.new_tab()
# tab.listen.start([
#     "passport.baidu.com/cap/init",
#     "passport.baidu.com/cap/style",
# ])
# tab.get("https://wappass.baidu.com/static/captcha/tuxing_v2.html?&logid=10332554090053311096&ak=c27bbc89afca0463650ac9bde68ebe06&backurl=https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3Dsite%253Altxbbs.com%26pn%3D50%26oq%3Dsite%253Altxbbs.com%26ct%3D2097152%26ie%3Dutf-8%26si%3Dltxbbs.com%26fenlei%3D256%26rsv_idx%3D1%26rsv_pq%3D99cae74f0003cd72%26rsv_t%3Dab2dk%252Fq4PohUCmoLbyMlEMrGJszk983ojkNLk%252FUiZGJ4ZLpwvZ46PtQUufk%26gpc%3Dstf%253D1741437499%252C1744115898%257Cstftype%253D1%26tfflag%3D1%26topic_pn%3D%26rsv_page%3D1&ext=x9G9QDmMXq%2FNo87gjGO0P1dyBXu4PagAZrreQL6%2Bticsr0rrDszYO2sAbAnT1vLIUgqUK9LXd1cIlztrhMwiv3XfcB99Y5gyF0c0ETsDFDls5CsGNJQRLPawcntn2ndVLHHLl46IaoOp8l%2FC1xtOHwMQi85PCzAojcSf2wQ76KRxVau99LtSYCIfwtv7By0w&signature=f2fbb1b81926e247835f69195661a06b&timestamp=1744115910")
# for pkg in tab.listen.steps():
#     print(f"{pkg.url=}")
#     print(f"{pkg.response.raw_body=}")
# current_path = Path(__file__).resolve()
# print(current_path)
# current_dir = current_path.parent.parent
# print(current_dir)
# js_path = current_dir.joinpath("./js/mkd_v2_link_submit.js")
# print(js_path.exists())

# with open("./js/mkd_v2_link_submit.js", "r", encoding="utf-8") as f:
#     ds_js = f.read()
#

proxy_str = "http://127.0.0.1:7890"
headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,zh-TW;q=0.6',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    # 'Origin': 'https://jubao.baidu.com',
    'Pragma': 'no-cache',
    'Referer': "https://wappass.baidu.com/",
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0",
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua_wap': '"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"',
    'sec-ch-ua_wap-mobile': '?0',
    'sec-ch-ua_wap-platform': '"Windows"',
    "Cookie": "BDUSS=ldlSDMwdkg5VmlrbE5TZFdHUHVhWEFCTVNqcGtKZHhXeTNaTHFGZHY4Y3F5LVJiQVFBQUFBJCQAAAAAAAAAAAEAAADj3ycY1tC5zNXywO4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACo-vVsqPr1bS; PHPSESSID=f364o6o7tpsag92pd67630p870; lastIdentity=PassUserIdentity; BAIDUID=5C7396A6BE9E28B769E6E9815A1B8D5E:FG=1; BAIDUID_BFESS=5C7396A6BE9E28B769E6E9815A1B8D5E:FG=1; BDUSS_BFESS=ldlSDMwdkg5VmlrbE5TZFdHUHVhWEFCTVNqcGtKZHhXeTNaTHFGZHY4Y3F5LVJiQVFBQUFBJCQAAAAAAAAAAAEAAADj3ycY1tC5zNXywO4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACo-vVsqPr1bS",
}

tab = browser.new_tab()
tab.listen.start(r"/cap/(init|style|log)", is_regex=True)
tab.get("https://www.baidu.com")

captcha_data = {}


def listener():
    for pkg in tab.listen.steps():
        if "/cap/init" in pkg.url:
            captcha_data["init"] = pkg.response.body
        if "/cap/style" in pkg.url:
            captcha_data["style"] = pkg.response.body
            captcha_data["referer"] = pkg.request.headers.get("Referer")
            logger.debug(f"正确的 referer: {captcha_data["referer"]}")
            captcha_data["cookie"] = pkg.request.headers.get("Cookie")
            logger.debug(f"cookie: {captcha_data['cookie']}")
        if "/cap/log" in pkg.url:
            captcha_data["log"] = pkg.response.body


thread = threading.Thread(target=listener, daemon=True)
thread.start()


def verify_captcha(current_url: str):
    headers["Referer"] = captcha_data["referer"]
    headers["Cookie"] = captcha_data["cookie"]

    # 解出AS / TK
    as_value = captcha_data["init"]["data"]["as"]
    tk_value = captcha_data["init"]["data"]["tk"]
    # logger.debug(f"{as_value=}, {tk_value=}")
    # ts = time.time()
    # ts1 = int(ts)
    # ts2 = int(ts * 1000)
    # response = requests.post(
    #     "https://passport.baidu.com/cap/init",
    #     data={
    #         "_": ts2,
    #         "refer": re.sub(r'timestamp=\d+', f'timestamp={ts1}', captcha_data["referer"]),
    #         "ak": "c27bbc89afca0463650ac9bde68ebe06",
    #         "ver": "2",
    #         "scene": "",
    #         "ds": "",
    #         "tk": "",
    #         "as": "",
    #         "reinit": 0
    #     },
    #     headers=headers,
    #     proxies={
    #         "http": proxy_str, "https": proxy_str
    #     }
    # ).json()
    # as_value = response["data"]["as"]
    # tk_value = response["data"]["tk"]
    logger.debug(f"{as_value=}, {tk_value=}")

    # 解出 style
    backstr = captcha_data["style"]["data"]["backstr"]
    captcha_link = captcha_data["style"]["data"]["captchalist"][0]["source"]["back"]["path"]
    # response = requests.post(
    #     "https://passport.baidu.com/cap/style",
    #     data={
    #         "_": int(time.time() * 1000),
    #         "refer": re.sub(r'timestamp=\d+', f'timestamp={ts1}', captcha_data["referer"]),
    #         "ak": "c27bbc89afca0463650ac9bde68ebe06",
    #         "tk": tk_value,
    #         "scene": "",
    #         "isios": "0",
    #         "type": "spin",
    #         "ver": "2"
    #     },
    #     headers=headers,
    #     proxies={
    #         "http": proxy_str, "https": proxy_str
    #     }
    # )
    # logger.debug(f"{response.content=}")
    # response = response.json()
    # backstr = response["data"]["backstr"]
    # captcha_link = response["data"]["captchalist"][0]["source"]["back"]["path"]
    logger.debug(f"{backstr=}, {captcha_link=}")

    # 下载验证码图片
    image_response = requests.get(captcha_link, headers=headers)
    with open("captcha.png", "wb") as f:
        f.write(image_response.content)
    logger.debug("download captcha.png")

    # 识别验证码
    ydm = YdmVerify()
    with open("captcha.png", "rb") as fp:
        picture = fp.read()

    slide_distance = ydm.rotate(picture)
    logger.debug(f"{slide_distance=}")
    if not slide_distance:
        logger.error("识别验证码失败")
        return None
    rotate_angle_rate = round(slide_distance / 360, 2)
    logger.debug(f"{rotate_angle_rate=}")

    if not rotate_angle_rate:
        logger.debug("识别验证码失败")
        return None

    # 发送验证码请求
    time_log = str(int(time.time() * 1000))
    with open("./js/mkd_v2_link_submit.js", 'r', encoding='utf-8') as f:
        ds_js = f.read()
    fs = execjs.compile(ds_js).call('getFs2', backstr, rotate_angle_rate, as_value)
    data = {
        "_": time_log,
        "refer": captcha_data["referer"],
        # "refer": "https://aigc.baidu.com/works",
        # "ak": self.get_ak(),
        "ak": "c27bbc89afca0463650ac9bde68ebe06",  # c27bbc89afca0463650ac9bde68ebe06
        # "ak": "76AKmP4xDQjB3vAIPef3KxOlJZWCpw64",  # c27bbc89afca0463650ac9bde68ebe06
        "as": as_value,
        "scene": "",
        "tk": tk_value,
        "ver": "2",
        "cv": "submit",
        "typeid": "spin-0",
        # fuid 短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r4J4agZi377iM/izL6cVCGRy2F8c0VpEvM5FjnYxYstXg/9EfB3EVmKAfzNRIeToJ5YV9twMcgdmlV1Uhhp5FAe6gNJIUptp7EMAaXYKm11G+JVPszQFdp9AJLcm4YSsYUXkaPI2Tl66J246cmjWQDTahAOINR5rXR5r/7VVI1RMZ8gb40q7az7vCK56XLooKT5a+rsFrf5Zu0yyCiiagElhrTEOtNdBJJq8eHwEHuFBni9ahSwpC7lbKkUwaKH69tf0DFV7hJROiLETSFloIVkHdy3+I2JUr1LsplAz0hMkWt/tE4tXVUV7QcTDTZWS/2mCoS/GV3N9awQ6iM6hs/BWjlgnEa1+5iP7WSc7RJ34FaE5PsyGXyoCWdXwNRGSZPSvVtB/Ea6w5FKazXcZ/j40FJv+iLGBn3nkkgHlne61I8I7KhtQgIkmBMJIjPMkS/L051MeqdGScsKYTJuSucgI5c3+79eVH+y2TvbOTuuHv1uGxwXFb2atIU1ZYPbmmXculmizKcKI/s44qf8uM8iBZLGkKeVyL74aPyLkg7Gk359g98BIGN/ZzJR/h+Y6AyFx+HlMoYJnS06dVmqFbvlCtSdGylKQ5f8eWtxPkJGqOFtWjIVteQYMsH/AaSJonqw+WLiZvGjYfm9p0alEyujapoTy77HzDcUoU1wUSXa5xS/Z6hXEr2OnLi0LdPVcGjz8lpLcdVeSfm9p0alEyujapoTy77HzDWf5PERRSTFqLd9BTUHLyY4Ji3EQLGQPaM1aeHxG1bJZH0s1Si/KwzTaTYzu6ziQiqwcr2kaYUiH+fMOxn69/BhNJVMhpQkhprc1KZuJRvXjppq0gKweencPxgS/jd0rjw==",
        "fs": fs
    }
    # logger.info(data)

    response = requests.post(
        "https://passport.baidu.com/cap/log",
        headers=headers,
        data=data,
        proxies={"http": proxy_str, "https": proxy_str},
    ).json()
    try:
        result = {
            "ds": response["data"]["ds"],
            "op": response["data"]["op"],
            "tk": response["data"]["tk"]
        }
    except KeyError:
        logger.error(f"验证码没转成功, response: {response=}")
        time.sleep(1)
        return None
    logger.debug(f"{result=}")

    # 检查验证码是否正确
    if result["op"] != 1:
        logger.error(f"op != 1, 重试")
        return None

    # 发送验证码请求 /cap/c 请求，获取待跳转的URL
    response = requests.post(
        "https://passport.baidu.com/cap/c?ak=c27bbc89afca0463650ac9bde68ebe06",
        headers=headers,
        json={
            "tk": result["tk"],
            "ds": result["ds"],
            "qrsign": "",
            "refer": captcha_data["referer"]
        },
        proxies={"http": proxy_str, "https": proxy_str},
    )

    data = response.json()
    if data["data"].get("f"):
        logger.error(f"验证码失败: {data['data'].get('f')}")
        return None
    if data["data"].get("s"):
        logger.debug("验证成功，URL：" + data["data"].get("s").get("url"))
        url = data["data"].get("s").get("url")
        url = url.encode("utf-8").decode("unicode-escape")
        logger.success("解码后的URL：" + url)
        return url


current_page = 1
while current_page < 15:
    tab.get(f"https://www.baidu.com/s?wd=site%3Abaidu.com&pn={(current_page - 1) * 10}")
    current_page += 1
    if "wappass.baidu.com/static/captcha/tuxing_v2.html" in tab.url:
        logger.debug("captcha!!!!")
        time.sleep(2)

        idx = 0
        while idx < 3:
            idx += 1
            url = verify_captcha(tab.url)
            if not url:
                tab.refresh()
                time.sleep(3)
            else:
                tab.get(url)

        time.sleep(30)

logger.debug(f"{captcha_data=}")
# browser.quit()
