"""
购买券
1、https://t.livepocket.jp/api/tickets/purchase?mytimestamp=1714488081955

2、https://t.livepocket.jp/api/promoter_notification_settings/update?mytimestamp=1714488083447

3、https://t.livepocket.jp/api/common/lists?dev=sp&mytimestamp=1714488092578

4、https://t.livepocket.jp/api/purchases/show?order_id=56707983&mytimestamp=1714488092592

5、https://t.livepocket.jp/api/tickets/purchase?mytimestamp=1714525133395

"""
import json
import random
import time

import requests


def step_buy():
    step_timestamp = str(int(time.time()))
    event_id = 312901
    reserved_session_id = random.randint(10000, 60000)

    headers = {
        'Accept-Encoding': 'gzip, deflate, br, zstd',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Dnt': '1',
        'Pragma': 'no-cache',
        'Priority': 'u=0, i',
        'Referer': f'https://t.livepocket.jp/purchase/confirm?id={event_id}&reserved_session_id={reserved_session_id}&otoken=zIxMWI1OTU5NzFhMzBmMTc3NGIxNmMxMWJkNz',
        'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"macOS"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'authority': 't.livepocket.jp',
        'method': 'POST',
        'path': f'/api/tickets/purchase?mytimestamp={step_timestamp}',
        'scheme': 'https',
        'X-Requested-With': 'XMLHttpRequest',
    }

    cookies = {
        'ci_session': 'xvPftOucrr%2FKm8QK8rm%2FYMqQevVyEzdeVR%2BXweTNG3GqeftmbsU48MUFg6DsNW1L8%2FLMO5CcXwfxX5o7%2FV4GcqY7cHv24bfWoL0YLdDaE0NCQJi4H08YEco3cJH%2F1VSYdA1EphBT0BgWXRwfL0SRmCe6b%2FvYGiyBXHqT1Av%2BRi35TI3WGcL5kyBll8DXaZdL6%2FpSLb8H%2Bqn0oT%2BSk2sQ5ReyKS4oanPTOL6q6kuARaDAg9opO3iZpC6NVqqToQZoiHemQYYV9YL9IFUTLIullRcw5bu%2FgbTxgkyFOzjKoiMSF69zdJDDv4lZ3vM9dxHzOS8iJ1fInQoQIq7Z28WlGxeHHbFgfcrpf4oqhrtxvi2nTqq9%2Bkb3mx80sPkrZwPsBexeAIobAHhtgc5VuCXWenhGqdpy%2F2AAyNladWQOZxmR%2FzHA4A6EQ75Krgi9ekzL4tzx1uriBm%2BEP5Ng5e4qIQ%3D%3D'}

    step1_url = 'https://t.livepocket.jp/api/tickets/purchase?mytimestamp=' + str(int(time.time()))

    form1_data = {
        'utoken': 'utoken',
        'onetime_token_name': 'buy_ticket',
        'onetime_token_value': 'onetime_token_value',
        'url': f'https://t.livepocket.jp/purchase/confirm?id={event_id}&reserved_session_id={reserved_session_id}',
        'reserve_session_id': f'{reserved_session_id}',
        'payment_method': '0',
        'event_id': f'{event_id}',
        'payment_type': 'credit'
    }

    step1_session = requests.session()
    step1_content = step1_session.post(url=step1_url, headers=headers, data=form1_data, cookies=cookies).text
    step1_json = json.loads(step1_content)
    print(f"step1_content:{step1_content}, error: {step1_json['errmsg']}")


def purchase():
    event_id = 312901
    reserved_session_id = 739202
    buy_timestamp = str(int(time.time()))
    url = 'https://t.livepocket.jp/api/tickets/purchase?mytimestamp=' + buy_timestamp

    headers = {
        'authority': 't.livepocket.jp',
        'method': 'POST',
        'path': f'/api/tickets/purchase?mytimestamp={buy_timestamp}',
        'scheme': 'https',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, br, zstd',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cookie': 'ci_session=xvPftOucrr%2FKm8QK8rm%2FYMqQevVyEzdeVR%2BXweTNG3GqeftmbsU48MUFg6DsNW1L8%2FLMO5CcXwfxX5o7%2FV4GcqY7cHv24bfWoL0YLdDaE0NCQJi4H08YEco3cJH%2F1VSYdA1EphBT0BgWXRwfL0SRmCe6b%2FvYGiyBXHqT1Av%2BRi35TI3WGcL5kyBll8DXaZdL6%2FpSLb8H%2Bqn0oT%2BSk2sQ5ReyKS4oanPTOL6q6kuARaDAg9opO3iZpC6NVqqToQZoiHemQYYV9YL9IFUTLIullRcw5bu%2FgbTxgkyFOzjKoiMSF69zdJDDv4lZ3vM9dxHzOS8iJ1fInQoQIq7Z28WlGxeHHbFgfcrpf4oqhrtxvi2nTqq9%2Bkb3mx80sPkrZwPsBexeAIobAHhtgc5VuCXWenhGqdpy%2F2AAyNladWQOZxmR%2FzHA4A6EQ75Krgi9ekzL4tzx1uriBm%2BEP5Ng5e4qIQ%3D%3D',
        'Dnt': '1',
        'Origin': "https://t.livepocket.jp",
        'Pragma': 'no-cache',
        'Priority': 'u=1, i',
        'Referer': f'https://t.livepocket.jp/purchase/confirm?id={event_id}&reserved_session_id={reserved_session_id}&otoken=WU1OGQ1NDgxMzA3MzM5MDUwM2JmODA0ZjE3Yj',
        'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"macOS"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }

    form_data = {
        'utoken': 'VnpLJuTk66deUZmE5mam7JLGPmoHl0Mp24p9yURh',
        'onetime_token_name': 'buy_ticket',
        'onetime_token_value': 'WU1OGQ1NDgxMzA3MzM5MDUwM2JmODA0ZjE3Yj',
        'url': f'https://t.livepocket.jp/purchase/confirm?id={event_id}&reserved_session_id={reserved_session_id}',
        'reserve_session_id': f'{reserved_session_id}',
        'payment_method': '0',
        'event_id': f'{event_id}',
        'payment_type': 'credit',
        'order_id': '',
        'security_code': '',
        'serial_codes': 'null',
        'fan_club': 'null',
        'use_discount_id': '',
        'use_discount_code_id': ''
    }

    step1_session = requests.session()
    response = step1_session.post(url=url, headers=headers, data=form_data)

    print(response.request.headers)
    print(response.request.body)
    step1_json = json.loads(response.text)
    print(f"step1_content:{response.text}, error: {step1_json['errmsg']}")


def get_page_content():
    url = 'https://t.livepocket.jp/purchase/confirm?id=312901'

    response = requests.get(url)

    print(response.text)


if __name__ == "__main__":
    purchase()
