import pathlib
import platform
import random
import re
import subprocess
import time
import os
import loguru
import requests
import json
# import tls_client
from bson import ObjectId
import hashlib
from pymongo import MongoClient
from loguru import logger

def get_random_location():
    return ''
    t1 = '''var _hostname, _origin,_host,_protocol,_port,_pathname,_search,_hash;
    Object.defineProperties(location, {'''

    t2 = [

        '''    href: {
            value: 'https://www.southwest.com/air/booking/index.html?adultPassengersCount=1&adultsCount=1&departureDate=2023-07-13&departureTimeOfDay=ALL_DAY&destinationAirportCode=BOS&fareType=POINTS&int=HOMEQBOMAIR&originationAirportCode=LAS&passengerType=ADULT&reset=true&returnDate=&returnTimeOfDay=ALL_DAY&tripType=oneway&validate=true',
            enumerable: true
        },
        ''',
        '''ancestorOrigins: {
                get: v_saf(function ancestorOrigins() {
                    return new DOMStringList()
                }),
                enumerable: true,
                configurable: false
            },''',
        '''  hostname: {set: function(arg){_hostname = arg}, get:function(){return _hostname}, enumerable:true},''',

        '''reload: {
            value: v_saf(function reload() {

            }),
            enumerable: true,

        },''',
        '''protocol: {set: function(arg){_protocol = arg}, get:function(){return _protocol}, enumerable:true},''',

        '''host: {set: function(arg){_hostname = arg}, get:function(){return _hostname}, enumerable:true},''',
        '''  port: {set: function(arg){_port = arg}, get:function(){return _port}, enumerable:true},''',
        '''  hash: {set: function(arg){_hash = arg}, get:function(){return _hash}, enumerable:true},''',
        '''   pathname: {set: function(arg){_pathname = arg}, get:function(){return _pathname}, enumerable:true},''',
        '''origin: {set: function(arg){_origin = arg}, get:function(){return _origin}, enumerable:true},''',
        '''search: {set: function(arg){_pathname = arg}, get:function(){return _pathname}, enumerable:true},''',

        '''assign: {
            value: v_saf(function assign() {

            }),
            enumerable: true,

        },''',
        '''toString: {
            value: v_saf(function toString() {
                return ''
            }),
            enumerable: true,

        },''',
        '''replace: {
            value: v_saf(function replace() {

            }),
            enumerable: true,
        },'''
    ]

    random.shuffle(t2)
    # print(t2)

    r = '''location.href = 'https://www.southwest.com/air/booking/index.html?adultPassengersCount=1&adultsCount=1&departureDate=2023-07-13&departureTimeOfDay=ALL_DAY&destinationAirportCode=BOS&fareType=POINTS&int=HOMEQBOMAIR&originationAirportCode=LAS&passengerType=ADULT&reset=true&returnDate=&returnTimeOfDay=ALL_DAY&tripType=oneway&validate=true'
    function set_href(v, href){
            href = href.trim()
            v_console_log(`  href[set]:`, JSON.stringify(href))
            if (href.startsWith("http://") || href.startsWith("https://")) {/*ok*/
            } else if (href.startsWith("//")) {
                href = (v.protocol ? v.protocol : 'http:') + href
            } else {
                href = v.protocol + "//" + v.host + (v.port ? ":" + v.port : "") + '/' + ((href[0] == '/') ? href.slice(1) : href)
            }
            var a = href.match(/([^:]+:)\/\/([^/:?#]+):?(\d+)?([^?#]*)?(\?[^#]*)?(#.*)?/);
            v.protocol = a[1] ? a[1] : "";
            v.host = a[2] ? a[2] : "";
            v.port = a[3] ? a[3] : "";
            v.pathname = a[4] ? a[4] : "";
            v.search = a[5] ? a[5] : "";
            v.hash = a[6] ? a[6] : "";
            v.hostname = v.host;
            v.origin = v.protocol + "//" + v.host + (v.port ? ":" + v.port : "");
    }
    // 'valueOf,ancestorOrigins,href,origin,protocol,host,hostname,port,pathname,search,hash,assign,reload,replace,toString'
    set_href(location, 'https://www.southwest.com/air/booking/index.html?adultPassengersCount=1&adultsCount=1&departureDate=2023-07-13&departureTimeOfDay=ALL_DAY&destinationAirportCode=BOS&fareType=POINTS&int=HOMEQBOMAIR&originationAirportCode=LAS&passengerType=ADULT&reset=true&returnDate=&returnTimeOfDay=ALL_DAY&tripType=oneway&validate=true')
    window.location = location
    // window.location = new Proxy(location, {
    //     get(target, p, receiver) {
    //         console.log('locaiton get', p)
    //         return Reflect.get(target, p, receiver)
    //     },
    //     has(target, p) {
    //         let res = Reflect.has(target, p);
    //         console.log('locaiton has', p, res)
    //         return res;
    //     },
    //     getOwnPropertyDescriptor(target, p) {
    //         let res = Reflect.getOwnPropertyDescriptor(target, p);
    //
    //         console.log('locaiton getOwnPropertyDescriptor', p, res)
    //         return res;
    //     }
    // })

    origin = location.origin
    '''

    return t1 + '\n' + '\n'.join(t2) + '})\n' + r
work_dir = os.environ.get('work_dir')
def get_proxy():
    get_proxy_url = 'http://ymx-lcc.unififi.com/proxy/get-proxy'
    r = requests.post(get_proxy_url, json={
        'name': 'WN',
        'type': 'shopping'
    })
    return r.json()
def md5(text):
    m = hashlib.md5()
    m.update(text.encode())
    return m.hexdigest()

# 连接MongoDB数据库
client = MongoClient('mongodb://lcc-spider:JIUsfhiad&^@54.183.159.9:27017/?authSource=admin&directConnection=true')
db = client.env
# 查询所有文档
#platform.uname().system != 'Darwin' and not
if not pathlib.Path('env.json').exists():
    # , 'success': {'$gte': 30}
    # , 'env.navigatorparams.useragent': {'$regex': 'iPhone'}
    # result = db.env_shape.find({'env.funcs' :{'$ne': None}})
    result = db.env_shape.find({'success1' :{'$exists': True}})
    data = {}
    for x in result:
        # if '₹' in x['env']['fontsres']['1000px monospace']:
        data[str(x['_id'])] = x['env']


    print(result)
    with open('env.json', 'w', encoding='utf-8') as f:
        f.write(json.dumps(data))

with open('env.json', 'r', encoding='utf-8') as f:
    data = json.loads(f.read())
envs = data.keys()

print(len(envs))

# session = requests.session()
headers  = {
    "authority": "www.southwest.com",
    "accept": "application/json, text/javascript, */*; q=0.01",
    "accept-language": "en,en-US;q=0.9,zh-CN;q=0.8,zh;q=0.7,ru;q=0.6",
    "cache-control": "no-cache",
    "content-type": "application/json",
    # "ee30zvqlwf-a": "hTGMXafTzv2PfXbe_AY1Qkn8IuOmoRMR3Yh26sw-zc1x=URAL_NjXURwnMjr=M5euHEBOSzi8tniqBESpKvNKChOKjRp5hCoPOAXzv8fy7gU=PPwj=WnHlkTiQL-uGbyN=yAgyt_7F69MXB0rL9Ykyg7wF8r6z7y0vX_yA52hfXStuPYTdZ-aBpvoAR=72VmWKQVYfAYB1gvWtN66gSLlhXMV=L4qETVcuOA2bI3BU2zorTFaoTXGkPuSXzVHaNymIyMfou2OMuEk460slIfBxlBcFug0sTE3nqbPVsrKdm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aMkG-rej8wVOHD91XWB7DjxX89D21uXrGf2FxIXCHtCfpRlXxIWoznZGWk_3_bDs6-LmQ2zgdS9SXQeqNcjd6pA0pvt14celeUmrcVC6kUoTDlsklKLVbNwZn2PoW1YHHP=gKRgjRnQAcNM70d87MBc09egVxF8qAGGwBvqvdF3TnEgt8Bv7YkYCZ5CtuTur_AZzFAbcLPRtyyXntqd5jBRFGl6Uqy8OQLgRpcwsShpwb1hY=qny6rYydgcLIWIDw-9AlKMNXuPMSqQaUfZeWC7Y2m-jUpZ1DBWZPjCNh8jo56gLXL8KQlmtbnKu6pADjv-lgKXA-ltTF3riZQdyARXP2Cqk_OVx8olMTYtcSIEutBCrmoLzyUiH97C4f=pfaRUBcSUAdHbFsK-wiSpNB=SBCXNfYDRG3",
    # "ee30zvqlwf-b": "4ih8bw",
    # "ee30zvqlwf-c": "AAAVF4KMAQAAIjh8ZUHWFh_dXPGoIB5nyDQCVIWEDYjClQpQK7Z0MG51WSS4",
    # "ee30zvqlwf-d": "ABaAhIDBCKGFgQGAAYIQgISigaIAwBGAzvpizi_33we2dDBudVkkuAAAAABnMdsJAEskk19wkWHrMAo_h-nxK_s",
    # "ee30zvqlwf-f": "AzQRG4KMAQAA3sP_kq2I3xtte3phkhRNbYJgTSgioyQ9zCzyMSvEUjJby6YpAReeaPauchRAwH9eCOfvosJeCA==",
    # "ee30zvqlwf-z": "q",
    "origin": "https://www.southwest.com",
    "pragma": "no-cache",
    "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
    "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/114.0.0.0 Safari/537.36",
    "x-api-key": "l7xx944d175ea25f4b9c903a583ea82a1c4c",
    "x-app-id": "air-booking",
    "x-channel-id": "southwest",
    # "x-user-experience-id": "094ff7f7-a044-4b53-973a-b9e4a9374001"
}
# tlss = [ 'chrome_103', 'chrome_104', 'chrome_105', 'chrome_106',
#         'firefox_102', 'firefox_104'
        # Opera --> opera_89, opera_90
      # 'safari_15_3', 'safari_15_6_1', 'safari_16_0',
      #   'safari_ios_15_5', 'safari_ios_15_6', 'safari_ios_16_0',
      #  'safari_ios_15_6'
      #   ]

success = 0
total = 0
token = ''
while True:
    # r = requests.get('http://ymx-lcc.unififi.com/header/task/pull/cookieB6?brush_name=test')
    # if platform.uname().system == 'Darwin':
    #     if r.json()['status'] != 0:
    #         logger.debug(f'不需要刷值')
    #         time.sleep(10)
    #         continue
    logger.debug('开始刷值')
    selectId = random.choice(list(envs))
    lastHash = ''
    usedTime = 10

    code = open('src.js', 'r', encoding='utf-8').read()
    for i in range(30):
        try:
            proxy = f'http://unfflcc:76cc14-47b8dd-1f8ace-827836-0c740e@usa.rotating.proxyrack.net:{int(random.randint(10000, 11149))}'
            proxy1 = f'http://unfflcc:76cc14-47b8dd-1f8ace-827836-0c740e@usa.rotating.proxyrack.net:{int(random.randint(10000, 11149))}'
            # proxy = random.choice(['http://unfflcc:76cc14-47b8dd-1f8ace-827836-0c740e@usa.rotating.proxyrack.net:9000', proxy])
            # proxy = requests.post('http://ymx-lcc.unififi.com/proxy/get-proxy', json={'name':"B6", 'type':'shopping'}).json()['url']

            # proxy = f'http://ynfflcc2023.hotmail.com:gdcfh9@69.30.227.194:{random.randint(20001, 20250)}'
            proxy2 = f'http://user-uni003-region-us-sessid-{random.randint(1000,5000)}-sesstime-5-keep-true:q39CEBTs5A5YQXor@pr.roxlabs.cn:4600'
            # proxy = f'http://user-uni003-region-us-sessid-{random.randint(1000,5000)}-sesstime-5-keep-true:q39CEBTs5A5YQXor@pr.roxlabs.cn:4600'
            # proxy = f'http://unfflcc:76cc14-47b8dd-1f8ace-827836-0c740e@198.147.25.26:{int(random.randint(10000, 11149))}'
            # proxy = 'http://user-unifflcc-region-us:q39CEBTs5A5YQXor@pr.roxlabs.cn:4600'
            tlsV = f'chrome_{random.randint(105, 113)}'
            # proxy = random.choice([proxy1, proxy2])
            proxy = get_proxy()['url']
            logger.info(f"success:{success}  proxy: {proxy} ")

            tlsV = 'chrome_112'
            # session = requests.Session(impersonate='chrome110')
            # # session = tls_client.Session(tlsV)
            # # session.timeout_seconds = 15
            # # session = requests.session()
            # session.proxies = {
            #     'http': proxy,
            #     'https': proxy,
            # }
            # # session.verify = False
            # if platform.uname().system == 'Darwin':
            #     session.proxies = {
            #         'http': 'http://127.0.0.1:7890',
            #         'https': 'http://127.0.0.1:7890',
            #     }
            selectId = random.choice(list(envs))
            # selectId = '197d94c3099d110aef4e0a52'
            if  usedTime > 5:
                # c = session.get('https://www.southwest.com/assets/app/scripts/swa-common.js').text
                response = requests.post('http://52.52.23.116/tls/forward',
                                         json=dict(url='https://www.southwest.com/assets/app/scripts/swa-common.js', headers=headers, method="GET", proxy=proxy, debug=True
                                                   , allow_redirects=True, random_tls=True
                                                   ))
                c = response.json()['text']
                m5 = md5(c)
                if m5 != lastHash:
                    lastHash = m5
                    usedTime = 0
                    logger.debug('js变化')
                    token = re.findall('nt"\),\["(.+?)",', c)[0]
                    # db.token_shape.insert_one({
                    #     '_id': token,
                    #     'update_time': int(time.time() * 1000)
                    # })
                    code = open('src.js', 'r', encoding='utf-8').read()
                    with open('src_run2.js', 'w', encoding='utf-8') as f:
                        f.write(code + '\n\n' + get_random_location() + ';;' + c
                                +
                                ';;;\n\n' + '''
    if (randomEvent){
        window.mouse_move(startx,starty,endx,endy)

    }

    var xhr = new XMLHttpRequest();
    //xhr.open('POST', "https://www.southwest.com/api/air-booking/v1/air-booking/page/air/booking/shopping", false);
    //xhr.send(`{"tripType":"oneWay","from":"ZLA","to":"NAS","depart":"2023-04-11","cabin":"economy","refundable":false,"dates":{"before":"3","after":"3"},"pax":{"ADT":1,"CHD":0,"INF":0,"UNN":0},"redempoint":false,"pointsBreakup":{"option":"","value":0},"isMultiCity":false}`)
    //xhr.setRequestHeader('user-agent', ENV.navigatorparams.useragent)
    //print(JSON.stringify(xhr.headers))
     //_process.exit(0)
setTimeout(r=> {
                            var
                        xhr = new
                        XMLHttpRequest();
                        xhr.open('POST',
                                 "https://www.southwest.com/api/air-booking/v1/air-booking/page/air/booking/shopping",
                                 false);
                        xhr.send(`{"tripType": "oneWay", "from": "ZLA", "to": "NAS", "depart": "2023-04-11",
                                   "cabin": "economy", "refundable": false, "dates": {"before": "3", "after": "3"},
                                   "pax": {"ADT": 1, "CHD": 0, "INF": 0, "UNN": 0}, "redempoint": false,
                                   "pointsBreakup": {"option": "", "value": 0}, "isMultiCity": false}`)
                        xhr.setRequestHeader('user-agent', ENV.navigatorparams.useragent)
                        //xhr.setRequestHeader('xhr_url', xhr_url)
                        //xhr.setRequestHeader('xhr_res', xhr_res)
                        print22(JSON.stringify(xhr.headers))
                        _process.exit(0)
                        }, 1000)

                        '''

                                )
                        # f.write(code + '\n\n' + get_random_location() + ';;' + c)

                code1 = open('js_dom_run.js','r').read()
                # with open('js_dom_run2js', 'w', encoding='utf-8') as f:
                #     f.write(code1 + '\n\n' + c)
            t1 = time.time()
            # token = list(db.token_shape.aggregate( [ {'$match': {'update_time':{'$gte': int(time.time() * 1000) - 60*60*12}}}, { '$sample': { 'size': 1 } } ] ))[0]['_id']
            # token = '11'
            # r = requests.get('http://127.0.0.1:5000/getinfo').json()['message']
            # r = requests.get('http://127.0.0.1:3001').json()
            logger.info("start execute js")

            # p = subprocess.Popen(['node', 'js_dom_run1.js', selectId, '11'], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            # p = subprocess.Popen(['node', 'src_run_chrome_105.js', selectId, '11', token], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            # p = subprocess.Popen(['node', 'src_run_company_chrome.js', selectId, '11', token], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
            p = subprocess.Popen(['node', 'src_run2.js', selectId, '11', token, proxy], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
            output, err = p.communicate(timeout=10)
            tmp = ''
            if p.returncode == 0:
                tmp = output.decode().split('\n')[-2]
            else:
                raise Exception(selectId)
            r: dict = json.loads(tmp)
            # res = db.env_shape.update_one(
            #     {'_id': ObjectId(selectId)},
            #     {"$inc": {"used": 1}}
            # )
            usedTime += 1
            # print(r)
            # xhr_url = r.pop('xhr_url')
            # xhr_res = r.pop('xhr_res')
            # print('xhr_url', xhr_url)
            # print('xhr_res', xhr_res)
            headers.update(r)
            # headers = r
            logger.info(f"success:{success}  total:{total}  {(time.time() - t1) *1000:.3f}ms {len(headers['ee30zvqlwf-a'.lower()])} {selectId} {headers['user-agent']}")

            url = "https://www.southwest.com/api/air-booking/v1/air-booking/page/air/booking/shopping"
            data = {
                "adultPassengersCount": "1",
                "adultsCount": "1",
                "departureDate": "2023-12-22",
                "departureTimeOfDay": "ALL_DAY",
                "destinationAirportCode": "LAX",
                "fareType": "USD",
                "from": "",
                "int": "HOMEQBOMAIR",
                "originationAirportCode": "LAS",
                "passengerType": "ADULT",
                "promoCode": "",
                "reset": "true",
                "returnDate": "2023-08-24",
                "returnTimeOfDay": "ALL_DAY",
                "to": "",
                "tripType": "roundtrip",
                "application": "air-booking",
                "site": "southwest"
            }
            data = json.dumps(data)
            #
            # session = requests.Session()
            #
            # if platform.uname().system == 'Darwin':
            total += 1
            # response = session.post(url, headers=headers, data=data)
            # status_code =  response.status_code

            response = requests.post('http://52.52.23.116/tls/forward',
                          json=dict(url=url, headers=headers, method="POST", proxy=proxy, debug=True, data=data
                                    , allow_redirects=True, random_tls=True
                                    ))
            status_code = response.json()['status_code']
            # print(response.text)
            logger.info(f'tls:{tlsV} proxy:{proxy}  res:: {status_code}')
            # status_code = 400
            if status_code != 403:
                success += 1
                uri = 'http://ymx-lcc.unififi.com/header/add'
                # uri = 'http://52.52.23.116/header/add'
                # proxy = f'http://unfflcc:76cc14-47b8dd-1f8ace-827836-0c740e@usa.rotating.proxyrack.net:{int(random.randint(10000, 11149))}'
                # proxy = 'http://unfflcc:76cc14-47b8dd-1f8ace-827836-0c740e@usa.rotating.proxyrack.net:10042'
                # proxy = 'http://user-unifflcc-region-us:q39CEBTs5A5YQXor@pr.roxlabs.cn:4600'
                brush_name = 'jsk8s'
                if platform.uname().system == 'Darwin':
                    # print(headers)
                    brush_name = 'jstest'

                r = requests.post(uri, json={
                    'brush_fromto': 'LAS-BOS',
                    'brush_fromdate': '2023-05-26',
                    'brush_header': headers,
                    'brush_proxy': proxy,
                    'brush_mode': 'shopping',
                    'brush_name': brush_name,
                    'brush_type': 'cookieWN',
                })

                logger.success(f'添加成功 {success}')
            # time.sleep(3)
        except Exception as e:
            print(e)
    # time.sleep(10)
