# -*- coding: utf-8 -*-
"""股东信息"""

import ast
import json
import re
import time
import requests
from bs4 import BeautifulSoup
from kafka import KafkaProducer

import urllib3
from selenium.webdriver.support.wait import WebDriverWait

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

import sys
# sys.path.append('D:\\KK\\zzsn_spider\\base')
sys.path.append(r'D:\PycharmProjects\zzsn\base')
import BaseCore
baseCore = BaseCore.BaseCore()
cnx_ = baseCore.cnx
cursor_ = baseCore.cursor
log = baseCore.getLogger()

from classtool import Token, File, Tag
token = Token()
file = File()
tag = Tag()

from selenium import webdriver


def create_driver_():
    path = r'D:\soft\msedgedriver.exe'

    # options = webdriver.EdgeOptions()
    options = {
        "browserName": "MicrosoftEdge",
        "ms:edgeOptions": {
            "extensions": [], "args": ["--start-maximized"]  # 添加最大化窗口运作参数
        }
    }

    session = webdriver.Edge(executable_path=path, capabilities=options)
    return session

from selenium.webdriver.chrome.service import Service


def create_driver():
    chrome_driver = r'D:\cmd100\chromedriver.exe'
    path = Service(chrome_driver)
    chrome_options = webdriver.ChromeOptions()
    chrome_options.binary_location = r'D:\Google\Chrome\Application\chrome.exe'
    chrome_options.add_argument("--disable-javascript")
    # 设置代理
    # proxy = "127.0.0.1:8080"  # 代理地址和端口
    # chrome_options.add_argument('--proxy-server=http://' + proxy)
    driver = webdriver.Chrome(service=path, chrome_options=chrome_options)
    return driver

def login():
    driver = create_driver()
    url = 'https://www.qcc.com'
    driver.get(url)
    driver.maximize_window()
    # time.sleep(10)
    # cookie_list = driver.get_cookies()
    cookieinfo = token.getToken()
    if cookieinfo:
        pass
    else:
        log.info('==========已无cookies==========')
        time.sleep(30)
        return
    id_cookie = cookieinfo[0]
    cookie_list = cookieinfo[1]
    cookie_list = ast.literal_eval(cookie_list)
    # cookie_list = json.dumps(cookieinfo[1])
    print(cookie_list)
    # cookie_list= [{'domain': 'www.qcc.com', 'expiry': 1721815475, 'httpOnly': False, 'name': 'CNZZDATA1254842228', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': f'{cookie_["CNZZDATA1254842228"]}'}, {'domain': '.qcc.com', 'expiry': 1740650660, 'httpOnly': False, 'name': 'qcc_did', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'bb480035-2a34-4270-9a8b-db8b7d9374b3'}, {'domain': '.qcc.com', 'expiry': 1706695474, 'httpOnly': True, 'name': 'QCCSESSID', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'ccf17b97219476a1faa8aaff79'}, {'domain': '.qcc.com', 'expiry': 1721815461, 'httpOnly': False, 'name': 'UM_distinctid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '18d3aed87f3552-01ba17134bcbe9-4c657b58-e1000-18d3aed87f4c5d'}, {'domain': 'www.qcc.com', 'expiry': 1706092459, 'httpOnly': True, 'name': 'acw_tc', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '3d365a1c17060906591851865e848bfd116d30ed8d2ac3e144455c8ff8'}]
    for cookie in cookie_list:
        # cookie['expiry'] = int(cookie['expiry'])
        del cookie['expiry']
        driver.add_cookie(cookie)
    time.sleep(5)
    driver.refresh()
    url_test = 'https://www.qcc.com/firm/a5f5bb3776867b3e273cd034d6fb4baa.html'
    driver.get(url_test)
    # driver.get('https://www.qcc.com/')
    time.sleep(3)
    return driver, id_cookie, cookie_list[0]

import pymongo
# 连接数据库
db_storage = pymongo.MongoClient('mongodb://114.115.221.202:27017/', username='admin', password='ZZsn@9988').ZZSN[
        '股东信息']

# 从数据库存储
def send2db(dic_info):
    db_storage.insert_one(dic_info)
    pass



if __name__ == '__main__':
    taskType = '基本信息/企查查'
    # driver, id_cookie, cookie_ = login()
    # print(type(cookie_))
    while True:
        # nowtime = baseCore.getNowTime(1).replace('-', '')[:8]
        # file_name = f'./data/国内企业基本信息采集情况.xlsx'
        # print(file_name)
        # file.createFile(file_name)
        # start_time = time.time()
        # # 获取企业信息
        # # company_field = baseCore.redicPullData('BaseInfoEnterprise:gnqy_socialCode')
        # company_field = '91310000MAD51BU44D||上海交易集团有限公司|||||||||||||1|中国内地|||||||'
        # if company_field == 'end':
        #     # 本轮处理完毕，需要发送邮件，并且进入下一轮
        #     baseCore.sendEmail(file_name)
        #     time.sleep(20)
        #     file.deleteFile(file_name)
        #     continue
        #
        # if company_field == '' or company_field is None:
        #     # 本轮结束后没有新增的企业要采集
        #     file.deleteFile(file_name)
        #     flag = True
        #     while flag:
        #         log.info('--------已没有数据---------')
        #         time.sleep(30)
        #         if not baseCore.check_mysql_conn(cnx_):
        #             # 144数据库
        #             cnx_ = baseCore.cnx
        #             cursor_ = cnx_.cursor()
        #             log.info('===11数据库重新连接成功===')
        #         company_field = baseCore.redicPullData('BaseInfoEnterprise:gnqy_socialCode')
        #         if company_field:
        #             flag = False
        #             log.info("-----已添加数据------")
        #             baseCore.r.lpush('BaseInfoEnterprise:gnqy_socialCode',company_field)
        #             continue
        #     continue

        # 拼接链接
        #https://www.qcc.com/firm/3bc5dc687133ec9d6efdcd5e8e02ad4d.html
        url = 'https://www.qcc.com/firm/3bc5dc687133ec9d6efdcd5e8e02ad4d.html'
        # 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',
        #     'Accept-Language': 'zh-CN,zh;q=0.9',
        #     'Connection': 'keep-alive',
        #     # 'Cookie': 'qcc_did=046d99c9-566e-4046-9094-689901b79748; UM_distinctid=18aac5b8c21810-046f8431aecf58-26031f51-1fa400-18aac5b8c22efd; CNZZDATA1254842228=109635008-1695108795-https%253A%252F%252Fwww.qcc.com%252F%7C1695113473; _uab_collina=169935323766710839405007; acw_tc=db9062a717000200596487102e63dac7bed6aad2a049361c973816fabf; QCCSESSID=3c95642bd6445b7681c8fc6411',
        #     'Cookie': f'qcc_did={cookie_["qcc_did"]}; acw_tc={cookie_["acw_tc"]}; QCCSESSID={cookie_["QCCSESSID"]}',
        #     'Host': 'www.qcc.com',
        #     'Referer': 'https://www.qcc.com/',
        #     'Sec-Ch-Ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
        #     'Sec-Ch-Ua-Mobile': '?0',
        #     'Sec-Ch-Ua-Platform': '"Windows"',
        #     'Sec-Fetch-Dest': 'document',
        #     'Sec-Fetch-Mode': 'navigate',
        #     'Sec-Fetch-Site': 'same-origin',
        #     '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/119.0.0.0 Safari/537.36'
        # }


        for tag in script_tag:
            if 'window.__INITIAL_STATE__ =' in tag.text:
                true_text = tag.text
                print(true_text)
                break
            else:
                continue
        import re
        scrip = re.findall('__INITIAL_STATE__ =(.*);', true_text)[0].strip()
        reqJson = json.loads(scrip)
        print(reqJson)

        # try:
        #     gudong_tag = soup.find('div', class_="tablist tablist-ipopartner")
        # except:
        #     print("没有股东信息")
        #     gudong_tag = ''
        # if gudong_tag:
        #     table = gudong_tag.find('div', class_='app-tree-table')
        #     tr_tag = table.find_all('tr')[1]
        #     print(tr_tag)
        # tr_tag = BeautifulSoup("""""", '')    """
        #     <tr><td class="tx" rowspan="1">
        #     1
        #   </td> <td class="left first-td has-son-td" colspan="2"><div class="td-coy ipo-partner-app-tdcoy"><span class="headimg"><img class="img-logo app-auto-logo" src="https://image.qcc.com/logo/97da01cb2e03145138747afc9babf8a2.jpg?x-oss-process=style/logo_200" style="width: 40px; height: 40px;"/></span> <span class="cont"><span class="upside-line"><span class="name"><a href="https://www.qcc.com/firm/97da01cb2e03145138747afc9babf8a2.html" target="_blank">中国东方航空集团有限公司</a></span> <span class="tail-tag"><span class="icon-link ntag-v2 app-war-link"><i aria-label="图标: icon-icon_guquanjiegou" class="war-icon qccdicon qccdicon-icon-icon_guquanjiegou aicon aicon-guquanjiegou"><svg aria-hidden="true" class="" fill="currentColor" focusable="false" height="1em" width="1em"><use xlink:href="#icon-icon_guquanjiegou"></use></svg></i> <!-- --></span></span></span> <div class="app-tdcoy-tags app-tags margin-type-default"> <span class="ntag text-primary text-brand-blue"><span>央企</span></span><span class="ntag text-primary text-vip-gold"><span>大股东</span></span> <!-- --></div> <!-- --> </span> <!-- --> </div> <div class="show-son-btn"><i aria-label="图标: icon-icon_jiahao" class="tianjia qccdicon qccdicon-icon-icon_jiahao aicon aicon-jiahao"><svg aria-hidden="true" class="" fill="currentColor" focusable="false" height="1em" width="1em"><use xlink:href="#icon-icon_jiahao"></use></svg></i></div></td><td class="center"><span>流通A股,限售流通A股</span> <!-- --></td><td class="right"><span class="no-wrap"><span>39.57%</span><!-- --></span> <!-- --></td><td class=""><div><span class="app-data-mask"><span class="vip-mask"></span></span></div> <!-- --></td><td class=""><span>41.9674%</span><span class="icon-link ntag-v2 app-war-link"><i aria-label="图标: icon-icon_guquanlian" class="war-icon qccdicon qccdicon-icon-icon_guquanlian aicon aicon-guquanlian"><svg aria-hidden="true" class="" fill="currentColor" focusable="false" height="1em" width="1em"><use xlink:href="#icon-icon_guquanlian"></use></svg></i> <!-- --></span> <!-- --></td><td class="left"><span>-</span> <!-- --></td></tr>
        #
        #     """
        #     td_list = tr_tag.find_all('td')
        #     q = td_list[0].text()
        #     if q == '1':
        #         pass
        #     else:
        #         print('error')
        #
        #     gudong_name = td_list[1].find('span', class_='name').text
        #     gudong_pro = td_list[1].find_all('span', class_='ntag')



