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:8890',
            'https': 'http://127.0.0.1:8890'
        }
        self.ctx = execjs.compile(open('./src.js').read())

        self.cts = 'aylqflYEw3UReF+NHwmVm5NHv9ghECWvoYmzhZeBvKClUmUmfr744onaktuzC9XS'
        r = requests.post('http://aws-lcc.unififi.com/header/borrow', json={
            'brush_type':'cookiePR',
            'method': "shopping"
        })
        cks = r.json()['data']['brush_header']['cookie']
        for i in cks.split('; '):
            t = i.index('=')

            self.session.cookies.update({
                i[:t]: i[t+1:]
            })

        self.headers = r.json()['data']['brush_header']
        self.headers.pop('cookie')
        pass

    def init(self):
        url = 'https://book.philippineairlines.com/_Incapsula_Resource?SWCNGEEC=aylqflYEw3UReF%2bNHwmVm5NHv9ghECWvoYmzhZeBvKClUmUmfr744onaktuzC9XS'




        response = self.session.get(url, headers=self.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()
