import requests,pymysql,time,re,json
from datetime import datetime
import locale
locale.setlocale(locale.LC_CTYPE,"chinese")
# 近期市值
#cnx = pymysql.connect(host='114.115.159.144',user='root', password='zzsn9988', db='clb_project', charset='utf8mb4')  # mysql数据库链接
cnx = pymysql.connect(host='114.116.44.11',user='caiji', password='f7s0&7qqtK', db='clb_project', charset='utf8mb4')  # mysql数据库链接
def tableUpdate(social_id, v):

    current_date = datetime.now().date()

    # 格式化日期为 %年%月%日 格式
    formatted_date = current_date.strftime("%Y年%m月%d日")
    with cnx.cursor() as cursor:
        Upsql = ''' update ranklist_state_holding set recent_market_value = %s, recent_date = %s where social_credit_code = "{}" '''.format(social_id)
        cursor.execute(Upsql,v,formatted_date)  # 更新
        cnx.commit()  # 提交
        print("更新完成:{}".format(social_id))

with cnx.cursor() as cursor:
    querySql = '''select stock_code,social_credit_code,listed_exchange from ranklist_state_holding'''
    cursor.execute(querySql)
    selects = cursor.fetchall()
    
    for row in selects:
        code = row[0]
        social_id = row[1]
        try:
            if 'HK' in code:
                continue
        except:
            continue
        code1 = str(code)
        while True:
            if len(code1)<6:
                code1 = '0'+code1
            else:
                break
        if code1[0] == '0' or code1[0] == '3':
            com_code = 'SZ'+code1
        elif code1[0] == '6':
            com_code = 'SH'+code1
        elif code1[0] == '8':
            com_code = 'BJ'+code1
        
        t = int(time.time()*1000)
        if 'SZ' in com_code:
            code_sz = com_code[2:]
            url2 = f'https://push2.eastmoney.com/api/qt/stock/get?invt=2&fltt=1&cb=jQuery35109980141635843787_1669189162845&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.{code_sz}&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}'
        if 'SH' in com_code:
            code_sh = com_code[2:]
            url2 = f'https://push2.eastmoney.com/api/qt/stock/get?invt=2&fltt=1&cb=jQuery35109980141635843787_1669189162845&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.{code_sh}&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}'
        
        for i in range(0,5):
            try:
                res2 = requests.get(url2)
                break
            except:
                if i >=3:
                    time.sleep(100)
                    continue
                time.sleep(3)
                continue
        ttt = re.findall('\((.*?)\)',res2.text)[0]
        try:
            zongshizhi = json.loads(ttt)['data']['f116']
        except:
            continue
        
        tableUpdate(social_id,zongshizhi)
        print(com_code)