import requests, json, time, pymysql, sys
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver

cnx = pymysql.connect(host='114.116.44.11', user='root', password='f7s0&7qqtK', db='clb_project', charset='utf8mb4')

list_com_code = []

with cnx.cursor() as cursor:
    # selece_sql = '''SELECT securities_code,exchange FROM sys_base_enterprise_ipo where category = '1' '''
    selece_sql = '''select i.securities_code,i.exchange from sys_base_enterprise_ipo i inner join sys_base_label_type_map m on i.social_credit_code = m.relation_id where m.label_id = '1624235171922227202' and i.priority = 1 '''

    cursor.execute(selece_sql)
    selects = cursor.fetchall()

    for row in selects:
        com_code = row[0]
        com_type = row[1]
        if com_type == 1:
            com_code = 'BJ' + com_code
        elif com_type == 2:
            com_code = 'SH' + com_code
        elif com_type == 3:
            com_code = 'SZ' + com_code
        else:
            print(111)
        list_com_code.append(com_code)

headers = {
    'cookie': 'device_id=72922798c76cce710281dd6a2404a7a7; s=d11bic2en8; xq_a_token=7da3658c0a79fd9ef135510bc5189429ce0e3035; xqat=7da3658c0a79fd9ef135510bc5189429ce0e3035; xq_r_token=c4e290f788b8c24ec35bd4b893dc8fa427e1f229; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY3OTk2MjkxMiwiY3RtIjoxNjc4NDk2MzMwMzY5LCJjaWQiOiJkOWQwbjRBWnVwIn0.eteAa4NvugN2Ck4dO4r5xnNKTW-HbFKYrT86auaexzd4ESpeWFAdxrYMjhC4nQevwGuFDb0hZWb2Xsk8DKIpw5O42CjvotTBCv8ij4hHTZ1HoR1tNMwlcb3iibklvdSaeYeF-4Mcp_sOY4BSzb3dcEDxUlwvPSpvU94aX5FGgubkVnXbbhrTQ05NxqumlqqULuqrc3U8tMCrkufef3sVNj4L_8TwbZBuYo58OEYF5zTWEyWVkvRswo9snE7Q242qghidhYpG34G04SSlmG6chyShNGLnV1hA5k3TI6ELLGPArEey7mxtpBcRpEp3jhou0tJWUqjHeyvqMvGxGbPwng; u=301678496364087; Hm_lvt_1db88642e346389874251b5a1eded6e3=1676337877,1677743855,1678191984,1678496365; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1678497585',
    # 'referer': 'https://xueqiu.com/snowman/S/SZ002931/detail',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
}

list_all_info = []

for com_code in list_com_code[0:]:
    print(com_code)

    url_xueqiu = f'https://stock.xueqiu.com/v5/stock/f10/cn/company.json?symbol={com_code}'

    json_xueqiu = requests.get(url_xueqiu, headers=headers).json()
    dic_com = json_xueqiu['data']['company']

    com_type = dic_com['classi_name']
    # com_money = dic_com['issue_price']*dic_com['actual_issue_vol']

    url_dongfang = f'https://emweb.eastmoney.com/PC_HSF10/CompanySurvey/PageAjax?code={com_code}'
    json_dongfang = requests.get(url_dongfang).json()

    com_money = json_dongfang['fxxg'][0]['TOTAL_FUNDS']

    com_c = com_code[2:]
    t = int(time.time() * 1000)
    if 'SZ' in com_code or 'BJ' in com_code:
        url_shizhi = f'https://push2.eastmoney.com/api/qt/stock/get?invt=2&fltt=1&fields=f58%2Cf734%2Cf107%2Cf57%2Cf43%2Cf59%2Cf169%2Cf170%2Cf152%2Cf177%2Cf111%2Cf46%2Cf60%2Cf44%2Cf45%2Cf47%2Cf260%2Cf48%2Cf261%2Cf279%2Cf277%2Cf278%2Cf288%2Cf19%2Cf17%2Cf531%2Cf15%2Cf13%2Cf11%2Cf20%2Cf18%2Cf16%2Cf14%2Cf12%2Cf39%2Cf37%2Cf35%2Cf33%2Cf31%2Cf40%2Cf38%2Cf36%2Cf34%2Cf32%2Cf211%2Cf212%2Cf213%2Cf214%2Cf215%2Cf210%2Cf209%2Cf208%2Cf207%2Cf206%2Cf161%2Cf49%2Cf171%2Cf50%2Cf86%2Cf84%2Cf85%2Cf168%2Cf108%2Cf116%2Cf167%2Cf164%2Cf162%2Cf163%2Cf92%2Cf71%2Cf117%2Cf292%2Cf51%2Cf52%2Cf191%2Cf192%2Cf262%2Cf294%2Cf295%2Cf269%2Cf270%2Cf256%2Cf257%2Cf285%2Cf286&secid=0.{com_c}&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}'
    if 'SH' in com_code:
        url_shizhi = f'https://push2.eastmoney.com/api/qt/stock/get?invt=2&fltt=1&fields=f58%2Cf734%2Cf107%2Cf57%2Cf43%2Cf59%2Cf169%2Cf170%2Cf152%2Cf177%2Cf111%2Cf46%2Cf60%2Cf44%2Cf45%2Cf47%2Cf260%2Cf48%2Cf261%2Cf279%2Cf277%2Cf278%2Cf288%2Cf19%2Cf17%2Cf531%2Cf15%2Cf13%2Cf11%2Cf20%2Cf18%2Cf16%2Cf14%2Cf12%2Cf39%2Cf37%2Cf35%2Cf33%2Cf31%2Cf40%2Cf38%2Cf36%2Cf34%2Cf32%2Cf211%2Cf212%2Cf213%2Cf214%2Cf215%2Cf210%2Cf209%2Cf208%2Cf207%2Cf206%2Cf161%2Cf49%2Cf171%2Cf50%2Cf86%2Cf84%2Cf85%2Cf168%2Cf108%2Cf116%2Cf167%2Cf164%2Cf162%2Cf163%2Cf92%2Cf71%2Cf117%2Cf292%2Cf51%2Cf52%2Cf191%2Cf192%2Cf262%2Cf294%2Cf295%2Cf269%2Cf270%2Cf256%2Cf257%2Cf285%2Cf286&secid=1.{com_c}&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}'

    json_shizhi = requests.get(url_shizhi).json()

    com_zongshizhi = json_shizhi['data']['f116']

    url_lirun = f'https://emweb.eastmoney.com/PC_HSF10/NewFinanceAnalysis/lrbAjaxNew?companyType=4&reportDateType=1&reportType=1&dates=2021-12-31&code={com_code}'
    json_lirun = requests.get(url_lirun).json()

    com_yingye = json_lirun['data'][0]['TOTAL_OPERATE_INCOME']
    com_yingye_yoy = json_lirun['data'][0]['TOTAL_OPERATE_INCOME_YOY']
    com_lirun = json_lirun['data'][0]['TOTAL_PROFIT']
    com_lirun_yoy = json_lirun['data'][0]['TOTAL_PROFIT_YOY']

    for companyType in range(4, 0, -1):
        try:
            url_zichan = f'https://emweb.eastmoney.com/PC_HSF10/NewFinanceAnalysis/zcfzbAjaxNew?companyType={companyType}&reportDateType=1&reportType=1&dates=2021-12-31&code={com_code}'
            json_zichan = requests.get(url_zichan).json()

            com_zichan = json_zichan['data'][0]['TOTAL_ASSETS']
            com_gudong = json_zichan['data'][0]['TOTAL_EQUITY']
            break
        except:
            if companyType == 1:
                sys.exit(0)

    url_zhuyao = f'https://emweb.eastmoney.com/PC_HSF10/NewFinanceAnalysis/ZYZBAjaxNew?type=1&code={com_code}'
    json_zhuyao = requests.get(url_zhuyao).json()
    com_zichan_shouyi = json_zhuyao['data'][0]['ROEJQ']

    if com_money:
        com_money = round(com_money, 4)
    if com_zongshizhi:
        com_zongshizhi = round(com_zongshizhi, 4)
    if com_yingye:
        com_yingye = round(com_yingye, 4)
    if com_yingye_yoy:
        com_yingye_yoy = round(com_yingye_yoy, 4)
    if com_lirun:
        com_lirun = round(com_lirun, 4)
    if com_lirun_yoy:
        com_lirun_yoy = round(com_lirun_yoy, 4)
    if com_zichan:
        com_zichan = round(com_zichan, 4)
    if com_zichan_shouyi:
        com_zichan_shouyi = round(com_zichan_shouyi, 4)
    if com_gudong:
        com_gudong = round(com_gudong, 4)

    list_info = [
        com_type,
        com_money,
        com_zongshizhi,
        com_yingye,
        com_yingye_yoy,
        com_lirun,
        com_lirun_yoy,
        com_zichan,
        com_zichan_shouyi,
        com_gudong,
        com_c
    ]
    list_all_info.append(list_info)

list_all_info_tuple = []
for list_info in list_all_info:
    list_all_info_tuple.append(tuple(list_info))

with cnx.cursor() as cursor:
    Upsql = ''' update sys_base_enterprise_ipo set enterprise_type = %s,total_market_value = %s,before_total_market_value = %s,operating_revenue = %s,operating_revenue_rate = %s,profit = %s,profit_rate = %s,assets = %s,return_on_assets = %s,shareholders_equity = %s where securities_code = %s '''
    cursor.executemany(Upsql, list_all_info_tuple)
    cnx.commit()
