"""
    企业上市信息：只有上市的企业才能如企业库，未上市企业跳过采集步骤。退市企业标注为0
"""
import json
import time
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import urllib3
from BaseCore import BaseCore
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# from gpdm import Gpdm
baseCore = BaseCore()
# chromedriver = r"E:\kkwork\zzsn_spider\comData\ipoInfo\chromedriver.exe"
# browser = webdriver.Chrome(chromedriver)
taskType = '上市信息/东方财富网/新三板'
# gpdm = Gpdm()
# gpdmList = gpdm.doJob()
log = baseCore.getLogger()
error_list = []
list_all_info = []
# 需要提供股票代码、企业信用代码
flag = 0
while True:
    #从表中读取企业
    com_code = baseCore.redicPullData('NQEnterprise:nq_Ipo')
    # com_code = baseCore.redicPullData('NQEnterprise:nq_Ipo_test')
    if com_code is None:
        if flag==0:
            time.sleep(20)
            log.info('已没有数据----------等待')
            continue
        elif flag==1:
            # 通过接口将数据保存进数据库
            for num in range(0, len(list_all_info), 100):

                json_updata = json.dumps(list_all_info[num:num + 100])
                # print(json_updata)
                try:
                    response = requests.post('http://114.115.236.206:8088/sync/enterpriseIpo', data=json_updata,
                                             timeout=300,
                                             verify=False)
                except Exception as e:
                    print(e)
                print("{}：到：{}".format(num, num + 100))
                print(response.text)
            log.info('-----------数据发送接口完毕----------')
            flag = 0
            continue

    #从数据库中查询到其他信息
    log.info(f'========正在采集{com_code}===========')
    data = baseCore.getInfomation(com_code)
    social_code = data[1]
    short_name = data[3]
    start = time.time()
    log.info(f'======开始采集{com_code}======')
    url = f'https://xinsanban.eastmoney.com/F10/CompanyInfo/Introduction/{com_code}.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',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Cookie': 'qgqp_b_id=28edcf226f056ee077983f40f115eacf; st_si=15067486119520; emshistory=%5B%22%E4%BA%A7%E4%B8%9A%E9%93%BE%22%2C%22sz007sz%22%5D; websitepoptg_show_time=1694403032729; HAList=ty-0-002342-%u5DE8%u529B%u7D22%u5177%2Cty-0-301192-%u6CF0%u7965%u80A1%u4EFD%2Cty-1-688382-%u76CA%u65B9%u751F%u7269-U%2Cty-1-600895-%u5F20%u6C5F%u9AD8%u79D1%2Cty-1-600669-*ST%u978D%u6210%2Cty-116-00691-%u5C71%u6C34%u6C34%u6CE5%2Cty-0-300865-%u5927%u5B8F%u7ACB%2Cty-0-000656-%u91D1%u79D1%u80A1%u4EFD%2Cty-1-600257-%u5927%u6E56%u80A1%u4EFD%2Cty-1-688981-%u4E2D%u82AF%u56FD%u9645; xsb_history=833658%7C%u94C1%u8840%u79D1%u6280%2C838616%7C%u5317%u9CD0%u98DF%u54C1; st_asi=delete; st_pvi=44810095342512; st_sp=2023-07-18%2013%3A55%3A09; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=337; st_psi=20230914142347564-119112305908-4534169252',
        'Host': 'xinsanban.eastmoney.com',
        'Pragma': 'no-cache',
        'Referer': 'https://xinsanban.eastmoney.com/F10/CompanyInfo/Introduction/833658.html',
        '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/115.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"'
    }
    req = requests.get(url=url,headers=headers)
    reslut = BeautifulSoup(req.content,'html.parser')
    # print(reslut)
    li_list = reslut.find('div',id='company_info').find('ul',class_='company-page-left').find_all('li')
    security = reslut.find('div',id='security_info').find('ul',class_='company-page-right').find_all('li')
    listingDate = security[1].find('span',class_='company-page-item-right').text
    businessScope = li_list[7].find('span',class_='company-page-item-right').text
    industry = li_list[8].find('span',class_='company-page-item-right').text
    secutities_type = '新三板'
    category = '3'
    exchange = '1'
    dic_cwsj = {
        "exchange": exchange,
        "category": category,  # 股票类型(1-A股;2-B股;3-新三板;4-H股)
        'listed': '1',
        "listingDate": listingDate,
        "securitiesCode": com_code,
        "securitiesShortName": short_name,
        "securitiesType": secutities_type,
        "socialCreditCode": social_code,
        "businessScope": businessScope,
        "eastIndustry": industry,
        "csrcIndustry": ''
    }
    # print(dic_cwsj)
    list_all_info.append(dic_cwsj)
    log.info(f'======{com_code}====采集成功=====')
    flag = 1


