import redis
from base.BaseCore import BaseCore

baseCore = BaseCore()
log = baseCore.getLogger()
cnx = baseCore.cnx
cursor = baseCore.cursor
r = redis.Redis(host="114.115.236.206", port=6379, password='clbzzsn')

#获取所有键
keys = r.keys('*')
for key in keys:
    f_key = key.decode()
    if 'wx_url' in f_key:
        list_info = []
        print(f_key)
        print("----------")
        # 截取sid
        sid = f_key.split('url_')[1]
        #拿到微信公众号的key 获取值
        value = list(r.smembers(f_key))
        list_data = []
        for member in value:
            member = member.decode()
            # print(member)
            data = (sid,member)
            # print(data)
        # print(list_info)
            select_sql = f"select sid from wx_link where link = '{member}'"
            cursor.execute(select_sql)
            select = cursor.fetchone()
            if select:
                continue
            else:
                list_info.append(data)
        if len(list_info)!=0:
            pass
        else:
            continue
        #批量插入
        sql = "INSERT INTO wx_link (sid, link,state) VALUES (%s, %s,'99')"
        cursor.executemany(sql, list_info)
        cnx.commit()
        log.info(f'{sid}---插入成功--{len(list_info)}----')
baseCore.close()


