import json
import multiprocessing
import random
import re
import threading
import time
import traceback
from datetime import datetime
from urllib import parse

import requests

from tools import loggerKit
from tools.utils import get_random_browser, FileWriter, get_random_proxy_at_redis, get_external_ip


def poc_2(account, passwd, target_url, log_name, single_proxy):
    # 生成 5 到 10 秒之间的随机等待时间
    wait_time = random.uniform(5, 10)
    # 打印等待时间并等待
    loggerKit.info(f"等待 {wait_time:.2f} 秒...")
    time.sleep(wait_time)
    # 开始业务逻辑
    lock = threading.Lock()
    with lock:

        loggerKit.info(f'account: {account}, passwd: {passwd}')
        writer = FileWriter(log_name)
        writer.write_to_file(f'------{account}, {passwd}------ \n')
        user_agent = get_random_browser()
        # # 随机数分页
        # page = random.randint(1, 5)
        # page_size = random.randint(50, 100)
        # single_proxy = get_proxy_ip(page, page_size)
        # single_proxy = 'rrehqcjf:33f90umk8x32@185.199.228.220:7300'
        proxies = {
            'http': f'http://{single_proxy}',
            # 'https': f'https://{single_proxy}'
        }

        loggerKit.info(f'proxies: {proxies}')
        writer.write_to_file(f'account: {account}, passwd: {passwd}, proxies:{proxies} \n')

        # session
        """
        模拟登录
        """
        # 定义请求地址
        login_url = "https://t.livepocket.jp/api/sessions/create?mytimestamp=" + str(int(time.time()))

        # 定义请求header
        headers = {'Content-Type': 'application/x-www-form-urlencoded;',
                   'Referer': 'https://t.livepocket.jp/login?acroot=header-new_p_u_nl',
                   # 'Sec-Ch-Ua': 'Chromium',
                   'User-Agent': user_agent
                   }

        # 通过字典方式定义请求body
        form_data = {"login": account,
                     "password": passwd,
                     "auto_login": "on",
                     "login_password": f"{account}&{passwd}"
                     }

        data = parse.urlencode(form_data)
        session = requests.session()
        content = session.post(url=login_url, headers=headers, data=data, proxies=proxies).text
        # loggerKit.info(f'login info: {content}')
        if 'token' not in content:
            loggerKit.info(f'login account:{account}, 本机出口IP:{get_external_ip()} 被封!!!')
            return
        cookie = session.cookies
        response_cookie = cookie.get_dict()
        login_resp = json.loads(content)
        token = login_resp['result']['token']
        login_session = response_cookie['ci_session']
        php_session = response_cookie['PHPSESSID']

        # event_id
        event_id = 0
        response = requests.get(target_url, proxies=proxies)
        if response.status_code == 200:
            html_string = response.content.decode('utf8')
            start_index = html_string.find('https://t.livepocket.jp/purchase/verify?event_id=')
            if start_index != -1:
                end_index = html_string.find("'", start_index)
                if end_index != -1:
                    event_id = html_string[start_index + len('https://t.livepocket.jp/purchase/verify?event_id='):end_index]
                    print(f'event_id: {event_id}')
                else:
                    print('End index not found.')
            else:
                print('Substring not found.')
        else:
            print('Failed to fetch the page. Status code:', response.status_code)

        # ticket_id
        ticket_id = 0
        response = requests.get(target_url, proxies=proxies)
        if response.status_code == 200:
            html_string = response.text
            match = re.search(r'id="js_order_limited_(\d+)"', html_string)
            if match:
                ticket_id = match.group(1)
                print(ticket_id)
            else:
                print("未找到匹配的数值")

        # event_cname
        # url = 'https://t.livepocket.jp/e/lxyyc'
        sub_str = target_url.replace("https://t.livepocket.jp/", "")
        strs = sub_str.split('/')
        cname = strs[len(strs) - 1]
        # print(cname)

        # redirect_url1
        net_url = 'https://t.livepocket.jp/purchase?type=new'
        result = requests.Session()
        header = {
            'Host': 't.livepocket.jp',
            'User-Agent': "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
                          "Chrome/69.0.3947.100 Safari/537.36",
            'Content-Type': 'application/x-www-form-urlencoded',
            'Referer': f'{target_url}',
        }
        # my_session = 'IN7u3uuP5WcizQkmRzF%2F3OsLuNNQePnxNBvnlPgqT6FPz2BgyKT2KCJaMvdj8ZSa6wNp2xZIL6VoA7Mj0R2zECMZkOKJOLbsNwUdXwvcsROxjfCzibxEaj4nG%2Bq29dCEUdiWI2TgUsKSFGRZaOP0p37ktl%2B1wSMqMAk918Nkt7APIxpQtZ%2BoLwId4PMOkN3oOIwT3CvsuDbgPQHIuokZXcnNe8uOAUuZBLW4nKOdYimLcSw6oTMt85UXkwm4OyTdxYR3%2B8crA0OfQCyfHXqY%2Bj0t9R0mmNSusRXRDuDBmOCFR58vHVuuwZ0AXNHYoYB0sdm28VL9xgHHrzkvm2M9cpLEIVWUWBWGaZgtO3xp2d8L70%2BfAKwyv6JPGcGmGf9wUQzBltssBpMmYqSfkws1%2Bp8BOhmXOqfljSYbWcxxVVSw%2B2dtoxBXsTf793mD9sVYRsrr8YPGz4JPVGWGU0outg%3D%3D'
        cookies = {
            'ci_session': f'{login_session}',
            'PHPSESSID': f'{php_session}',
            'list_count': '{"success":true,"result":{"myticket_count":{"count":"0"},"today_event":{"total_count":0,"data_list":[]},"unread_count":5},"submit":true}',
            'sns_status': '{"success":true,"result":{"facebook":0,"twitter":0,"mixi":0,"line":0,"yahoo":0,"plusid":1,"google":0},"submit":true}',
            'display_init': '{"success":true,"result":{"purchased_tickets":{"total_count":0,"data_list":{"ticket_info":[]}},"lottery_tickets":{"total_count":0,"data_list":{"ticket_info":[]}},"order_limited_event_tickets":{"data_list":{"ticket_info":[]},"total_count":0},"event_order_limit":true},"submit":true}',
        }

        ticket_key = f'ticket_id_{ticket_id}'
        form_data = {
            'redirect_url': 'https://t.livepocket.jp/purchase/',
            'event_id': event_id,
            'event_cname': f'{cname}',
            'ticket_type': 'lottery',
            'facebook_ticket_count': 0,
            'twitter_ticket_count': 0,
            'plusid_linkage_invalidation_flg': 0,
            ticket_key: 1
        }

        print('form_data:', form_data)
        response = result.post(net_url, headers=header, cookies=cookies, data=form_data, allow_redirects=False,
                               proxies=proxies)
        print('response.headers: ', response.headers)
        if 'Location' in response.headers:
            redirect_url1 = response.headers['Location']
            print('redirect_url1: ', redirect_url1)
        else:
            return

        # https://t.livepocket.jp/purchase/security?id=309887&type=new
        # redirect_url2
        response2 = result.get(redirect_url1, headers=header, cookies=cookies, allow_redirects=False, proxies=proxies)
        print('response2.headers: ', response2.headers)
        if 'Location' in response2.headers:
            redirect_url2 = response2.headers['Location']
            print('redirect_url2: ', redirect_url2)
        else:
            return

        # https://t.livepocket.jp/purchase/confirm?id=309887&reserved_session_id=423272
        # redirect_url3
        response3 = result.get(redirect_url2, headers=header, cookies=cookies, allow_redirects=False, proxies=proxies)
        print('response3.headers: ', response3.headers)
        if 'Location' in response3.headers:
            redirect_url3 = response3.headers['Location']
            print('redirect_url3: ', redirect_url3)
        else:
            print('redirect_url3 is None')
            return

        # https://t.livepocket.jp/purchase/confirm?id=309887&reserved_session_id=794986&otoken=mMyZTgzMmQ4OWQ5ZjY5NWFlNGE2ZmJlMWM5Mm
        # drawing_tickets
        drawing_url = 'https://t.livepocket.jp/api/drawing_tickets/entry?mytimestamp=' + str(int(time.time()))

        region_string = redirect_url3.split('?')[1]
        params = region_string.split('&')
        reserve_session_id = params[1].split("=")[1]

        drawing_headers = {
            'Host': 't.livepocket.jp',
            'User-Agent': user_agent,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Referer': f'{redirect_url3}',
        }
        drawing_cookies = {
            'ci_session': f'{login_session}',
            'PHPSESSID': f'{php_session}',
        }
        drawing_form_data = {
            'utoken': f'{token}',
            'onetime_token_name': 'buy_ticket',
            'onetime_token_value': params[2].split('=')[1],
            'url': f'https://t.livepocket.jp/purchase/confirm?id={event_id}&reserved_session_id={reserve_session_id}',
            'payment_method': 0,
            'reserve_session_id': reserve_session_id,
            'event_id': event_id,
            'reserve_ticket[0][ticket_id]': ticket_id,
            'reserve_ticket[0][amount]': 1,
            'payment_type': 'credit'
        }
        drawing_response = result.post(drawing_url, headers=drawing_headers, cookies=drawing_cookies,
                                       data=drawing_form_data, allow_redirects=False, proxies=proxies)
        print('drawing_response.text: ', drawing_response.text)
        drawing_data = json.loads(drawing_response.text)
        if drawing_data['success']:
            order_id = drawing_data['result']['order_id']
            onetime_token_name = drawing_data['result']['onetime_token_name']
            onetime_token_value = drawing_data['result']['onetime_token_value']
            print('account, order_id, onetime_token_name, onetime_token_value', account, order_id, onetime_token_name,
                  onetime_token_value)

            writer.write_to_file(
                f'account:{account}, order_id:{order_id}, onetime_token_name:{onetime_token_name}, onetime_token_value:{onetime_token_value} \n')


def process_account(account, log_name, single_proxy):
    try:
        run_poc(account.strip(), 'panyue666', 'https://t.livepocket.jp/e/0vapa', log_name, single_proxy)
    except Exception as ex:
        print(f'error: {traceback.format_exc()}, account:{account}')


def process_running(max_threads):
    log_name = f"data_{datetime.now().strftime('%Y%m%d%H%M')}.txt"
    # 设置日志配置
    # logging.basicConfig(filename=log_name, level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
    with open('account.txt', 'r') as accounts:
        account_list = accounts.readlines()

    threads = []
    for account in account_list:
        # 随机数分页
        # page = random.randint(1, 5)
        # page_size = random.randint(10, 50)
        # single_proxy = get_proxy_ip(page, page_size)

        # 直接从redis中获取
        single_proxy = get_random_proxy_at_redis(100)
        thread = threading.Thread(target=process_account, args=(account, log_name, single_proxy))
        threads.append(thread)
        thread.start()

        # 如果当前线程数达到上限,等待有线程结束后再创建新线程
        if len(threads) >= max_threads:
            for t in threads:
                t.join()
            threads.clear()

    # 等待所有线程执行完毕
    for thread in threads:
        thread.join()


def run_poc(account, password, url, log_name, single_proxy):
    try:
        poc_2(account, password, url, log_name, single_proxy)
    except Exception as e:
        print(f'error: {traceback.format_exc()}, account:{account}')


if __name__ == '__main__':
    # 新增下面一行代码即可打包多进程
    multiprocessing.freeze_support()
    # log_name = f"data_{datetime.now().strftime('%Y%m%d%H%M')}.txt"
    # # 设置日志配置
    # logging.basicConfig(filename=log_name, level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

    # with open('account.txt', 'r') as accounts:
    #     for account in accounts:
    #         try:
    #             poc_2(account.strip(), 'panyue666', 'https://t.livepocket.jp/e/cx591', log_name)
    #         except Exception as e:
    #             print(f'error: {e}, account:{account}')
    while 1:
        process_running(max_threads=6)

# if __name__ == '__main__':
#     multiprocessing.freeze_support()
#     log_name = f"data_{datetime.now().strftime('%Y%m%d%H%M')}.txt"
#
#     # 读取 accounts.txt 文件中的账号数量
#     with open('account.txt', 'r') as accounts:
#         account_count = sum(1 for _ in accounts)
#
#     # 创建线程池
#     threads = []
#     with open('account.txt', 'r') as accounts:
#         for account in accounts:
#             account = account.strip()
#             t = threading.Thread(target=run_poc,
#                                  args=(account, 'panyue666', 'https://t.livepocket.jp/e/cx591', log_name))
#             t.start()
#             threads.append(t)
#
#             # 如果线程数达到账号数,则等待所有线程执行完毕
#             if len(threads) == account_count:
#                 for t in threads:
#                     t.join()
#                 threads.clear()
