import os
import random
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from sqlalchemy import create_engine
from lxml import etree
import re
import time
import traceback
import datetime
import json
import sys
from sqlalchemy.engine import URL

syn_state = False

click_product_name_list = []
# 类目分析
class dow_category_Product():
    def __init__(self, site):
        self.site_name = site

    def mysql_connect(self, site='us'):
        if site == 'us':
            db = 'selection'
        else:
            db = f'selection_{site}'
        DB_CONN_DICT = {
            "mysql_port": 3306,
            "mysql_user": "XP_Yswg2025_PY",
            "mysql_pwd": "Gd1pGJog1ysLMLBdML8w81",
            "mysql_host": "rm-wz9yg9bsb2zf01ea4yo.mysql.rds.aliyuncs.com",
        }
        url = URL.create(
            drivername="mysql+pymysql",
            username=DB_CONN_DICT["mysql_user"],
            password=DB_CONN_DICT["mysql_pwd"],  # 原始密码，含 @ 也没问题
            host=DB_CONN_DICT["mysql_host"],
            port=int(DB_CONN_DICT["mysql_port"]),
            database=db,
            query={"charset": "utf8mb4"}
        )
        self.engine_mysql = create_engine(
            url)
        url_us = URL.create(
            drivername="mysql+pymysql",
            username=DB_CONN_DICT["mysql_user"],
            password=DB_CONN_DICT["mysql_pwd"],  # 原始密码，含 @ 也没问题
            host=DB_CONN_DICT["mysql_host"],
            port=int(DB_CONN_DICT["mysql_port"]),
            database='selection',
            query={"charset": "utf8mb4"}
        )
        self.engine_us_mysql = create_engine(
            url_us)
        self.engine_pg = create_engine(
            f"postgresql+psycopg2://postgres:F9kL2sXe81rZq@113.100.143.162:5432/{db}",
            encoding='utf-8')
        self.num = 0
        week = time.strftime("%W")
        yaer = time.strftime('%Y', time.localtime(time.time()))
        self.y_w = f"{yaer}-{week}"

    def download_day_st(self, site='us'):
        print(f'下载 {site} 站点数据')
        for i in range(1):
            try:
                pr_name = "chrome.exe"
                os.system('%s%s' % ("taskkill /F /IM ", pr_name))
            except Exception as e:
                print("强制关闭chrome.exe失败：", e)
            port = 9222
            params_ = ""
            # params_ = "--blink-settings=imagesEnabled=false"
            os.system(f'start Chrome {params_} --remote-debugging-port={port}')
            chrome_options = Options()
            # 禁止加载图片
            chrome_options.add_argument('--blink-settings=imagesEnabled=false')  # 这样可以；；；；激动
            chrome_options.add_experimental_option("debuggerAddress", f"127.0.0.1:{port}")
            driver = webdriver.Chrome(r'chromedriver.exe', options=chrome_options)
            # 无界面模式
            # chrome_options.add_argument('--headless')
            # 禁用 GPU，加快在 headless 下的渲染
            chrome_options.add_argument('--disable-gpu')
            # 禁用沙箱，规避权限问题
            chrome_options.add_argument('--no‑sandbox')
            # 改用 /tmp 而不是 /dev/shm（避免共享内存不足）
            chrome_options.add_argument('--disable-dev-shm-usage')
            # 其他可以尝试的降配项
            chrome_options.add_argument('--disable-extensions')
            chrome_options.add_argument('--disable-application-cache')
            chrome_options.add_argument('--disable-background-timer-throttling')
            chrome_options.add_argument('--disable-backgrounding-occluded-windows')
            chrome_options.add_argument('--disable-renderer-backgrounding')
            chrome_options.add_argument('--no-sandbox')
            chrome_options.add_argument('--disable-dev-shm-usage')
            # 彻底关掉大部分渲染路径
            chrome_options.add_argument('--disable-gpu-compositing')
            chrome_options.add_argument('--disable-2d-canvas-clip-aa')
            chrome_options.add_argument('--disable-2d-canvas-cpu-path')
            chrome_options.add_argument('--disable-accelerated-2d-canvas')
            opts = chrome_options
            # 限制渲染进程数
            opts.add_argument('--renderer-process-limit=1')
            # 限制实用进程数（utility）
            opts.add_argument('--utility-process-limit=1')
            # 关闭 Site Isolation
            opts.add_argument('--disable-site-isolation-trials')

            self.get_category(site, driver)

    def get_category(self, site, driver):
        for i in range(2):
            try:
                driver.get('https://sellercentral.amazon.com/gp/homepage.html/ref=xx_home_logo_xx?')
                time.sleep(random.uniform(8, 25.25))
                driver.get('https://sellercentral.amazon.com/selection/category-insights')
                time.sleep(random.uniform(8, 25.25))
                break
            except:
                time.sleep(5)
        time.sleep(1)
        if site == 'us':
            driver.execute_script(
                'document.querySelector("#ATVPDKIKX0DER > kat-radiobutton").shadowRoot.querySelector("div > div.text > slot > kat-label:nth-child(1)").click()')
        elif site == 'uk':
            driver.execute_script(
                'document.querySelector("#A1F83G8C2ARO7P > kat-radiobutton").shadowRoot.querySelector("div > div.text > slot > kat-label:nth-child(1)").click()')
        elif site == 'de':
            driver.execute_script(
                'document.querySelector("#A1PA6795UKMFR9 > kat-radiobutton").shadowRoot.querySelector("div > div.text > slot > kat-label:nth-child(1)").click()')
        time.sleep(random.uniform(8, 15.25))
        html = etree.HTML(driver.page_source)
        self.save_category(html)
        print(333333333333333333333333)
        global syn_state
        syn_state = True
        Category_list = self.read_category()
        if Category_list:
            self.get_category_data(Category_list, driver, site)

    def cilik_site(self, driver):
        if self.site_name == 'us':
            driver.execute_script(
                'document.querySelector("#ATVPDKIKX0DER > kat-radiobutton").shadowRoot.querySelector("div > div.text > slot > kat-label:nth-child(1)").click()')
        elif self.site_name == 'uk':
            driver.execute_script(
                'document.querySelector("#A1F83G8C2ARO7P > kat-radiobutton").shadowRoot.querySelector("div > div.text > slot > kat-label:nth-child(1)").click()')
        elif site == 'de':
            driver.execute_script(
                'document.querySelector("#A1PA6795UKMFR9 > kat-radiobutton").shadowRoot.querySelector("div > div.text > slot > kat-label:nth-child(1)").click()')

    def get_category_data(self, Category_list, driver, site):
        print('Category_list:::', Category_list)
        num = 0
        print('click_product_name_list::',click_product_name_list)
        for Category in Category_list:
            self.cilik_site(driver)
            print(Category, '   22222222222222222222222222222222222222')
            try:
                num += 1
                Category_name = Category
                # _Category = Category.replace('&', '\\\&')
                print("Category_name 名称 11111", Category)
                driver.execute_script(f"""document.querySelector("kat-radiobutton[label='{Category}']").click()""")
                time.sleep(2)
                html = etree.HTML(driver.page_source)
                Product_Type_list = html.xpath(
                    '//h2[contains(text(),"Product Type")]/following-sibling::div/div')
                product_nums = 0
                for Product_Type in Product_Type_list:
                    product_nums+=1
                    time.sleep(0.5)
                    driver.execute_script("localStorage.clear();")  # 清除本地存储
                    time.sleep(0.5)
                    # driver.execute_script("sessionStorage.clear();")  # 清除会话存储
                    time.sleep(0.5)
                    driver.execute_script(
                        "caches.keys().then(function(names) { for (let name of names) { caches.delete(name); } });")
                    driver.execute_script("window.performance.clearResourceTimings();")
                    time.sleep(1)
                    # 假设你已经有了 driver
                    # 先 enable heap profiler
                    driver.execute_cdp_cmd('HeapProfiler.enable', {})
                    # 然后强制 GC
                    driver.execute_cdp_cmd('HeapProfiler.collectGarbage', {})
                    # 最后可选地 disable 掉
                    driver.execute_cdp_cmd('HeapProfiler.disable', {})
                    try:
                        time.sleep(2)
                        save_Category_list = []
                        Product_name = Product_Type.xpath('./@id')
                        print("Product_name3222222222::", Product_name[0].upper())
                        print('click_product_name_list::', click_product_name_list)
                        if Product_name[0] in click_product_name_list:
                            continue
                        click_product_name_list.append(Product_name[0])
                        driver.execute_script(f"document.querySelector('#{Product_name[0]} > kat-radiobutton').click()")
                        time.sleep(2)
                        html = etree.HTML(driver.page_source)
                        Item_Type_Keyword_id_list = html.xpath(
                            '//h2[contains(text(),"Item Type Keyword")]/following-sibling::div/div')
                        for Item_Type_Keyword_id in Item_Type_Keyword_id_list:
                            try:
                                most_popular_keyword_list = []
                                reasons_returns_json = None
                                most_popular_json_dict = None
                                Keyword_id = Item_Type_Keyword_id.xpath('./@id')
                                print("Keyword_id:", Keyword_id)
                                Keyword = html.xpath(f"//div[@id='{Keyword_id[0]}']/kat-radiobutton/@label")
                                print('Keyword', Keyword)
                                driver.find_element(By.XPATH, f'//kat-radiobutton[@value="{Keyword_id[0]}"]').click()
                                time.sleep(2.3)
                                html_1 = etree.HTML(driver.page_source)
                                most_popular_list = html_1.xpath(
                                    "//div[@class='most-popular-keywords-container']/kat-list//li")
                                if most_popular_list:
                                    for most_popular in most_popular_list:
                                        most_popular_keyword = most_popular.xpath('.//div[2]/text()')[0]
                                        most_popular_b_nums = most_popular.xpath('.//div/b/text()')[0]
                                        most_popular_dict = {"most_popular_keywords": most_popular_keyword,
                                                             'most_popular_search_nums': most_popular_b_nums}
                                        most_popular_keyword_list.append(most_popular_dict)
                                    print('most_popular_keyword_list::', most_popular_keyword_list)
                                    most_popular_json_dict = json.dumps(most_popular_keyword_list)

                                div_list = html_1.xpath("//div[@class='percentage-list-item-container']/div")
                                if div_list:
                                    reasons_returns_list = []
                                    for div in div_list:
                                        values = div.xpath("./div[@class='value']/text()")
                                        value = values[0] if values else None
                                        strings = div.xpath("./div[@class='string']/text()")
                                        string = strings[0] if strings else None
                                        value_string_dict = {"value": value, 'string': string}
                                        print(value_string_dict)
                                        reasons_returns_list.append(value_string_dict)
                                    reasons_returns_json = json.dumps(reasons_returns_list)
                                    print('reasons_returns_json:', reasons_returns_json)
                                '1.86‰ product type average'
                                print('测试')
                                ratio_list = html_1.xpath(
                                    '//div[@class="big-text-section-name"][1]/div[@class="big-text"]/text()')
                                if ratio_list:
                                    search_ratio = re.findall(r'(.*?)‰', ratio_list[0])[0]  # 搜索购买比率
                                    return_ratio = re.findall(r'(.*?)%', ratio_list[1])[0]  # 回报率
                                else:
                                    search_ratio = None
                                    return_ratio = None
                                product_ratio_list = html_1.xpath(
                                    '//div[@class="big-text-section-name"][1]/div[@class="sub-text"]/text()')
                                if product_ratio_list:
                                    product_average = re.findall(r'(.*?)‰', product_ratio_list[0])[0]  # 产品类型平均值
                                    return_product_average = re.findall(r'(.*?)%', product_ratio_list[1])[
                                        0]  # 产品类型回报率平均值
                                else:
                                    product_average = None
                                    return_product_average = None
                                print('测试2')
                                big_text_sllers = html_1.xpath(
                                    '//h4[contains(text(),"Number of sellers")]/parent::div/following-sibling::div/text()')  # 卖家数量
                                big_text_sller = big_text_sllers[0] if big_text_sllers else None
                                big_text_brands = html_1.xpath(
                                    '//h4[contains(text(),"Number of new brands")]/parent::div/following-sibling::div/text()')  # 品牌数量
                                big_text_brand = big_text_brands[0] if big_text_brands else None
                                big_text_asins = html_1.xpath(
                                    '//h4[contains(text(),"Number of ASINs")]/parent::div/following-sibling::div/text()')  # asin 数量
                                big_text_asin = big_text_asins[0] if big_text_asins else None
                                big_text_new_asins = html_1.xpath(
                                    '//h4[contains(text(),"Number of new ASINs")]/parent::div/following-sibling::div/text()')  # 新asin 数量
                                big_text_new_asin = big_text_new_asins[0] if big_text_new_asins else None
                                big_text_per_asins = html_1.xpath(
                                    '//h4[contains(text(),"Offers per ASIN")]/parent::div/following-sibling::div/text()')  # 每个asin平均报价数量
                                big_text_per_asin = big_text_per_asins[0] if big_text_per_asins else None
                                big_text_Advertisement_list = html_1.xpath(
                                    '//h4[contains(text(),"Advertisement Spend")]/parent::div/following-sibling::div//text()')  # 广告支出
                                if big_text_Advertisement_list:
                                    big_text_Advertisement = '|-|'.join(big_text_Advertisement_list)
                                else:
                                    big_text_Advertisement = None
                                big_text_star_list = html_1.xpath(
                                    '//h4[contains(text(),"Star Ratings")]/parent::div/parent::div/parent::div/div//@width')  # 星级的宽度
                                if big_text_star_list:
                                    del big_text_star_list[0]
                                    big_text_star = '|-|'.join(big_text_star_list)
                                else:
                                    big_text_star = None
                                print('测试3')
                                if big_text_star:
                                    sta_list = big_text_star.split('|-|')
                                    # 初始化总和为0
                                    total = 0
                                    for num_str in sta_list:
                                        # 将数字字符串转换为浮点数
                                        num = float(num_str)
                                        # 将数字相加
                                        total += num
                                    print('总数：', total)
                                    # 计算每个数字的结果，跳过0
                                    results = [float(num) / total if float(num) != 0 else 0 for num in sta_list]
                                    five_star = round(results[0], 2)
                                    three_star = round(results[1], 2)
                                    two_star = round(results[2], 2)
                                    one_star = round(results[3], 2)
                                else:
                                    five_star = 0
                                    three_star = 0
                                    two_star = 0
                                    one_star = 0
                                if big_text_Advertisement:
                                    # 定义正则表达式模式，匹配$后面的金额
                                    if self.site_name == 'us':
                                        pattern = r'\$([\d.]+)'
                                    elif self.site_name == 'uk':
                                        pattern = r'\£([\d.]+)'
                                    elif self.site_name == 'de':
                                        pattern = r'\€([\d.]+)'
                                    # 使用findall函数找到所有匹配的金额
                                    matches_list = re.findall(pattern, big_text_Advertisement)
                                    ad_spend = matches_list[0]
                                    majority_spend = matches_list[1]
                                else:
                                    ad_spend = 0
                                    majority_spend = 0
                                print("week 周：", self.y_w)
                                print(five_star, three_star, two_star, one_star)
                                print(ad_spend, majority_spend)
                                print('原始数据')
                                print([big_text_sller, big_text_brand, big_text_asin, big_text_new_asin,
                                       big_text_per_asin, big_text_Advertisement, big_text_star])
                                # 品牌
                                if big_text_brand:
                                    if 'K' in big_text_brand:
                                        big_brand = big_text_brand.replace('K', '')
                                        big_brand_int = int(float(big_brand) * 1000)
                                    elif 'M' in big_text_brand:
                                        big_brand = big_text_brand.replace('M', '')
                                        big_brand_int = int(float(big_brand) * 10000)
                                    else:
                                        big_brand_int = int(big_text_brand)
                                else:
                                    big_brand_int = None
                                    # asin
                                if big_text_asin:
                                    if 'K' in big_text_asin:
                                        big_asin = big_text_asin.replace('K', '')
                                        big_asin_int = int(float(big_asin) * 1000)
                                    elif 'M' in big_text_asin:
                                        big_asin = big_text_asin.replace('M', '')
                                        big_asin_int = int(float(big_asin) * 10000)
                                    else:
                                        big_asin_int = int(big_text_asin)
                                else:
                                    big_asin_int = None
                                # 新出现asin
                                if big_text_new_asin:
                                    if 'K' in big_text_new_asin:
                                        big_new_asin = big_text_new_asin.replace('K', '')
                                        big_new_asin_int = int(float(big_new_asin) * 1000)
                                    elif 'M' in big_text_new_asin:
                                        big_new_asin = big_text_new_asin.replace('M', '')
                                        big_new_asin_int = int(float(big_new_asin) * 10000)
                                    else:
                                        big_new_asin_int = int(big_text_new_asin)
                                else:
                                    big_new_asin_int = None
                                # 报价 asin
                                if big_text_per_asin:
                                    if 'K' in big_text_per_asin:
                                        big_per_asin = big_text_per_asin.replace('K', '')
                                        big_per_asin_int = int(float(big_per_asin) * 1000)
                                    elif 'M' in big_text_per_asin:
                                        big_per_asin = big_text_per_asin.replace('M', '')
                                        big_per_asin_int = int(float(big_per_asin) * 10000)
                                    else:
                                        big_per_asin_int = int(big_text_per_asin)
                                else:
                                    big_per_asin_int = None
                                print('转成int')
                                print([big_brand_int, big_asin_int,
                                       big_new_asin_int, big_per_asin_int])
                                top_data_json = self.new_top_grossing(driver, 'Top')
                                time.sleep(1)
                                news_data_json = self.new_top_grossing(driver, 'News')
                                time.sleep(1)
                                save_Category_list.append(
                                    [Category_name, Product_name[0], Keyword[0], float(search_ratio),
                                     float(product_average), float(return_ratio), float(return_product_average),
                                     self.y_w, big_text_sller, big_text_brand, big_text_asin, big_text_new_asin,
                                     big_text_per_asin, big_text_Advertisement, big_text_star, big_brand_int,
                                     big_asin_int, big_new_asin_int, big_per_asin_int, five_star, three_star, two_star,
                                     one_star, ad_spend, majority_spend, most_popular_json_dict, reasons_returns_json,
                                     top_data_json, news_data_json])
                                print('数据：', save_Category_list)
                            except:
                                print('============  下标。超出 。 ==========')
                                continue
                        print('存储数据长度：', len(save_Category_list))
                        while True:
                            try:
                                if save_Category_list:
                                    with self.engine_mysql.begin() as conn_mysql:
                                        for i in save_Category_list:
                                            dele_sql = f"DELETE from {site}_aba_profit_category_insights where category='{i[0]}' and product_type='{i[1]}' and item_type_keyword='{i[2]}' and year_week='{self.y_w}'"
                                            print('删除删除mysql：', dele_sql)
                                            conn_mysql.execute(dele_sql)
                                    df = pd.DataFrame(data=save_Category_list,
                                                      columns=['category', "product_type", "item_type_keyword",
                                                               "search_ratio", "product_average", "return_ratio",
                                                               "return_product_average", "year_week", 'sellers',
                                                               'new_brands',
                                                               'asin', 'new_asin', 'per_asin', 'advertisement_spend',
                                                               'star_ratings', 'new_brands_int', 'asin_int',
                                                               'new_asin_int', 'per_asin_int', 'five_star',
                                                               'three_star', 'two_star', 'one_star', 'ad_spend',
                                                               'majority_spend', 'most_popular_keywords_item',
                                                               'reasons_returns_json', 'top_data_json', 'news_data_json'
                                                               ])
                                    df.to_sql(f'{site}_aba_profit_category_insights', con=self.engine_mysql,
                                              if_exists="append", index=False)
                                    print('存储成功 mysql')
                                    with self.engine_pg.begin() as conn_pg:
                                        for i in save_Category_list:
                                            dele_sql = f"DELETE from {site}_aba_profit_category_insights where category='{i[0]}' and product_type='{i[1]}' and item_type_keyword='{i[2]}' and year_week='{self.y_w}'"
                                            print('删除删除删除pg：', dele_sql)
                                            conn_pg.execute(dele_sql)
                                    df = pd.DataFrame(data=save_Category_list,
                                                      columns=['category', "product_type", "item_type_keyword",
                                                               "search_ratio", "product_average", "return_ratio",
                                                               "return_product_average", "year_week", 'sellers',
                                                               'new_brands',
                                                               'asin', 'new_asin', 'per_asin', 'advertisement_spend',
                                                               'star_ratings', 'new_brands_int', 'asin_int',
                                                               'new_asin_int', 'per_asin_int', 'five_star',
                                                               'three_star', 'two_star', 'one_star', 'ad_spend',
                                                               'majority_spend', 'most_popular_keywords_item',
                                                               'reasons_returns_json', 'top_data_json',
                                                               'news_data_json'])
                                    df.to_sql(f'{site}_aba_profit_category_insights', con=self.engine_pg,
                                              if_exists="append", index=False)
                                    print(save_Category_list)
                                    print('存储成功 pg')
                                break
                            except Exception as e:
                                print('存储报错。。。。：', e, '33333333333333333333333333232323')
                                time.sleep(30)
                                continue
                    except Exception as e:
                        print('============  产品分类 下标。超出 。无数据 ==========', f"\n{traceback.format_exc()}")
                        time.sleep(2)
                        continue
                    if product_nums>15:
                        time.sleep(2)
                        print('product_nums 重新启动 浏览器，')
                        driver.close()
                        driver.quit()
                        time.sleep(2)
                        self.run()
            except Exception as e:
                print(e, '执行错误')
                time.sleep(random.uniform(10, 20))
                self.reboot_driver(driver, site)  # 重启刷新
            with self.engine_pg.begin() as conn:
                update_sql = f"update seller_category_insights_syn set state =3 where category='{Category}'"
                print('更新update_sql：', update_sql)
                conn.execute(update_sql)
            if num > 1:
                driver.close()
                driver.quit()
                print('重新启动 浏览器，')
                self.run()
        workflow_everyday_list = [
            [self.site_name, self.y_w, '类目分析抓取完成', 3, f'{self.site_name}_aba_profit_category_insights', 'week', '类目分析',
             '是']]
        df_seller_asin_account = pd.DataFrame(data=workflow_everyday_list,
                                              columns=['site_name', 'date_info', 'status', 'status_val',
                                                       'table_name', 'date_type', 'page', 'is_end'])
        df_seller_asin_account.to_sql('workflow_progress', con=self.engine_us_mysql, if_exists='append', index=False)

    def analysis_top_Newly_html(self, driver):
        html_top = etree.HTML(driver.page_source)
        # 10 Products | Aggregate sales (past 12 months): $103,698,296.00 | 1,222,405 units
        products_aggregate_sales = html_top.xpath("//div[@class='sa-aggregation-label']/text()")
        # 获取10个产品数据
        div_list = html_top.xpath("//div[@aria-label='Press SPACE to select this row.']")
        items_list = []
        seen = set()
        for div in div_list:
            items = {}
            img_src_list = div.xpath('./div//img/@src')
            img_src = img_src_list[0] if img_src_list else None
            a_href_list = div.xpath(".//div//a/@href")
            a_href = a_href_list[0] if a_href_list else None
            title_list = div.xpath("./div//span[@class='popover-content']/text()")
            title = title_list[0] if title_list else None
            brand_list = div.xpath(".//span[contains(@id,'cell-brandName')]//div/text()")  # cell-renderer-popover
            brand = brand_list[0] if brand_list else None
            bsr_rank_list = div.xpath(".//span[contains(@id,'cell-bsrBnRank')]//div/text()")  # cell-renderer-popover
            bsr_rank = bsr_rank_list[0] if bsr_rank_list else None
            buy_price_list = div.xpath(".//span[contains(@id,'cell-buyBoxPrice')]//div/text()")  # cell-renderer-popover
            buy_price = buy_price_list[0] if buy_price_list else None
            # 'cell-avgRating-184 //span[contains(@id,"avgRating")]//kat-star-rating/value/review'
            rating_list = div.xpath(
                ".//span[contains(@id,'avgRating')]//kat-star-rating/@value")  # cell-renderer-popover
            rating = rating_list[0] if rating_list else None
            review_list = div.xpath(
                ".//span[contains(@id,'avgRating')]//kat-star-rating/@review")  # cell-renderer-popover
            review = review_list[0] if review_list else None
            offers_list = div.xpath(".//span[contains(@id,'cell-offerCount')]/text()")  # cell-renderer-popover
            offers = offers_list[0] if offers_list else None
            key = (img_src, a_href, title, brand, bsr_rank, buy_price, rating, review, offers)

            if key in seen:
                continue
            seen.add(key)
            if img_src and a_href and title and brand:
                items['img_src'] = img_src
                items['a_href'] = a_href
                items['title'] = title
                items['brand'] = brand
                items['bsr_rank'] = bsr_rank
                items['buy_price'] = buy_price
                items['rating'] = rating
                items['review'] = review
                items['offers'] = offers
                items_list.append(items)
        print(items_list)

        print(products_aggregate_sales)
        # Most Popular Keywords
        most_popular_list = html_top.xpath(
            "//div[@class='most-popular-keywords-container']/kat-list//li")
        most_popular_keyword_list = []
        if most_popular_list:
            for most_popular in most_popular_list:
                most_popular_keyword = most_popular.xpath('.//div[2]/text()')[0]
                most_popular_b_nums = most_popular.xpath('.//div/b/text()')[0]
                most_popular_dict = {"most_popular_keywords": most_popular_keyword,
                                     'most_popular_search_nums': most_popular_b_nums}
                most_popular_keyword_list.append(most_popular_dict)
            print('most_popular_keyword_list::', most_popular_keyword_list)

        # Search to purchase ratio |  Return Ratio
        top_ratio_list = html_top.xpath('//div[@class="big-text-section-name"][1]/div[@class="big-text"]/text()')
        if top_ratio_list:
            search_ratio = re.findall(r'(.*?)‰', top_ratio_list[0])[0]  # 搜索购买比率
            return_ratio = re.findall(r'(.*?)%', top_ratio_list[1])[0]  # 回报率
        else:
            search_ratio = None
            return_ratio = None

        big_text_Advertisement_list = html_top.xpath(
            '//h4[contains(text(),"Advertisement Spend")]/parent::div/following-sibling::div//text()')  # 广告支出
        if big_text_Advertisement_list:
            big_text_Advertisement = '|-|'.join(big_text_Advertisement_list)
        else:
            big_text_Advertisement = None

        big_text_star_list = html_top.xpath(
            '//h4[contains(text(),"Star Ratings")]/parent::div/parent::div/parent::div/div//@width')  # 星级的宽度
        if big_text_star_list:
            del big_text_star_list[0]
            big_text_star = '|-|'.join(big_text_star_list)
        else:
            big_text_star = None

        data_dict = {'products_aggregate_sales': products_aggregate_sales, 'asin_json': items_list,
                     'most_popular_json_dict': most_popular_keyword_list, 'search_ratio': search_ratio,
                     'return_ratio': return_ratio,
                     'big_text_Advertisement': big_text_Advertisement, 'big_text_star': big_text_star}
        print('data_dict')
        return json.dumps(data_dict)

    def new_top_grossing(self, driver, click_type):
        # Top Grossing
        if click_type == 'Top':
            print('点击Top Grossing')
            time.sleep(1)
            driver.execute_script(
                f'document.querySelector("#section_id > kat-radiobutton:nth-child(2) > kat-label").click()')
            time.sleep(1)
            top_data_dict = self.analysis_top_Newly_html(driver)
            return top_data_dict
        else:
            # Newly launched
            print('点击Newly launched')
            time.sleep(1)
            driver.execute_script(
                f'document.querySelector("#section_id > kat-radiobutton:nth-child(3) > kat-label").click()')
            time.sleep(1)
            newly_data_dict = self.analysis_top_Newly_html(driver)
            return newly_data_dict

    def reboot_driver(self, driver, site):
        # 重启刷新
        while True:
            try:
                driver.get('https://sellercentral.amazon.com/selection/category-insights')
                time.sleep(random.uniform(10, 20))
                driver.execute_script(
                    'document.querySelector("#ATVPDKIKX0DER > kat-radiobutton").shadowRoot.querySelector("div > div.text > slot > kat-label:nth-child(1)").click()')
                time.sleep(2)
                break
            except Exception as e:
                print('reboot_driver详细报错')
                print(traceback.format_exc(e))
                driver.close()
                driver.quit()
                time.sleep(5)
                self.run()
                time.sleep(5)
        Category_list = self.read_category()
        if Category_list:
            time.sleep(5)
            self.get_category_data(Category_list, driver, site)

    def read_category(self):
        # 接着上次中断的继续
        print('接着上次中断的继续')
        self.mysql_connect(site=self.site_name)
        select_sql = 'select category  from seller_category_insights_syn where state =1'
        df = pd.read_sql(select_sql, con=self.engine_pg)
        category_list = list(df.category)
        if category_list:
            return category_list
        else:
            self.mysql_connect()
            workflow_everyday_list = [
                [self.site_name, self.y_w, '类目分析抓取完成', 3, f'{self.site_name}_aba_profit_category_insights', 'week',
                 '类目分析', '是']]
            df_seller_asin_account = pd.DataFrame(data=workflow_everyday_list,
                                                  columns=['site_name', 'date_info', 'status', 'status_val',
                                                           'table_name', 'date_type', 'page', 'is_end'])
            df_seller_asin_account.to_sql('workflow_progress', con=self.engine_us_mysql, if_exists='append',
                                          index=False)

    def save_category(self, html):
        Category_list = html.xpath('//h2[contains(text(),"Category")]/following-sibling::div/div')
        Categorys_list = []
        self.category_item = {}
        Categorys_list_syn = []
        for Category in Category_list:
            Category_name = Category.xpath('./@id')
            Category_label = Category.xpath('.//@label')
            self.category_item[Category_label[0]] = Category_name[0]
            Categorys_list.append(Category_name[0])
            Categorys_list_syn.append([Category_label[0]])
        print(Categorys_list, '11111111111111111')
        if Categorys_list:
            print('插入数据')
            with self.engine_pg.begin() as conn:
                if syn_state == False:
                    # pass
                    now = datetime.datetime.now()
                    is_monday = (now.weekday() == 0)  # 0表示星期一
                    # is_920_am = (now.hour == 9 and now.minute == 20)
                    is_9_am = (now.hour == 1)
                    if is_monday and is_9_am:
                        print("当前时间是每周一早上9点20分")
                        TRUNCATE_SQL = 'TRUNCATE seller_category_insights_syn'
                        conn.execute(TRUNCATE_SQL)
                    conn.execute(
                        f'INSERT INTO seller_category_insights_syn (category) VALUES (%s) ON CONFLICT (category) DO UPDATE SET category = EXCLUDED.category',
                        Categorys_list_syn)
                print('存储初始数据成功')
        return Categorys_list

    def run(self):
        self.mysql_connect(site=self.site_name)
        self.download_day_st(site=self.site_name)


if __name__ == '__main__':
    site = sys.argv[1]
    dow_category_Product(site).run()
