import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from lxml import etree
import time
from selenium.webdriver.support import expected_conditions as EC
from sqlalchemy import create_engine
import pandas as pd
import random
from sqlalchemy.engine import URL


class dow_seller_help():
    def __init__(self):
        DB_CONN_DICT = {
            "mysql_port": 3306,
            "mysql_user": "XP_Yswg2025_PY",
            "mysql_pwd": "Gd1pGJog1ysLMLBdML8w81",
            "mysql_host": "rm-wz9yg9bsb2zf01ea4yo.mysql.rds.aliyuncs.com",
        }
        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)

    def db_(self):
        DB_CONN_DICT = {
            "mysql_port": 3306,
            "mysql_user": "XP_Yswg2025_PY",
            "mysql_pwd": "Gd1pGJog1ysLMLBdML8w81",
            "mysql_host": "rm-wz9yg9bsb2zf01ea4yo.mysql.rds.aliyuncs.com",
        }
        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)


    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)
            # driver = webdriver.Chrome(f"{self.path_driver}/chromedriver.exe", options=chrome_options)
            return driver

    def get_help_hub(self, driver):
        driver.get('https://sellercentral.amazon.com/help/hub/reference')
        time.sleep(5)
        return driver

    def save_data(self, data_list):
        df_seller_help = pd.DataFrame(data=data_list, columns=['category', 'outer_html', 'number'])
        df_seller_help.to_sql('seller_help_html', con=self.engine_us_mysql, if_exists='append', index=False)

    # def get_category_url(self):
    #     # 调用 get_category_url 存储每个大分类下的所有url
    #     with open(r'C:\Users\ASUS\Downloads\Merch Collab-html.html', 'r', encoding='utf-8')as f:
    #         # with open(r'新建文本文档.html', 'r', encoding='utf-8')as f:
    #         resp = f.read()
    #     resp_html = etree.HTML(resp)
    #     all_a_list = resp_html.xpath("//kat-tab[@id='hh-nav-tree-widget-tab']//a")
    #     print('all_a_list::', len(all_a_list))
    #     data = []
    #     for _a in all_a_list[21:]:
    #         title_href = _a.xpath('./@href')
    #         title = _a.xpath('.//span/text()')
    #         num = re.findall(r'reference/(.*)', title_href[0])
    #         data.append([title[0], title_href[0], num[0]])
    #     print(data)
    #     print(len(data))
    #     df_seller_help = pd.DataFrame(data=data, columns=['zh_name', 'url', 'number'])
    #     df_seller_help.drop_duplicates(['number'], inplace=True)
    #     df_seller_help.to_sql('seller_help_syn', con=self.engine_us_mysql, if_exists='append', index=False)

    def select_url(self):
        sql = 'select zh_name, number, url from seller_help_syn where state=1'
        syn_data_df = pd.read_sql(sql, con=self.engine_us_mysql)
        syn_data_list = list(syn_data_df.zh_name + '|' + syn_data_df.number + '|' + syn_data_df.url)
        return syn_data_list

    def update_url_state(self, number):
        with self.engine_us_mysql.begin() as conn:
            up_sql = f"UPDATE seller_help_syn set state=3 WHERE number='{number}'"
            print('up_sql:', up_sql)
            conn.execute(up_sql)

    def run(self):
        driver = self.download_day_st()
        driver = self.get_help_hub(driver)
        time.sleep(5)
        syn_data_list = self.select_url()
        for syn_data in syn_data_list:
            categroy_data_list = syn_data.split('|')
            print(categroy_data_list)
            for i in range(3):
                try:
                    data_list = []
                    title_name = categroy_data_list[0]
                    title_href = categroy_data_list[2]
                    number = categroy_data_list[1]
                    print('小类链接', title_href)
                    print('具体小类名称', title_name)
                    print('编码', number)
                    url = 'https://sellercentral.amazon.com' + title_href
                    print('请求url:', url)
                    driver.get(url)
                    time.sleep(5)
                    wait = WebDriverWait(driver, 15)  # 最多等待 10 秒
                    element = wait.until(EC.presence_of_element_located((By.ID, "full-help-page")))
                    html = etree.HTML(driver.page_source)
                    label_list = html.xpath("//kat-breadcrumb[@role='list']//kat-breadcrumb-item/@label")
                    jion_label = ' _ '.join(label_list).replace(' _ ...', '')
                    jion_label = jion_label.replace('...', '').replace('\n', '')
                    div_element = driver.find_element(By.ID, "full-help-page")
                    # 获取 div 内部的所有 HTML 标签
                    inner_html = div_element.get_attribute("innerHTML")
                    print(inner_html)  # 打印 div 内部的所有 HTML 代码
                    data_list.append([jion_label, inner_html, number, ])
                    self.save_data(data_list)
                    print('jion_label:::', jion_label)
                    self.update_url_state(number)
                    time.sleep(random.uniform(3, 10))
                    break
                except Exception as e:
                    print('报错：', e)
                    time.sleep(15)
                    driver.refresh()
                    self.db_()
                    continue


if __name__ == '__main__':
    dow_seller_help().run()
