import sys
import os
sys.path.append(os.path.dirname(sys.path[0]))  # 上级目录
from utils.db_connect import BaseUtils
# 数据库连接
engine_strrocks = BaseUtils().doris_connect()
engine_14 = BaseUtils().pg_connect()
import pandas as pd
import json
import gzip
import ast


class Parse_asin_html():
    # 解压缩字符串
    def decompress_string(self, input_bytes):
        if isinstance(input_bytes, str):
            input_bytes = ast.literal_eval(input_bytes)
        return gzip.decompress(input_bytes).decode('utf-8')

    def asin_html(self, site_name='us', month='05'):

        # sql = f"SELECT asin,html FROM asin_html_2025_{month} WHERE asin in ('B07DC7CWPZ') and site_name = '{site_name}'"
        sql = f"SELECT asin,html FROM asin_html_2025_{month} WHERE  site_name = '{site_name}' and asin='B0CZ72QWWL'"
        print(sql)
        df = pd.read_sql(sql, con=engine_strrocks)
        if not df.empty:
            for index, row in df.iterrows():
                asin = row['asin']
                html = row['html']
                resp = self.decompress_string(html)
                with open(rf'D:\新建文件夹\html_selenium_files\{site_name}_{month}_{asin}.html', 'w', encoding='utf-8')as f:
                    f.write(resp)
        else:
            print('没有该 asin html')

    def search_term_html(self, site_name='us', month='04'):
        sql = f"SELECT search_term,page,html FROM search_term_html_2025_{month} WHERE search_term='lace white tops for women' and site_name = '{site_name}'"
        print(sql)
        df = pd.read_sql(sql, con=engine_strrocks)
        print(df.values)
        if not df.empty:
            for index, row in df.iterrows():
                search_term = row['search_term']
                page = row['page']
                html = row['html']
                resp = self.decompress_string(html)
                with open(rf'D:\新建文件夹\html_selenium_files\{site_name}_{month}_{search_term}_{page}.html', 'w',
                          encoding='utf-8')as f:
                    f.write(resp)
        else:
            print('没有该 搜索词 html')

    def run(self):
        self.asin_html()
        # self.search_term_html(site_name='us',month='04')


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