import requests
import re
import json
from loguru import logger

headers = {
    'authority': 'premier.hkticketing.com',
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
    'cache-control': 'max-age=0',
    'referer': 'https://premier.hkticketing.com/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
}

response = requests.get('https://premier.hkticketing.com/', headers=headers,
                        proxies={'https': 'http://127.0.0.1:10809'})
if "window.rbzns" in response.text:
    rbzns_data = re.findall("window.rbzns=(.*?);winsocks", response.text)[0]
    rbzns_data = json.loads(rbzns_data)
    result = requests.post("https://akm.sakura-luo.top/rbzid?token=xxxx", json=rbzns_data).json()
    logger.debug(f"接口结果:{result}")
    url = result.pop("url")
    headers.update(result)

    response = requests.get(
        url,
        headers=headers,
        proxies={'https': 'http://127.0.0.1:10809'}
    )
    rbzid = response.cookies.get("rbzid")
    logger.success(f"rbzid:{rbzid}")

    headers = {
        'authority': 'premier.hkticketing.com',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'zh-CN,zh;q=0.9,en-GB;q=0.8,en-US;q=0.7,en;q=0.6',
        'cache-control': 'max-age=0',
        'referer': 'https://premier.hkticketing.com/',
        'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
    }

    response = requests.get('https://premier.hkticketing.com/', headers=headers,
                            cookies={"rbzid": rbzid},
                            proxies={'https': 'http://127.0.0.1:10809'})
    print(response.text)
else:
    if "ROBOTS" in response.text:
        logger.warning("出的是 84,请看84的文档")
    else:
        logger.warning("没有出 rbzid")