# import json
# import re
#
# from curl_cffi import requests
# from lxml import etree
#
# requ_see = requests.Session()
# headers = {
#     'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
#     'Accept-Encoding': 'gzip, deflate, br, zstd',
#     'Accept-Language': 'zh-CN,zh;q=0.9',
#     'Cache-Control': 'no-cache',
#     'Pragma': 'no-cache',
#     'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
#     'Sec-Ch-Ua-Mobile': '?0',
#     'Sec-Ch-Ua-Platform': ' "Windows"',
#     'Sec-Fetch-Dest': 'document',
#     'Sec-Fetch-Mode': 'navigate',
#     'Sec-Fetch-Site': ' none',
#     'Sec-Fetch-User': '?1',
#     'Upgrade-Insecure-Requests': '1',
#     'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
# }
# asin_resp = requ_see.get('https://www.amazon.co.uk/dp/B0714LLB2T?th=1', headers=headers)
# print("第一步 请求首页")
# html_xpath = etree.HTML(asin_resp.text)
# ingress = html_xpath.xpath("//span[@id='glow-ingress-line2']/text()")
# print("第一次发送请求，获取邮编：", ingress)
# # url2 = 'https://www.amazon.co.uk/nav/ajax/hMenuDesktopFirstLayer?ajaxTemplate=hMenuDesktopFirstLayer&pageType=Detail&hmDataAjaxHint=1&isFreshRegion=false&isFreshCustomer=false&isPrimeMember=false&isPrimeDay=false&isBackup=false&firstName=false&navDeviceType=desktop&hashCustomerAndSessionId=8b35c8413eaf45f3509509691ec91ce8cc82c3f3&environmentVFI=AmazonNavigationCards%2Fdevelopment%40B6407668806-AL2_aarch64&languageCode=en_GB&customerCountryCode=US'
# # requ_see.get(url2, headers=headers)
#
# 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])
# print(data_modal)
# headers['Anti-Csrftoken-A2z'] = data_modal['ajaxHeaders']['anti-csrftoken-a2z']
# clkci_url = 'https://www.amazon.co.uk/portal-migration/hz/glow/get-rendered-address-selections?deviceType=desktop&pageType=Detail&storeContext=grocery&actionSource=desktop-modal&toasterType=AIS_INGRESS'
# headers['Referer'] = 'https://www.amazon.co.uk/dp/B0714LLB2T?th=1'
# print(headers, 23333333)
# clkci_resp = requ_see.get(clkci_url, headers=headers)
# print(clkci_resp.text)
# CSRF_TOKEN = re.findall('CSRF_TOKEN : "(.*?)",', clkci_resp.text)[0]
# print("CSRF_TOKEN:", CSRF_TOKEN)
# address_url = 'https://www.amazon.co.uk/portal-migration/hz/glow/address-change?actionSource=glow'
#
# headers_post = {
#     'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
#     'Accept-Encoding': 'gzip', }
# headers_post['Origin'] = 'https://www.amazon.co.uk'
# headers_post['Anti-Csrftoken-A2z'] = CSRF_TOKEN
# headers_post['Sec-Fetch-Site'] = 'same-origin'
# headers_post['Sec-Fetch-Mode'] = 'cors'
# headers_post['Sec-Fetch-Dest'] = 'empty'
# headers_post['Accept'] = '*/*'
# headers_post['Content-Type'] = 'application/json'
# headers_post['X-Requested-With'] = 'XMLHttpRequest'
#
# address_json = {"locationType": "LOCATION_INPUT", "zipCode": "W1S 3PR", "deviceType": "web", "storeContext": "grocery",
#                 "pageType": "Detail", "actionSource": "glow"}
# address_resp = requ_see.post(address_url, headers=headers_post, json=address_json, verify=False, impersonate="chrome")
#
# submit_headers = {
#     'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
#     'Accept-Encoding': 'gzip',
#     'Accept-Language': 'zh-CN,zh;q=0.9',
# }
# submit_headers['Accept'] = '*/*'
# submit_headers['Referer'] = 'https://www.amazon.co.uk/dp/B0714LLB2T?th=1'
# submit_headers['X-Requested-With'] = 'XMLHttpRequest'
# submit_headers['Sec-Fetch-Site'] = 'same-origin'
# submit_headers['Sec-Fetch-Mode'] = 'cors'
# submit_headers['Sec-Fetch-Dest'] = 'empty'
# detail_url = 'https://www.amazon.co.uk/portal-migration/hz/glow/get-location-label?storeContext=grocery&pageType=Detail&actionSource=desktop-modal'
# submit_resp = requ_see.get(detail_url, headers=submit_headers, verify=False, impersonate="chrome")
# print(submit_resp.text)
# print(submit_resp.cookies.get_dict(), '322222222')
# print(requ_see.cookies.get_dict(), '433333333')
# cookie_dict = requ_see.cookies.get_dict()
# index_resp = requests.get('https://www.amazon.co.uk', headers=headers, cookies=cookie_dict, verify=False,
#                           impersonate="chrome")
# index_xpath = etree.HTML(index_resp.text)
# ingress = index_xpath.xpath("//span[@id='glow-ingress-line2']/text()")
# print("获取最新邮编：", ingress)






import json
import random
import re
import time
import traceback
import pandas as pd
from curl_cffi import requests
from lxml import etree
import sys
print(sys.executable)
from amazon_every_day_spider.secure_db_client import get_remote_engine

"""
打包命令：cd /d E:\Git_new\spider\py_spider
pyinstaller -F amazon_every_day_spider\Get_Cookies.py --clean --paths . --collect-submodules amazon_every_day_spider

"""


def get_cookie(site='us', zipCode='10010'):
    try:
        params_site = {
            "us": "pet-supplies",
            "de": "toys",
            "uk": "grocery",
            "it": "luggage",
            "es": "apparel",
            "fr": "kitchen",
            "ca": "beauty",
            "mx": "beauty",
            "au": "fashion",
            "ae": "generic",
            "br": "generic",
            "nl": "home-improvement",
            "pl": "beauty",
            "se": "beauty",
            "tr": "home-improvement",
            # grocery
        }
        if site == "us":
            index_url = 'https://www.amazon.com'
            url_asin = 'https://www.amazon.com/dp/B0009X29WK'
            host = 'www.amazon.com'
        elif site == 'uk':
            index_url = 'https://www.amazon.co.uk'  # 站点url
            url_asin = 'https://www.amazon.co.uk/dp/B0714LLB2T'  # 站点url
            host = 'www.amazon.co.uk'
        elif site == 'de':
            index_url = 'https://www.amazon.de'
            url_asin = 'https://www.amazon.de/dp/B00006YYXM'
            host = 'www.amazon.de'
        elif site == 'fr':
            index_url = 'https://www.amazon.fr'
            url_asin = 'https://www.amazon.fr/dp/B0FK9JNPM5'
            host = 'www.amazon.fr'
        elif site == 'es':
            index_url = 'https://www.amazon.es'
            url_asin = 'https://www.amazon.es/dp/B0FDFVY9J6'
            host = 'www.amazon.es'
        elif site == 'it':
            index_url = 'https://www.amazon.it'
            url_asin = 'https://www.amazon.it/dp/B0F3C16GTF'
            host = 'www.amazon.it'
        elif site == 'ca':
            index_url = 'https://www.amazon.ca'
            url_asin = 'https://www.amazon.ca/dp/B08H3JPH74'
            host = 'www.amazon.ca'
        elif site == 'mx':
            index_url = "https://www.amazon.com.mx"
            url_asin = 'https://www.amazon.com.mx/dp/B08H3JPH74'
            host = 'www.amazon.com.mx'
        elif site == 'ae':
            index_url = "https://www.amazon.ae"
            url_asin = 'https://www.amazon.ae/dp/B08H3JPH74'
            host = 'www.amazon.ae'
        elif site == 'au':
            index_url = "https://www.amazon.com.au"
            url_asin = 'https://www.amazon.com.au/dp/B0D1YFSYGQ'
            host = 'www.amazon.com.au'
        elif site == 'tr':
            index_url = "https://www.amazon.com.tr"
            url_asin = 'https://www.amazon.com.tr/dp/B08SPXK5WC'
            host = 'www.amazon.com.tr'
        elif site == 'be':
            index_url = "https://www.amazon.com.be"
            url_asin = 'https://www.amazon.com.be/dp/B01B7O6JH0'
            host = 'www.amazon.com.be'
        elif site == 'jp':
            index_url = "https://www.amazon.co.jp"
            url_asin = 'https://www.amazon.co.jp/dp/B08H3JPH74'
            host = 'www.amazon.co.jp'
        elif site == 'nl':
            index_url = "https://www.amazon.nl"
            url_asin = 'https://www.amazon.nl/dp/B01COWDLGG'
            host = 'www.amazon.nl'
        elif site == 'pl':
            index_url = "https://www.amazon.pl"
            url_asin = 'https://www.amazon.pl/dp/B08H3JPH74'
            host = 'www.amazon.pl'
        elif site == 'se':
            index_url = "https://www.amazon.se"
            url_asin = 'https://www.amazon.se/dp/B08H3JPH74'
            host = 'www.amazon.se'
        elif site == 'br':
            index_url = "https://www.amazon.com.br"
            url_asin = 'https://www.amazon.com.br/dp/B08SPXK5WC'
            host = 'www.amazon.com.br'
        if site not in ['us', 'uk', 'fr', 'es', 'it', 'de']:
            engine_us = get_remote_engine('us', 'mysql')
        else:
            engine_us = get_remote_engine(site, 'mysql')

        requ_see = requests.Session()
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'Accept-Encoding': 'gzip, deflate, br, zstd',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Pragma': 'no-cache',
            'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
            'Sec-Ch-Ua-Mobile': '?0',
            'Sec-Ch-Ua-Platform': ' "Windows"',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': ' none',
            'Sec-Fetch-User': '?1',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
        }
        if site in ['au', 'pl']:
            del headers['Accept-Encoding']
            headers['Sec-Ch-Ua'] = "\"Google Chrome\";v=\"143\", \"Chromium\";v=\"143\", \"Not A(Brand\";v=\"24\""
            headers['priority'] = "u=0, i"
            headers['User-Agent'] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"

        asin_resp = requ_see.get(url_asin, headers=headers, impersonate="chrome")
        print("第一步 请求asin首页:", url_asin)
        html_xpath = etree.HTML(asin_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])
        print('获取参数anti-csrftoken-a2z：', data_modal)
        headers['Anti-Csrftoken-A2z'] = data_modal['ajaxHeaders']['anti-csrftoken-a2z']
        #                        /portal-migration/hz/glow/get-rendered-address-selections?deviceType=desktop&pageType=Detail&storeContext=grocery&actionSource=desktop-modal
        clkci_url = f'{index_url}/portal-migration/hz/glow/get-rendered-address-selections'
        # clkci_url = f'{index_url}/portal-migration/hz/glow/get-rendered-address-selections?deviceType=desktop&pageType=Detail&storeContext=grocery&actionSource=desktop-modal&toasterType=AIS_INGRESS'
        headers['Referer'] = url_asin
        print('第二步点击')
        params = {
            "deviceType": "desktop",
            "pageType": "Detail",
            "storeContext": params_site.get(site),
            "actionSource": "desktop-modal"
        }
        clkci_resp = requ_see.get(clkci_url, headers=headers, params=params, impersonate="chrome")
        CSRF_TOKEN = re.findall('CSRF_TOKEN : "(.*?)",', clkci_resp.text)[0]
        print("CSRF_TOKEN:", CSRF_TOKEN)
        # https://www.amazon.com/portal-migration/hz/glow/address-change?actionSource=glow
        # https://www.amazon.com/portal-migration/hz/glow/address-change?actionSource=glow
        # https://www.amazon.com/portal-migration/hz/glow/address-change?actionSource=glow
        address_url = f'{index_url}/portal-migration/hz/glow/address-change?actionSource=glow'
        # if site in ['tr', 'be', 'nl', 'pl', 'se']:
        #     url = f"https://{host}/portal-migration/hz/glow/get-rendered-address-selections?deviceType=desktop&pageType=Gateway&storeContext=NoStoreName&actionSource=desktop-modal"

        headers_post = {
            'Host': host,
            'accept': 'text/html,*/*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'anti-csrftoken-a2z': CSRF_TOKEN,
            'content-type': 'application/json',
            'origin': index_url,
            'referer': f'https://{host}/dp/B0009X29WK?th=1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            # 'x-amzn-flow-closure-id': '1768269613',
            'x-requested-with': 'XMLHttpRequest'
        }
        if site in ['uk', 'it', 'es', 'fr']:
            del headers_post['referer']

        address_json = {"locationType": "LOCATION_INPUT", "zipCode": f"{zipCode}", "deviceType": "web",
                        "storeContext": params_site.get(site),
                        "pageType": "Detail", "actionSource": "glow"}

        if site == 'au':
            address_json['locationType'] = 'POSTAL_CODE_WITH_CITY'
            address_json['city'] = 'WARWICK FARM'
        elif site == 'ae':
            address_json['locationType'] = 'CITY'
            address_json['city'] = 'Abu Dhabi'
            address_json['pageType'] = 'Gateway'
        elif site == 'nl':
            del address_json['zipCode']
            address_json['locationType'] = 'COUNTRY'
            address_json['district'] = 'NL'
            address_json['countryCode'] = 'NL'

        print('第三步 输入 邮编')
        print(address_url)

        post_resp = requ_see.post(address_url, headers=headers_post, json=address_json, verify=False,
                                      impersonate="chrome")
        print(post_resp.text)
        print(post_resp)
        submit_headers = {
            'User-Agent': ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
            'Accept-Encoding': 'gzip',
            'Accept-Language': 'zh-CN,zh;q=0.9',
        }
        submit_headers['Accept'] = '*/*'
        submit_headers['Referer'] = url_asin
        submit_headers['X-Requested-With'] = 'XMLHttpRequest'
        submit_headers['Sec-Fetch-Site'] = 'same-origin'
        submit_headers['Sec-Fetch-Mode'] = 'cors'
        submit_headers['Sec-Fetch-Dest'] = 'empty'
        print('第四步。提交')
        detail_url = f'{index_url}/portal-migration/hz/glow/get-location-label?storeContext=pet-supplies&pageType=Detail&actionSource=desktop-modal'

        # detail_url = f'{index_url}/portal-migration/hz/glow/get-location-label?storeContext=grocery&pageType=Detail&actionSource=desktop-modal'
        requ_see.get(detail_url, headers=submit_headers, verify=False, impersonate="chrome")
        print(requ_see.cookies.get_dict())
        cookie_dict = requ_see.cookies.get_dict()
        index_resp = requests.get(index_url, headers=headers, cookies=cookie_dict, verify=False,
                                  impersonate="chrome")
        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() or 'M5B 2H' in ingress[0].strip():
            print(f"***************    当前获取 {site} 站点 cookie   邮编  {zipCode}  ********************")
            cookies = json.dumps(cookie_dict, ensure_ascii=False)
            item = {"site": site, 'zipCode': ingress[0].strip(), 'cookie': cookies}
            print(item)
            # 构造 DataFrame
            if site not in ['us', 'uk', 'fr', 'es', 'it', 'de']:
                df = pd.DataFrame([{"cookies": cookies, 'site': site}])
                # 存储到数据库
                engine_us.to_sql(df, 'other_site_cookies', if_exists="append")
                print(f"入库成功 {site} other_site_cookies")
            else:
                # 构造 DataFrame
                df = pd.DataFrame([{"cookies": cookies}])
                # 存储到数据库
                engine_us.to_sql(df, f"{site}_comment_cookies", if_exists="append")
                print(f"入库成功 {site} {site}_comment_cookies")

        print('\n')
    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='us', zipCode='10010')
        time.sleep(random.uniform(60.5, 180.5))
# ae
# au
# be
# br
# ca
# mx
# nl
# pl
# se
# tr
