import json
import random
from pprint import pprint

import execjs
from curl_cffi import requests


class Gee():
    def __init__(self):
        self.session = requests.Session(impersonate='chrome110')
        self.session.verify = False
        self.session.proxies = {
            'http': 'http://127.0.0.1:9000',
            'https': 'http://127.0.0.1:9000'
        }
        self.ctx = execjs.compile(open('./src.js').read())

        self.cts = '%2f9XNtvh%2b8sFajJwQz8jmN%2f%2faHuZXcLYTqMdydRu5uHE07VQmDkmT%2batCVVo%2fb%2fNi'
        pass

    def init(self):
        headers = {
            "authority": "makeabooking.flyscoot.com",
            "accept": "*/*",
            "accept-language": "en",
            "cache-control": "no-cache",
            "pragma": "no-cache",
            "referer": "https://makeabooking.flyscoot.com/_Incapsula_Resource?SWUDNSAI=31&xinfo=5-105210083-0%20NNNY%20RT%281704432742576%20659%29%20q%280%20-1%20-1%203%29%20r%280%20-1%29%20B12%2814%2c0%2c0%29%20U18&incident_id=1160001190813994914-755464947643194821&edet=12&cinfo=0e0000000628&rpinfo=0&cts=G%2f%2b%2fkZktndgiZArS%2fSFP%2b2saKar%2bLqbKCyUlpPSoyd%2fP1gM8zgmubGYfRB%2fLJIzM&mth=GET",
            "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
            "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/120.0.0.0 Safari/537.36"
        }
        url = "https://makeabooking.flyscoot.com/_Incapsula_Resource?SWCNGEEC=" + self.cts

        response = self.session.get(url, headers=headers)

        response = self.session.get(url, headers=headers)
        print(response.text)
        self.challenge = response.json()["challenge"]
        self.gt = response.json()["gt"]
        headers = {
            "Accept": "*/*",
            "Accept-Language": "en",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Pragma": "no-cache",
            "Referer": "https://makeabooking.flyscoot.com/",
            "Sec-Fetch-Dest": "script",
            "Sec-Fetch-Mode": "no-cors",
            "Sec-Fetch-Site": "cross-site",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\""
        }
        url = "https://api.geetest.com/gettype.php"
        params = {
            "gt": self.gt,
            "callback": "geetest_1704432773301"
        }

        response = self.session.get(url, headers=headers, params=params)
        data = json.loads(response.text.replace('geetest_1704432773301', '')[1:-1])
        pprint(data)
        return data

    def start(self):
        data = self.init()
        self.step1(data)
        self.get_pic()
        validate = self.click()
        if validate:
            self.verify()

    def step1(self, data):
        data = {
            "gt": self.gt,
            "challenge": self.challenge,
            "lang": "en",
            "offline": False,
            "new_captcha": True,
            "width": "100%",
            "product": "popup",
            "protocol": "https://",
            "type": "fullpage",
            "static_servers": ["static.geetest.com/", "static.geevisit.com/"],
            "beeline": "/static/js/beeline.1.0.1.js",
            "voice": "/static/js/voice.1.2.4.js",
            "click": "/static/js/click.3.1.0.js",
            "fullpage": "/static/js/fullpage.9.1.9-r8k4eq.js",
            "slide": "/static/js/slide.7.9.2.js",
            "geetest": "/static/js/geetest.6.0.9.js",
            "aspect_radio": {"slide": 103, "click": 128, "voice": 128, "beeline": 50},
            "cc": 10,
            "ww": True,
            "i": "-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1!!-1"
        }

        self.key = '275eb894e65df12f'
        strs = json.dumps(data, separators=(',', ':'))
        w = self.ctx.call('encrypt', self.key, strs)
        print(w)

        headers = {
            "Accept": "*/*",
            "Accept-Language": "en",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Pragma": "no-cache",
            "Referer": "https://makeabooking.flyscoot.com/",
            "Sec-Fetch-Dest": "script",
            "Sec-Fetch-Mode": "no-cors",
            "Sec-Fetch-Site": "cross-site",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\""
        }
        url = "https://api.geetest.com/get.php"
        params = {
            "gt": self.gt,
            "challenge": self.challenge,
            "lang": "en",
            "pt": "0",
            "client_type": "web",
            "w": w,
            "callback": "geetest_1704433768209"
        }
        response = self.session.get(url, headers=headers, params=params)

        # print(response.text)
        # print(response)
        data = json.loads(response.text.replace('geetest_1704433768209', '')[1:-1])
        pprint(data)

    def get_pic(self):
        url = "https://api.geevisit.com/ajax.php"
        params = {
            "gt": self.gt,
            "challenge": self.challenge,
            "lang": "en",
            "pt": "0",
            "client_type": "web",
            "w": self.ctx.call('getw2', self.gt, self.challenge, self.key, random.randint(4414, 4614)),
            "callback": "geetest_1704435037740"
        }
        response = self.session.get(url, headers={
            "Accept": "*/*",
            "Accept-Language": "en",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Pragma": "no-cache",
            "Referer": "https://makeabooking.flyscoot.com/",
            "Sec-Fetch-Dest": "script",
            "Sec-Fetch-Mode": "no-cors",
            "Sec-Fetch-Site": "cross-site",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\""
        }, params=params)
        print(response.text)

        headers = {
            "Accept": "*/*",
            "Accept-Language": "en",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Pragma": "no-cache",
            "Referer": "https://makeabooking.flyscoot.com/",
            "Sec-Fetch-Dest": "script",
            "Sec-Fetch-Mode": "no-cors",
            "Sec-Fetch-Site": "cross-site",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\""
        }
        url = "https://api.geevisit.com/get.php"
        params = {
            "is_next": "true",
            "type": "click",
            "gt": self.gt,
            "challenge": self.challenge,
            "lang": "en",
            "https": "false",
            "protocol": "https://",
            "offline": "false",
            "product": "popup",
            "api_server": "api.geevisit.com",
            "isPC": "true",
            "autoReset": "true",
            "width": "100%",
            "callback": "geetest_1704435376539"
        }
        response = self.session.get(url, headers=headers, params=params)
        data = json.loads(response.text.replace('geetest_1704435376539', '')[1:-1])
        pprint(data)
        self.c = data['data']['c']
        self.s = data['data']['s']
        self.pic = data['data']['pic']

    def click(self):
        with open('pic.jpg', 'wb') as f:
            f.write(self.session.get('https://static.geetest.com' + self.pic).content)
        mouse = [
            [122, 121]
        ]
        aaa = input('...')
        mouse = []
        for i in aaa.split('|'):
            mouse.append([
                int(i.split(',')[0]),
                int(i.split(',')[1])
            ])
        w = self.ctx.call('getClick', mouse, random.randint(4400, 4800), {
            'c': self.c,
            's': self.s,
            'pci': self.pic,
        }, self.key)

        print(w)

        headers = {
            "Accept": "*/*",
            "Accept-Language": "en",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Pragma": "no-cache",
            "Referer": "https://makeabooking.flyscoot.com/",
            "Sec-Fetch-Dest": "script",
            "Sec-Fetch-Mode": "no-cors",
            "Sec-Fetch-Site": "cross-site",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"macOS\""
        }
        url = "https://api.geevisit.com/ajax.php"
        params = {
            "gt": self.gt,
            "challenge": self.challenge,
            "lang": "en",
            "pt": "0",
            "client_type": "web",
            "w": w,
            "callback": "geetest_1704440583213"
        }
        response = self.session.get(url, headers=headers, params=params)
        print(response.text)

        data = json.loads(response.text.replace('geetest_1704440583213', '')[1:-1])
        if data['status'] == 'success':
            print('success')
            self.validate = data['data']['validate']
            return True
        else:
            print('fail')

    def verify(self):
        headers = {
            "authority": "makeabooking.flyscoot.com",
            "accept": "*/*",
            "accept-language": "en",
            "cache-control": "no-cache",
            "content-type": "application/x-www-form-urlencoded",
            "origin": "https://makeabooking.flyscoot.com",
            "pragma": "no-cache",
            "referer": "https://makeabooking.flyscoot.com//_Incapsula_Resource?SWUDNSAI=31&xinfo=9-50218134-0%20NNNN%20RT%281704440813480%20109%29%20q%280%20-1%20-1%200%29%20r%282%20-1%29%20B12%2814%2c0%2c0%29%20U18&incident_id=266000780497654468-340043384159343497&edet=12&cinfo=0e0000005a28&rpinfo=0&cts=Fpsl1N44r%2ba5lOGG5IVMxuBuM%2fqTZ7Ql7AAOECU40KmmvR9Kjko6Xqj6BANcohXO&mth=GET",
            "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
            "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/120.0.0.0 Safari/537.36"
        }
        url = "https://makeabooking.flyscoot.com/_Incapsula_Resource"
        params = {
            "SWCGHOEL": "gee",
            "dai": "340043384159343497",
            "cts": self.cts
        }
        data = {
            "geetest_challenge": self.challenge,
            "geetest_validate": self.validate,
            "geetest_seccode": self.validate + "|jordan"
        }
        response = self.session.post(url, headers=headers, params=params, data=data)

        print(response.text)
        print(response)


if __name__ == '__main__':
    g = Gee()
    g.start()
