import pymysql
import datetime
from params import DB_CONN_DICT, PG_CONN_DICT_14
from sqlalchemy import create_engine
import pandas as pd
from lxml import etree
import json
import os
import socket
import requests
import random
import time

"""删除 10天cookie"""


def send_mg(site, report_date, type):
    url = 'http://47.112.96.71:8082/selection/sendMessage'
    data = {
        'account': 'pengyanbing',
        'title': site + '站点 搜索词',
        'content': report_date + ' 搜索词 ' + type + '页面解析错误。请检查'
    }
    requests.post(url=url, data=data, timeout=15)


def init_db(site_name):
    if site_name == 'us':
        connect = 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:
        connect = 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_name,
                                  charset="utf8mb4")
    cursor = connect.cursor()
    # if site_name == 'us':
    #     db = 'selection'
    # else:
    #     db = f'selection_{site_name}'
    # DB_CONN_DICT = {
    #     "mysql_port": 3306,
    #     "mysql_db": "selection",
    #     "mysql_user": "XP_Yswg2025_PY",
    #     "mysql_pwd": "Xp_Yswg2025_Py300",
    #     "mysql_host": "rm-wz9yg9bsb2zf01ea4yo.mysql.rds.aliyuncs.com",
    # }

    if site_name == 'us':
        engine_pg = create_engine(
            f"postgresql+psycopg2://{PG_CONN_DICT_14['pg_user']}:{PG_CONN_DICT_14['pg_pwd']}@{PG_CONN_DICT_14['pg_host']}:{PG_CONN_DICT_14['pg_port']}/selection",
            encoding='utf-8')
    else:
        engine_pg = create_engine(
            f"postgresql+psycopg2://{PG_CONN_DICT_14['pg_user']}:{PG_CONN_DICT_14['pg_pwd']}@{PG_CONN_DICT_14['pg_host']}:{PG_CONN_DICT_14['pg_port']}/selection_{site_name}",
            encoding='utf-8')
    #
    # sql_read = f"SELECT asin, img_url, title, title_len, price, rating, total_comments, buy_box_seller_type, page_inventory, category, volume, weight, rank, launch_time, video_url, add_url, material, created_at, updated_at, img_num, img_type, qa_num, brand, ac_name, node_id, sp_num, mpn, online_time, `describe`, one_star, two_star, three_star, four_star, five_star, low_star,    asin_type, is_coupon, search_category, weight_str, date_info, site,    account_name, other_seller_name, bsr_date_info, account_id FROM us_self_asin_detail where date_info = '2023-05-24'"
    #
    # cursor.execute(sql_read)
    # data_search_term = cursor.fetchall()
    # # print(data_search_term)
    # df = pd.DataFrame(data=data_search_term,
    #                   columns=['asin', 'img_url', 'title', 'title_len', 'price', 'rating', 'total_comments',
    #                            'buy_box_seller_type', 'page_inventory', 'category', 'volume', 'weight', 'rank',
    #                            'launch_time', 'video_url', 'add_url', 'material', 'created_at', 'updated_at', 'img_num',
    #                            'img_type', 'qa_num', 'brand', 'ac_name', 'node_id', 'sp_num', 'mpn', 'online_time',
    #                            'describe', 'one_star', 'two_star', 'three_star', 'four_star', 'five_star', 'low_star',
    #                            'asin_type', 'is_coupon', 'search_category', 'weight_str', 'date_info', 'site',
    #                            'account_name', 'other_seller_name', 'bsr_date_info', 'account_id'])
    # df.loc[df['launch_time'] == '0000-00-00', 'launch_time'] = None
    # df.to_sql('us_self_asin_detail_2023', con=engine_pg, if_exists="append", index=False)

    '上传日搜索词测试'
    # days = ((datetime.datetime.now()) + datetime.timedelta(days=-5)).strftime("%d")
    # print(days)
    # temp_date = datetime.datetime.now()  # 获取当前时间 年月日时分秒
    # date2 = (temp_date + datetime.timedelta(days=-5)).strftime("%Y-%m-%d")  # 获取当前日期的前两天日期
    # day = int(days)
    # print(date2, '2222222')
    # _yera_time = time.strftime('%Y', time.localtime(time.time()))
    # report_date = date2
    # print("report_date: ", report_date)
    # df = pd.read_csv(rf"C:\Users\ASUS\Desktop\amazon_spider\US_Top_Search_Terms_Simple_Day_2023_04_01.csv", skiprows=1, encoding='UTF-8-SIG', on_bad_lines='skip', encoding_errors='ignore')
    # print(df.shape)
    # try:
    #     df.drop(columns=['Top Clicked Product #1: Product Title', 'Top Clicked Product #2: Product Title', 'Top Clicked Product #3: Product Title','Reporting Date'], inplace=True)
    # except:
    #     df.drop(columns=['Top Clicked Product #1: Product Title', 'Top Clicked Product #2: Product Title', 'Top Clicked Product #3: Product Title','Reporting Date'],
    #             inplace=True)
    # print(df.columns)
    # df.rename(columns={
    #     'Search Term': 'search_term',
    #     'Search Frequency Rank': 'rank',
    #     'Top Clicked Brand #1': 'top_clicked_brand_1',
    #     'Top Clicked Brands #2': 'top_clicked_brand_2',
    #     'Top Clicked Brands #3': 'top_clicked_brand_3',
    #     'Top Clicked Category #1': 'top_clicked_category_1',
    #     'Top Clicked Category #2': 'top_clicked_category_2',
    #     'Top Clicked Category #3': 'top_clicked_category_3',
    #     'Top Clicked Product #1: ASIN': 'asin1',
    #     'Top Clicked Product #1: Click Share': 'click_share1',
    #     'Top Clicked Product #1: Conversion Share': 'conversion_share1',
    #     'Top Clicked Product #2: ASIN': 'asin2',
    #     'Top Clicked Product #2: Click Share': 'click_share2',
    #     'Top Clicked Product #2: Conversion Share': 'conversion_share2',
    #     'Top Clicked Product #3: ASIN': 'asin3',
    #     'Top Clicked Product #3: Click Share': 'click_share3',
    #     'Top Clicked Product #3: Conversion Share': 'conversion_share3',
    # }, inplace=True)
    #
    # df['rank'] = df['rank'].apply(lambda x: str(x).replace(",", ""))
    # df.search_term = df.search_term.apply(lambda x: str(x)[:150])  # 截取前150字符
    # df = df.loc[df.search_term.str.len() > 1]  # 字符大于1
    # df.click_share1 = df.click_share1.apply(
    #     lambda x: float(x) / 100)
    # df.conversion_share1 = df.conversion_share1.apply(
    #     lambda x: float(x) / 100)
    # df.click_share2 = df.click_share2.apply(
    #     lambda x: float(x) / 100)
    # df.conversion_share2 = df.conversion_share2.apply(
    #     lambda x: float(x) / 100)
    # df.click_share3 = df.click_share3.apply(
    #     lambda x: float(x) / 100)
    # df.conversion_share3 = df.conversion_share3.apply(
    #     lambda x: float(x) / 100)
    # df['report_date'] = report_date.replace('-', '')
    # df['date_info'] = report_date
    # y, m, d = report_date.split("-")
    # print(y, m, d)
    #
    # df.to_sql(f"{site_name}_brand_analytics_everyday_{y}", con=engine_pg, if_exists="append",
    #           index=False)

    '下载  音乐 '
    # scraper_url = 'https://artlist.io/api/Song/List'
    # headers = {
    #     'accept': 'application/json, text/javascript, */*; q=0.01',
    #     'accept-encoding': 'gzip, deflate, br',
    #     'accept-language': 'en-US',
    #     'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5MzMwNTc3Yy0zNWQyLTQ3NTktYjdhNy00NTgxODIxYTAyNDciLCJzdWIiOiIyNTk2NDMxIiwiZW1haWwiOiJ6b3V3ZWlAeXN3Zy5jb20uY24iLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjI1OTY0MzEiLCJ1bmlxdWVfbmFtZSI6InpvdXdlaUB5c3dnLmNvbS5jbiIsImdpdmVuX25hbWUiOiJ5c3dnMjAyMSAiLCJpc0V4dGVybmFsQmlsbGVkIjoiVHJ1ZSIsInN1YnNjcmlwdGlvbklkIjoiMzY1MDQxMiIsInRlbmFudElkIjoiYXJ0bGlzdCIsImV4cCI6MTY4MDc0MjkxNX0.KqSjuVxA0YUGVr33pRNorE0d9yJLAEBjWHKBkGUA1hI',
    #     'cache-control': 'no-cache',
    #     'content-length': '204',
    #     'content-type': 'application/json',
    #     'cookie': 'optimizelyEndUserId=oeu1679536870850r0.7071089853062891; userVisitorNew=c6ba6bbf-0695-4ee2-9103-99cca7c0513d; userSession=6e802db0-2b6f-4547-825b-ef3e538f4e0f; _gid=GA1.2.1803988674.1679536872; _gcl_au=1.1.244746656.1679536872; x-ms-routing-name=self; _rdt_uuid=1679536872203.1d0b064a-18b3-46cb-b171-d5686bc3e0aa; _hjFirstSeen=1; _hjSession_458951=eyJpZCI6ImE4MzQwZmE3LTlkMzYtNGNmMC04YzdhLWY3ZmZmNGQxNjllZiIsImNyZWF0ZWQiOjE2Nzk1MzY4NzIzODgsImluU2FtcGxlIjp0cnVlfQ==; _hjAbsoluteSessionInProgress=0; ln_or=eyI0MTkwMjQ5IjoiZCJ9; _tt_enable_cookie=1; _ttp=QHJvXju2YvJudz1ZCKxM8FgSx2T; TiPMix=82.15806888204298; AntiforgeryCookie=CfDJ8CpxpXmrGSJOvO4Uovren5glom4KYSIyVww8aRLUNlx2XhQnfW0YImGjCwLcWT3-X0phlXgMV_W-17IeiQaIUUTcpRt0UBnBAXzZEyMMAqsvmGmaGM5B4v8TPaU3kBcN7C8gZ3Ok9YuDvBKXb97i39o; _pin_unauth=dWlkPVpXRmlPV0psTnpjdE9XWTNNaTAwTmpsa0xUazBNV1F0T0RjelpUYzJZVEl3WWpObA; _clck=kabt7t|1|fa5|0; _fbp=fb.1.1679536873608.1646885667; ajs_anonymous_id=7578a89b-7b45-431d-9e75-7bebda6da804; PAPVisitorId=MSKl3AqwbgqmjSfC6CMgwrL3GsCxfmu2; accessToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5MzMwNTc3Yy0zNWQyLTQ3NTktYjdhNy00NTgxODIxYTAyNDciLCJzdWIiOiIyNTk2NDMxIiwiZW1haWwiOiJ6b3V3ZWlAeXN3Zy5jb20uY24iLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjI1OTY0MzEiLCJ1bmlxdWVfbmFtZSI6InpvdXdlaUB5c3dnLmNvbS5jbiIsImdpdmVuX25hbWUiOiJ5c3dnMjAyMSAiLCJpc0V4dGVybmFsQmlsbGVkIjoiVHJ1ZSIsInN1YnNjcmlwdGlvbklkIjoiMzY1MDQxMiIsInRlbmFudElkIjoiYXJ0bGlzdCIsImV4cCI6MTY4MDc0MjkxNX0.KqSjuVxA0YUGVr33pRNorE0d9yJLAEBjWHKBkGUA1hI; _hjSessionUser_458951=eyJpZCI6ImY5YmVjOTQ3LTBlNDktNWNlMi1hNGI1LWUzMmI5NzI5YTMyMyIsImNyZWF0ZWQiOjE2Nzk1MzY4NzIzODIsImV4aXN0aW5nIjp0cnVlfQ==; _uetsid=95f97ab0c91e11edb428876e0544e20e; _uetvid=95f9bd40c91e11ed972a0f49c6b1bcfe; _ga=GA1.2.1417359823.1679536872; _hjHasCachedUserAttributes=true; ajs_user_id=2596431; ab.storage.deviceId.a8859136-009c-49a9-9457-9554c055e10d=%7B%22g%22%3A%22393578a1-b15d-0701-34c0-e379b26c246f%22%2C%22c%22%3A1679536874640%2C%22l%22%3A1679537488302%7D; ab.storage.userId.a8859136-009c-49a9-9457-9554c055e10d=%7B%22g%22%3A%222596431%22%2C%22c%22%3A1679537488300%2C%22l%22%3A1679537488302%7D; _hjDonePolls=870089; _clsk=12o55qj|1679538654837|9|1|j.clarity.ms/collect; _ga_65CXCH03KJ=GS1.1.1679536871.1.1.1679538655.60.0.0; __cf_bm=N0uk7HKoRw7.Yl20zvtKhsmiFg.zdvqiUEyPWxqFYiA-1679538653-0-AWRCasJ/UULh1Y2l+KyYnyiM7NHOsHEjWJjQVSOK+tn+zpZuX1Up3+ZY24+qG80V7rzDrfKAaEPGYg4uRH9Moxk=; al-artlist=CfDJ8CpxpXmrGSJOvO4Uovren5h2HxTWKTLxo7pe0W0TpW4BA26iJ9LWrAcqMvsoMxYF3Qr4g4dQkKDmwTZ9He2OXeZdu7AgPMLG-VeaKWblPywDfySGBAaibbBrNk-Saaq35MhgCUcEDnRpJNGCjBtKQ_Gn84GwN5r-9Kj8KIZR6or-Qiu-M1-QEdCClb2g4vszUNAeW3qNbPAD1rRUmLZXe8kzBWR8jWRPv0sO28b4x5exeBpH06bS8MVEY04pszLdSsREbNxzXK2qYV2B99abOYbYgXTK4TjXPZghZlXCwcmnROOYCufBbgfT6ERShqTuW7R1ur589Rsf8yveczDGJXUX3oIL0hB7KB2VDKVQC8A_PZJK0Z8CbBbsS1HeGnxpffHJSAYK8Bd7c8GpmY-HukokRiaFBl6VosJYXXPRggOTmaNgLF8954nW2dv-U1h1HzvYuWLrjhoPO9t-FlxvnugUgEYVm3MMQ9CiEQXf5kjp0aYKDRuQgxk-YcMZJMJ0QNPZnahnGvkfq5ZBnjMwmOaEMd_bRVlw0vnQwLZ3IU57N8blWmJ136dHph2_QLYerA; _gat=1; ab.storage.sessionId.a8859136-009c-49a9-9457-9554c055e10d=%7B%22g%22%3A%22bba6a8c9-c6f9-a513-22ef-8cbf0900a5da%22%2C%22e%22%3A1679541023985%2C%22c%22%3A1679537488301%2C%22l%22%3A1679539223985%7D; XSRF-TOKEN=CfDJ8CpxpXmrGSJOvO4Uovren5jFWko4Xo2CZ9fhETQd-duDCd9qoi7VhHOTyD1zW2_GBC8W1z7_TZ2CIhyn51Zkt8uEFrqjiDnCTto_ZqsHgRgsGkRGcMx1InB6MMevmpRWvToPdPBgnIrl8gs-yKzbj7ud-i6ikrPTvRq8tS8VGri6P4QGN_EdQbnTtbTY8qjNDw',
    #     'origin': 'https://artlist.io',
    #     'pragma': 'no-cache',
    #     'referer': 'https://artlist.io/category/5&311/uplifting&epic/',
    #     'sec-ch-ua': '"Google Chrome";v="110", "Not(A:Brand";v="8", "Chromium";v="111"',
    #     'sec-ch-ua-mobile': '?0',
    #     'sec-ch-ua-platform': '"Windows"',
    #     '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/110.0.0.0 Safari/537.36',
    #     'x-requested-with': 'XMLHttpRequest',
    #     'x-xsrf-token': 'CfDJ8CpxpXmrGSJOvO4Uovren5jFWko4Xo2CZ9fhETQd-duDCd9qoi7VhHOTyD1zW2_GBC8W1z7_TZ2CIhyn51Zkt8uEFrqjiDnCTto_ZqsHgRgsGkRGcMx1InB6MMevmpRWvToPdPBgnIrl8gs-yKzbj7ud-i6ikrPTvRq8tS8VGri6P4QGN_EdQbnTtbTY8qjNDw',
    # }
    # import requests
    # import time
    # id_list = []
    # categoryid_lsit = ['50', '33', '52', '9', '49', '48', '84', '85', '68', '83', '96', '42', '14', '101', '5', '36', '24', '18', '32', '19', '98', '8', '22', '10', '105', '17', '311', '21', '31', '57', '25', '34', '78', '322', '320', '65', '28', '39', '13', '37', '40', '46', '41', '26', '6', '20', '7', '38', '16', '35', '72', '92', '12', '62', '15', '51', '93', '23', '79', '69']
    # for i in categoryid_lsit:
    #     data = {"searchTerm": '', "categoryIDs": f"{i}", "excludedIDs": "", "songSortID": "1", "page": "1",
    #             "durationMin": "0", "durationMax": "0", "onlyVocal": '', "bpmMin": "0", "bpmMax": "0",
    #             "geoClusterToCollectionIdMapping": '', "vocalId": "0"}
    #     resp = requests.post(scraper_url, headers=headers, timeout=30, verify=False, json=data)
    #     json_dict = json.loads(resp.text)
    #     for i in json_dict['songs']:
    #         print()
    #         print(i['songName'])
    #         print(i['sitePlayableFilePath'])
    #         print(i['albumName'])
    #         songName = i['songName']
    #         sitePlayableFilePath = i['sitePlayableFilePath']
    #         albumName = i['albumName']
    #         sql = f'INSERT into artlist_music(songName,albumName,music_url)values("{songName}","{albumName}","{sitePlayableFilePath}")'
    #         print(sql)
    #         cursor.execute(sql)
    #         connect.commit()
    #         print(i['collections']['staffPicks'][0]['categoryId'])
    #         id_list.append(i['collections']['staffPicks'][0]['categoryId'])
    #     time.sleep(10)
    # print(id_list)

    ' asin存在不更新。'
    # self_asin_list = [['B0018Z8D64']]
    # cursor.executemany(
    #     f"insert into {site_name}_self_asin_pyb (asin) values (%s) ON DUPLICATE KEY UPDATE asin = values(asin)",
    #     self_asin_list)
    # connect.commit()

    '  查询 nysql  中  表的数据大小========================'
    # # # sql = f'SELECT count(*) FROM {site_name}_search_term WHERE state=1'
    # sql = f"SELECT week,count(*) FROM {site_name}_all_syn_st where  state=1"
    # # sql = f"SELECT `week`,count(1) FROM {site_name}_all_syn_st GROUP BY `week`"
    # # # sql = f"-- UPDATE {site_name}_all_syn_st set state=5 WHERE state=1"
    # # # # # # # # # # # # # # # # # # # # # # # # # sql = f"-- SELECT `week`,count(1) FROM {site_name}_all_syn_st_history_2022 GROUP BY `week`"
    # # # # # # # # # # # # # # # # # sql = f" SELECT `week`,count(1) FROM {site_name}_search_term where  state=1 GROUP BY `week`"
    # print(sql)
    # cursor.execute(sql)
    # connect.commit()
    # count_list = cursor.fetchall()
    # print(site_name,count_list)
    '===============删除重复数据。重新清空表 插入。================================'
    # # 读取状态不是1 的asin
    # # sql = f"SELECT count(DISTINCT(asin)) FROM us_st_everyday_syn_2023_02_13"
    # sql = f"SELECT asin, state,asin_is_variation FROM uk_st_everyday_syn_2023_02_12 where state !=1;"
    # df_12 = pd.read_sql(sql, con=engine_pg)
    # # df_12.drop_duplicates('asin', inplace=True)  # 根据asin去重
    # print(df_12.shape)
    # # 读取状态 1 的asin
    # sql = f"SELECT asin, state,asin_is_variation FROM uk_st_everyday_syn_2023_02_12 where  state =1;"
    # df_st_1 = pd.read_sql(sql, con=engine_pg)
    # print(df_st_1.shape)
    # print(333333333)
    # df_st_1.drop_duplicates('asin', inplace=True)  # 根据asin去重
    # print(df_st_1.shape)
    # # 剔除状态 7，9 的id
    # print(2222222222)
    # # 找出 df_st_1 的asin 不再 df_12 里面 然后进行合并，。asin 状态不变
    # df = df_st_1.loc[~(df_st_1.asin.isin(df_12.asin))]
    # print(df.shape)
    # df_save = pd.concat([df_12, df])
    # print(212)
    # print(df_save.shape)
    # print(df_save.values)
    # # with engine_pg.begin() as conn:
    # #     sql = 'truncate uk_st_everyday_syn_2023_02_12;'
    # #     conn.execute(sql)
    # # df_save['date_info'] = '2023-02-12'
    # # print('cun')
    # # df_save.to_sql("uk_st_everyday_syn_2023_02_12", con=engine_pg, if_exists='append', index=False)
    ' ==================   创建年表分区 。查询日期 ======================='
    # sql = 'select date from date_20_to_30 where year=2023'
    # cursor.execute(sql)
    # count_list = cursor.fetchall()
    # v = 0
    # # print(count_list[1])
    # date_list = []
    # for i in count_list:
    #     date_list.append(i[0])
    # i = 0
    # for data in date_list:
    #     i+=1
    # #     # 按年
    #     sql1 = f"create table us_self_product_detail_2023_{str(data).replace('-','_')} partition of us_self_product_detail_2023 for values from ('{str(data)}') to ('{str(date_list[i])}');"
    #     print(sql1)
    # #
    #     # 'create table us_aba_profit_gross_day_2023_05_01 partition of us_aba_profit_gross_day_2023_05 for values from ('2023-05-01') to ('2023-05-02');'
    #     # moth = str(data).split('-')[1] 按 月分表
    #     # print(moth)
    #     sql1 = f"-- create table us_aba_profit_gross_day_{str(data).replace('-','_')} partition of us_aba_profit_gross_day_2023_{moth} for values from ('{str(data)}') to ('{str(date_list[i])}');"
    #     print(sql1)
    # #
    '  =====================删除 各个站点 的搜索词数据==================='
    # for i in range(40, 45):
    #     for type in ['sp', 'sb', 'bs', 'tr', 'er']:
    #         sql = f'TRUNCATE {site_name}_search_term_rank_{type}_2022_{i}'
    #         print(sql)
    #         cursor.execute(sql)
    #         connect.commit()
    #     zr_sql = f'delete  from {site_name}_search_term_rank_zr_2022_{i}  where page  >= 2 ;'
    #     print(zr_sql)
    #     cursor.execute(zr_sql)
    #     connect.commit()

    ' 删除cookie   查询'
    select_all_cookie = f"SELECT * FROM {site_name}_cookies"
    cursor.execute(select_all_cookie)
    cookie_all_lsit = cursor.fetchall()
    print(f"{site_name} all cookie 总数量：", len(cookie_all_lsit))

    day = ((datetime.datetime.now()) + datetime.timedelta(days=-6)).strftime("%Y-%m-%d")
    select_sql = f"SELECT * FROM {site_name}_cookies WHERE updated_time < '{day}'"
    print(select_sql)
    cursor.execute(select_sql)
    cookie_lsit = cursor.fetchall()
    print("删除数量 10 天前的 cookie ：",len(cookie_lsit))
    delect_sql = f"DELETE FROM {site_name}_cookies WHERE updated_time < '{day}'"
    print(delect_sql)
    cursor.execute(delect_sql)
    connect.commit()
    if site_name in ('us','uk','de'):
        print(site_name)
        with engine_pg.begin() as conn:
            conn.execute(delect_sql)
    '  搜索词监控，  每日早上九点多 查询当日抓取量 ==========================='

    # if site_name == 'us':
    #     sql_read = f"SELECT DISTINCT(report_date) FROM {site}_st_everyday where state in (1,2) GROUP BY report_date"
    #     print(sql_read)
    #     df = pd.read_sql(sql_read, con=engine_pg)
    #     print(list(df.report_date))
    #     if list(df.report_date):
    #         report_date = list(df.report_date)[0]
    #         print("report_date: ", report_date)
    #         ymd = report_date.replace('-', '_')
    #         for i in ['zr', 'sp', 'sb', 'ac', 'er', 'tr', 'bs']:
    #             sql_count = f"select count(id) from {site}_st_{i}_everyday_{ymd} limit 1"
    #             print(sql_count)
    #             df_count = pd.read_sql(sql_count, con=engine_pg)
    #             count = list(df_count.values)[0][0]
    #             print(count)
    #             if i == 'er':
    #                 if count < 10000:
    #                     print('数据解析错误')
    #                     send_mg(site, ymd, i)
    #             elif i == 'ac' or i == 'tr':
    #                 if count < 90000:
    #                     print('数据解析错误')
    #                     send_mg(site, ymd, i)
    #             else:
    #                 if count < 200000:
    #                     print('数据解析错误')
    #                     send_mg(site, ymd, i)
    # cursor.close()
    # connect.close()


if __name__ == '__main__':
    site_list = ['us','de','uk','fr','es','it']
    # site_list = ['us']
    for site in site_list:
        spider_us = init_db(site_name=site)
