import sys
import os

sys.path.append(os.path.dirname(sys.path[0]))  # 上级目录
import requests
import json
from lxml import etree
import re
import random
import pymysql
import uuid
import time
import py_ja3
from params import DB_CONN_DICT
import urllib3
import traceback
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
item = {}

headers_num_int = 0

def get_cookie(site='us', zipCode='10010'):
    try:
        if site == "us":
            url_ = 'https://www.amazon.com'
            host = 'www.amazon.com'
        elif site == 'uk':
            url_ = 'https://www.amazon.co.uk'  # 站点url
            host = 'www.amazon.co.uk'
        elif site == 'de':
            url_ = 'https://www.amazon.de'
            host = 'www.amazon.de'
        elif site == 'fr':
            url_ = 'https://www.amazon.fr'
            host = 'www.amazon.fr'
        elif site == 'es':
            url_ = 'https://www.amazon.es'
            host = 'www.amazon.es'
        elif site == 'it':
            url_ = 'https://www.amazon.it'
            host = 'www.amazon.it'
        if site == 'us':
            us_db = pymysql.connect(host=DB_CONN_DICT['mysql_host'], port=DB_CONN_DICT['mysql_port'], user=DB_CONN_DICT['mysql_user'],
                             password=DB_CONN_DICT['mysql_pwd'], database="selection", charset="utf8mb4")
        else:
            us_db = pymysql.connect(host=DB_CONN_DICT['mysql_host'], port=DB_CONN_DICT['mysql_port'], user=DB_CONN_DICT['mysql_user'],
                             password=DB_CONN_DICT['mysql_pwd'], database="selection_" + site,
                                           charset="utf8mb4")
        us_cursor = us_db.cursor()
        n = random.randint(70, 114)
        ua = f'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{n}.0.{random.randint(1000, 5000)}.{random.randint(1, 181)} Safari/537.36'
        headers = {
            'connection': 'close',
            'authority': host,
            'accept': 'text/html,*/*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
            'origin': url_,
            'referer': url_,
            'sec-ch-ua-mobile': '?0',
            'user-agent': ua
        }
        alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
        k = ""
        for i in (0, random.randint(0, 5)):
            k += random.choice(alphabet)
        headers[k] = str(uuid.uuid4())
        sess = requests.Session()
        sess.mount(url_, py_ja3.DESAdapter())
        resp_ = sess.get(url_, headers=headers, timeout=15, verify=False)
        cookie = resp_.headers.get('set-cookie')
        print("第一步 请求首页", url_)
        cookies_dict = {i.split("=")[0]: i.split("=")[-1] for i in cookie.split("; ")}
        html_xpath = etree.HTML(resp_.text)
        ingress = html_xpath.xpath("//span[@id='glow-ingress-line2']/text()")
        print("第一次发送请求，获取邮编：", ingress)
        data_a_modal = html_xpath.xpath("//span[@id='nav-global-location-data-modal-action']/@data-a-modal")
        data_modal = json.loads(data_a_modal[0])
        if site != 'us':
            csrftoken = html_xpath.xpath("//input[@name='anti-csrftoken-a2z']/@value")[0]
            url_post = url_ + '/privacyprefs/retail/v1/acceptall'
            dada_post = {
                "anti-csrftoken-a2z": csrftoken,
                "accept": "all"
            }
            resp_post = sess.post(url_post, headers=headers, cookies=cookies_dict, timeout=15, data=dada_post, verify=False)
            cookie_post = resp_post.headers.get('set-cookie')
            cookies_dict_post = {i.split("=")[0]: i.split("=")[-1] for i in cookie_post.split("; ")}
            cookies_dict_post.update(cookies_dict)
        else:
            cookies_dict_post = cookies_dict
        if site == 'us':
            get_token_headers = {
                'anti-csrftoken-a2z': data_modal['ajaxHeaders']['anti-csrftoken-a2z'],
                'referer': url_,
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
            }
            print(get_token_headers,'23232')
        else:
            get_token_headers = {
                'accept': 'text/html,*/*',
                'accept-encoding': 'gzip, deflate, br',
                'accept-language': 'zh-CN,zh;q=0.9',
                'anti-csrftoken-a2z': data_modal['ajaxHeaders']['anti-csrftoken-a2z'],
                'cache-control': 'no-cache',
                'referer': url_,
                'sec-fetch-dest': 'empty',
                'sec-fetch-mode': 'cors',
                'sec-fetch-site': 'same-origin',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
                'viewport-width': '1920',
                'x-requested-with': 'XMLHttpRequest',
            }
        data_modal_url = url_ + data_modal['url']
        print('第二步 拼接url 点击更改位置:',data_modal_url)
        data_modal_resp = sess.get(data_modal_url, headers=get_token_headers, cookies=cookies_dict_post,
                                   timeout=15,verify=False)
        data_modal_cookie = data_modal_resp.headers.get('set-cookie')
        CSRF_TOKEN = re.findall('CSRF_TOKEN : "(.*?)",', data_modal_resp.text)[0]
        print("CSRF_TOKEN:",CSRF_TOKEN)
        try:
            data_modal_cookie_dict = {i.split("=")[0]: i.split("=")[-1] for i in data_modal_cookie.split("; ")}
            data_modal_cookie_dict.update(cookies_dict)
        except:
            data_modal_cookie_dict = cookies_dict_post

        url_2 = url_ + '/portal-migration/hz/glow/address-change?actionSource=glow'
        print('url_2:',url_2)
        # {"locationType":"LOCATION_INPUT","zipCode":"10010","deviceType":"web","storeContext":"generic","pageType":"Gateway","actionSource":"glow"}
        data = {"locationType":"LOCATION_INPUT","zipCode":zipCode,"storeContext":"generic","deviceType":"web","pageType":"Gateway","actionSource":"glow"}
        print(data)
        post_headers = {
            'anti-csrftoken-a2z': CSRF_TOKEN,
            'accept': 'text/html,*/*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'content-length': '138',
            'content-type': 'application/json',
            'device-memory': '8',
            'downlink': '10',
            'dpr': '1',
            'ect': '4g',
            'origin': url_,
            'pragma': 'no-cache',
            'referer': url_,
            'rtt': '250',
            'sec-ch-device-memory': '8',
            'sec-ch-dpr': '1',
            'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-ch-ua-platform-version': '"10.0.0"',
            'sec-ch-viewport-width': '1920',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
            'viewport-width': '1920',
            'TE':'trailers',
            'x-requested-with': 'XMLHttpRequest'

        }
        print('第三步 发送post 请求 输入 邮编 点击确定')
        resp_2 = sess.post(url_2, headers=post_headers, json=data, cookies=data_modal_cookie_dict,
                           timeout=15,verify=False)
        print(resp_2.text)
        post_cookies = resp_2.headers.get('set-cookie')
        try:
            post_cookies_dict = {i.split("=")[0]: i.split("=")[-1] for i in post_cookies.split("; ")}
            post_cookies_dict.update(data_modal_cookie_dict)
        except:
            post_cookies_dict = data_modal_cookie_dict

        done_url = url_ + "/portal-migration/hz/glow/get-location-label?storeContext=generic&pageType=Gateway&actionSource=desktop-modal"
        print('第四步，点击完成，')
        done_resp = sess.get(done_url, headers=headers, cookies=post_cookies_dict, timeout=15,verify=False)
        print(done_resp.text,'done_respdone_respdone_respdone_resp')

        done_cookies_dict = sess.cookies.get_dict()
        print('done_cookies_dict::',done_cookies_dict)
        print("第五步，请求首页，获取邮编，是否修改成功")
        index_resp = sess.get(url_, headers=headers, timeout=15,cookies=done_cookies_dict,verify=False)
        index_resp_cookies = sess.cookies.get_dict()
        print(sess.cookies.get_dict(),'2222222222222222')
        index_xpath = etree.HTML(index_resp.text)
        ingress = index_xpath.xpath("//span[@id='glow-ingress-line2']/text()")
        print("获取最新邮编：", ingress)

        if zipCode in ingress[0].strip() or "W1S 3" in ingress[0].strip():
            print(f"***************    当前获取 {site} 站点 cookie   邮编  {zipCode}  ********************")
            cookies = json.dumps(index_resp_cookies)
            item = {"site": site, 'zipCode': ingress[0].strip(), 'cookie': cookies}
            print(item)
            insert_sql = f'insert into {site}_cookies (cookies,type)values (%s,%s)'
            print(insert_sql)
            us_cursor.execute(insert_sql, (cookies,'DB'))
            us_db.commit()
            us_cursor.close()
            us_db.close()
        sess.close()
    except Exception as e:
        print(f"获取 {site} 站点 cookie 报错，切换下一个站点",e)
        print("报错", f"\n{traceback.format_exc()}")
        time.sleep(random.uniform(2.5, 5.5))


if __name__ == '__main__':
    while True:
        get_cookie(site='us', zipCode='10010')
        get_cookie(site='de', zipCode='10115')
        get_cookie(site='uk', zipCode='W1S 3PR')
        get_cookie(site='it', zipCode='00185')
        get_cookie(site='es', zipCode='28001')
        get_cookie(site='fr', zipCode='75019')
        get_cookie(site='us', zipCode='10010')
        get_cookie(site='de', zipCode='10115')
        get_cookie(site='uk', zipCode='W1S 3PR')
        time.sleep(random.uniform(5.5, 25.5))




'存储到pg'

'获取小语言cookie'

import sys
import os

sys.path.append(os.path.dirname(sys.path[0]))  # 上级目录
import requests
import json
from lxml import etree
import re
import random
import pymysql
import uuid
import time
import py_ja3
from params import DB_CONN_DICT
from sqlalchemy import text
import urllib3
import traceback
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
item = {}

headers_num_int = 0

def get_cookie(site='us', zipCode='10010'):
    try:
        if site == "us":
            url_ = 'https://www.amazon.com'
            host = 'www.amazon.com'
        elif site == 'uk':
            url_ = 'https://www.amazon.co.uk'  # 站点url
            host = 'www.amazon.co.uk'
        elif site == 'de':
            url_ = 'https://www.amazon.de'
            host = 'www.amazon.de'
        elif site == 'fr':
            url_ = 'https://www.amazon.fr'
            host = 'www.amazon.fr'
        elif site == 'es':
            url_ = 'https://www.amazon.es'
            host = 'www.amazon.es'
        elif site == 'it':
            url_ = 'https://www.amazon.it'
            host = 'www.amazon.it'
        if site == 'us':
            us_db = pymysql.connect(host=DB_CONN_DICT['mysql_host'], port=DB_CONN_DICT['mysql_port'],
                                    user=DB_CONN_DICT['mysql_user'],
                                    password=DB_CONN_DICT['mysql_pwd'], database="selection", charset="utf8mb4")
        else:
            us_db = pymysql.connect(host=DB_CONN_DICT['mysql_host'], port=DB_CONN_DICT['mysql_port'],
                                    user=DB_CONN_DICT['mysql_user'],
                                    password=DB_CONN_DICT['mysql_pwd'], database="selection_" + site,
                                    charset="utf8mb4")
        us_cursor = us_db.cursor()

        n = random.randint(70, 114)
        ua = f'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{n}.0.{random.randint(1000, 5000)}.{random.randint(1, 181)} Safari/537.36'
        headers = {
            'connection': 'close',
            'authority': host,
            'accept': 'text/html,*/*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
            'origin': url_,
            'referer': url_,
            'sec-ch-ua-mobile': '?0',
            'user-agent': ua
        }
        alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
        k = ""
        for i in (0, random.randint(0, 5)):
            k += random.choice(alphabet)
        headers[k] = str(uuid.uuid4())
        sess = requests.Session()
        sess.mount(url_, py_ja3.DESAdapter())
        resp_ = sess.get(url_, headers=headers, timeout=15, verify=False)
        cookie = resp_.headers.get('set-cookie')
        print("第一步 请求首页", url_)
        cookies_dict = {i.split("=")[0]: i.split("=")[-1] for i in cookie.split("; ")}
        html_xpath = etree.HTML(resp_.text)
        ingress = html_xpath.xpath("//span[@id='glow-ingress-line2']/text()")
        print("第一次发送请求，获取邮编：", ingress)
        data_a_modal = html_xpath.xpath("//span[@id='nav-global-location-data-modal-action']/@data-a-modal")
        data_modal = json.loads(data_a_modal[0])
        if site != 'us':
            csrftoken = html_xpath.xpath("//input[@name='anti-csrftoken-a2z']/@value")[0]
            url_post = url_ + '/privacyprefs/retail/v1/acceptall'
            dada_post = {
                "anti-csrftoken-a2z": csrftoken,
                "accept": "all"
            }
            resp_post = sess.post(url_post, headers=headers, cookies=cookies_dict, timeout=15, data=dada_post,
                                  verify=False)
            cookie_post = resp_post.headers.get('set-cookie')
            cookies_dict_post = {i.split("=")[0]: i.split("=")[-1] for i in cookie_post.split("; ")}
            cookies_dict_post.update(cookies_dict)
        else:
            cookies_dict_post = cookies_dict
        if site == 'us':
            get_token_headers = {
                'anti-csrftoken-a2z': data_modal['ajaxHeaders']['anti-csrftoken-a2z'],
                'referer': url_,
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
            }
            print(get_token_headers, '23232')
        else:
            get_token_headers = {
                'accept': 'text/html,*/*',
                'accept-encoding': 'gzip, deflate, br',
                'accept-language': 'zh-CN,zh;q=0.9',
                'anti-csrftoken-a2z': data_modal['ajaxHeaders']['anti-csrftoken-a2z'],
                'cache-control': 'no-cache',
                'referer': url_,
                'sec-fetch-dest': 'empty',
                'sec-fetch-mode': 'cors',
                'sec-fetch-site': 'same-origin',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
                'viewport-width': '1920',
                'x-requested-with': 'XMLHttpRequest',
            }
        data_modal_url = url_ + data_modal['url']
        print('第二步 拼接url 点击更改位置:', data_modal_url)
        data_modal_resp = sess.get(data_modal_url, headers=get_token_headers, cookies=cookies_dict_post,
                                   timeout=15, verify=False)
        data_modal_cookie = data_modal_resp.headers.get('set-cookie')
        CSRF_TOKEN = re.findall('CSRF_TOKEN : "(.*?)",', data_modal_resp.text)[0]
        print("CSRF_TOKEN:", CSRF_TOKEN)
        try:
            data_modal_cookie_dict = {i.split("=")[0]: i.split("=")[-1] for i in data_modal_cookie.split("; ")}
            data_modal_cookie_dict.update(cookies_dict)
        except:
            data_modal_cookie_dict = cookies_dict_post

        url_2 = url_ + '/portal-migration/hz/glow/address-change?actionSource=glow'
        print('url_2:', url_2)
        # {"locationType":"LOCATION_INPUT","zipCode":"10010","deviceType":"web","storeContext":"generic","pageType":"Gateway","actionSource":"glow"}
        data = {"locationType": "LOCATION_INPUT", "zipCode": zipCode, "storeContext": "generic", "deviceType": "web",
                "pageType": "Gateway", "actionSource": "glow"}
        print(data)
        post_headers = {
            'anti-csrftoken-a2z': CSRF_TOKEN,
            'accept': 'text/html,*/*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'content-length': '138',
            'content-type': 'application/json',
            'device-memory': '8',
            'downlink': '10',
            'dpr': '1',
            'ect': '4g',
            'origin': url_,
            'pragma': 'no-cache',
            'referer': url_,
            'rtt': '250',
            'sec-ch-device-memory': '8',
            'sec-ch-dpr': '1',
            'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-ch-ua-platform-version': '"10.0.0"',
            'sec-ch-viewport-width': '1920',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
            'viewport-width': '1920',
            'TE': 'trailers',
            'x-requested-with': 'XMLHttpRequest'

        }
        print('第三步 发送post 请求 输入 邮编 点击确定')
        resp_2 = sess.post(url_2, headers=post_headers, json=data, cookies=data_modal_cookie_dict,
                           timeout=15, verify=False)
        print(resp_2.text)
        post_cookies = resp_2.headers.get('set-cookie')
        try:
            post_cookies_dict = {i.split("=")[0]: i.split("=")[-1] for i in post_cookies.split("; ")}
            post_cookies_dict.update(data_modal_cookie_dict)
        except:
            post_cookies_dict = data_modal_cookie_dict

        done_url = url_ + "/portal-migration/hz/glow/get-location-label?storeContext=generic&pageType=Gateway&actionSource=desktop-modal"
        print('第四步，点击完成，')
        done_resp = sess.get(done_url, headers=headers, cookies=post_cookies_dict, timeout=15, verify=False)
        print(done_resp.text, 'done_respdone_respdone_respdone_resp')

        done_cookies_dict = sess.cookies.get_dict()
        print('done_cookies_dict::', done_cookies_dict)
        print("第五步，请求首页，获取邮编，是否修改成功")
        index_resp = sess.get(url_, headers=headers, timeout=15, cookies=done_cookies_dict, verify=False)
        index_resp_cookies = sess.cookies.get_dict()
        print(sess.cookies.get_dict(), '2222222222222222')
        index_xpath = etree.HTML(index_resp.text)
        ingress = index_xpath.xpath("//span[@id='glow-ingress-line2']/text()")
        print("获取最新邮编：", ingress)

        if zipCode in ingress[0].strip() or "W1S 3" in ingress[0].strip():
            print(f"***************    当前获取 {site} 站点 cookie   邮编  {zipCode}  ********************")
            cookies = json.dumps(index_resp_cookies)
            item = {"site": site, 'zipCode': ingress[0].strip(), 'cookie': cookies}
            print(item)
            insert_sql = f'insert into {site}_cookies (cookies,type)values (%s,%s)'
            print(insert_sql)
            us_cursor.execute(insert_sql, (cookies,'DB'))
            us_db.commit()
            us_cursor.close()
            us_db.close()
            if site in ('us','de','uk'):
                # 构造参数化的 SQL 语句
                insert_sql = text("INSERT INTO {}_cookies (cookies, type) VALUES (:cookies, :type)".format(site))
                print(insert_sql)
                # 使用 with 上下文管理连接，自动开启事务并在结束时提交和关闭连接
                # with engine_pg.begin() as conn:
                #     conn.execute(insert_sql, {"cookies": cookies, "type": "DB"})
                # print("存储成功")
        sess.close()
    except Exception as e:
        print(f"获取 {site} 站点 cookie 报错，切换下一个站点",e)
        print("报错", f"\n{traceback.format_exc()}")
        time.sleep(random.uniform(2.5, 5.5))


if __name__ == '__main__':
    while True:
        get_cookie(site='us', zipCode='10010')
        get_cookie(site='de', zipCode='10115')
        get_cookie(site='uk', zipCode='W1S 3PR')
        get_cookie(site='it', zipCode='00185')
        get_cookie(site='es', zipCode='28001')
        get_cookie(site='fr', zipCode='75019')
        get_cookie(site='us', zipCode='10010')
        get_cookie(site='de', zipCode='10115')
        get_cookie(site='uk', zipCode='W1S 3PR')
        time.sleep(random.uniform(10.5, 35.5))
