Administrator
2024-01-19 a00da3062c6c825b585f82275823ac45cdeb6502
third_data/kpl_data_manager.py
@@ -47,7 +47,7 @@
    __kplPlatManager = KPLPlatManager()
    __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager()
    __CodesPlateKeysManager = CodesHisReasonAndBlocksManager()
    __current_code_reason_dict = {}
    __current_code_reasons_dict = {}
    __current_reason_codes_dict = {}
    __records_cache = {}
@@ -81,15 +81,20 @@
            cls.latest_origin_datas = records
            cls.__LimitUpCodesPlateKeyManager.set_today_limit_up([(r[0], r[5]) for r in records])
        code_reason_dict = {}
        reason_code_dict = {}
        code_reasons_dict = {}
        reason_codes_dict = {}
        for d in records:
            code_reason_dict[d[0]] = d[5]
            if d[5] not in reason_code_dict:
                reason_code_dict[d[5]] = set()
            reason_code_dict[d[5]].add(d[0])
        cls.__current_code_reason_dict = code_reason_dict
        cls.__current_reason_codes_dict = reason_code_dict
            # 涨停原因 + 推荐原因
            bs = {d[5]}
            if d[6]:
                bs |= set(d[6].split("、"))
            code_reasons_dict[d[0]] = bs
            for b in bs:
                if b not in reason_codes_dict:
                    reason_codes_dict[b] = set()
                reason_codes_dict[b].add(d[0])
        cls.__current_code_reasons_dict = code_reasons_dict
        cls.__current_reason_codes_dict = reason_codes_dict
        # 涨停数据记录
        mysqldb = mysql_data.Mysqldb()
@@ -161,7 +166,6 @@
        mysqldb = mysql_data.Mysqldb()
        return mysqldb.select_all(f"select * from kpl_limit_up_record where _code='{code}' and _day='{day}'")
    @staticmethod
    def list_by_block(block_name, day):
        mysqldb = mysql_data.Mysqldb()
@@ -214,8 +218,8 @@
        return bs
    @classmethod
    def get_current_block(cls, code):
        return cls.__current_code_reason_dict.get(code)
    def get_current_blocks(cls, code):
        return cls.__current_code_reasons_dict.get(code)
    @classmethod
    def get_current_codes_by_block(cls, block):
@@ -277,8 +281,6 @@
        if fdata:
            cls.__file_content_cache[key] = fdata
        return fdata
    @classmethod
    # 获取最近几天的数据,根据日期倒序返回
@@ -352,7 +354,7 @@
        datas = __limit_up_list_records_dict[day]
    else:
        logger_debug.info("从文件中获取前几天的实时涨停数据")
        datas = KPLDataManager().get_latest_from_file(KPLDataType.LIMIT_UP, 10)
        datas = KPLDataManager().get_latest_from_file(KPLDataType.LIMIT_UP, count + 2)
        if datas:
            # 保存数据
            __limit_up_list_records_dict[day] = datas
@@ -376,6 +378,18 @@
    return yesterday_limit_up_data_records
# 获取最近几天涨停原因
__latest_current_limit_up_records = {}
def get_latest_current_limit_up_records():
    day = tool.get_now_date_str()
    if day not in __latest_current_limit_up_records:
        fdatas = get_current_limit_up_data_records(15)
        __latest_current_limit_up_records[day] = fdatas
    return __latest_current_limit_up_records.get(day)
# 运行拉取任务
def run_pull_task():
    def __upload_data(type, datas):
@@ -386,19 +400,13 @@
        requests.post("http://127.0.0.1:9004/upload_kpl_data", json.dumps(root_data))
    def get_limit_up():
        last_time = 0
        while True:
            if tool.is_trade_time():
            if tool.is_trade_time() and int(tool.get_now_time_str().replace(':', '')) > int("092530"):
                try:
                    results = kpl_api.getLimitUpInfo()
                    results = kpl_api.getLimitUpInfoNew()
                    result = json.loads(results)
                    start_time = time.time()
                    __upload_data("limit_up", result)
                    if start_time - last_time >= 60:
                        last_time = time.time()
                        # 记录涨停数据
                        logger_kpl_limit_up.info(result)
                    # logger_kpl_limit_up_reason_change.info("上传耗时:{}", time.time() - start_time)
                except Exception as e:
                    logging.exception(e)
            time.sleep(3)
@@ -438,11 +446,12 @@
            time.sleep(3)
    threading.Thread(target=get_limit_up, daemon=True).start()
    threading.Thread(target=get_bidding_money, daemon=True).start()
    # threading.Thread(target=get_bidding_money, daemon=True).start()
    # threading.Thread(target=get_market_industry, daemon=True).start()
    # threading.Thread(target=get_market_jingxuan, daemon=True).start()
if __name__ == "__main__":
    run_pull_task()
    print(get_latest_current_limit_up_records())
    print(get_latest_current_limit_up_records())
    input()