Administrator
2023-07-24 0e68e24f54db11d340785b17570fff2bc5fc7ac6
third_data/kpl_data_manager.py
@@ -2,17 +2,18 @@
import os
import constant
import tool
from utils import tool
# 开盘啦历史涨停数据管理
from db import mysql_data, redis_manager
from l2 import code_price_manager
from log import logger_kpl_limit_up_reason_change
from third_data import kpl_util
from third_data.code_plate_key_manager import LimitUpCodesPlateKeyManager
from log_module.log import logger_kpl_limit_up_reason_change
from third_data import kpl_util, kpl_api
from third_data.code_plate_key_manager import LimitUpCodesPlateKeyManager, CodesHisReasonAndBlocksManager
# 代码对应的涨停原因保存
from third_data.kpl_util import KPLPlatManager, KPLDataType
class KPLCodeLimitUpReasonManager:
    __redisManager = redis_manager.RedisManager(3)
@@ -31,37 +32,27 @@
        return dict_
class KPLPlatManager:
    def save_plat(self, _id, name):
        if not _id:
            return
        mysqldb = mysql_data.Mysqldb()
        key = f"{_id}-{name}"
        results = mysqldb.select_one(f"select * from kpl_plate where _name='{name}'")
        if not results:
            mysqldb.execute(f"insert into kpl_plate(_id,_name,_key) values({_id},'{name}','{key}')")
    def get_plat(self, name):
        mysqldb = mysql_data.Mysqldb()
        results = mysqldb.select_one(f"select * from kpl_plate where _name='{name}'")
        if results:
            return results[0]
        return None
    def get_same_plat_names(self, name):
        mysqldb = mysql_data.Mysqldb()
        plate = self.get_plat(name)
        if not plate:
            return {name}
        results = mysqldb.select_all(f"select _name from kpl_plate where _id='{plate}'")
        return set([r[0] for r in results])
class KPLLimitUpDataRecordManager:
    total_datas = None
    latest_datas = {}
    latest_origin_datas=[]
    __kplPlatManager = KPLPlatManager()
    __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager()
    __CodesPlateKeysManager = CodesHisReasonAndBlocksManager()
    @classmethod
    def __load_hist_and_blocks(cls, code):
        # 有数据新增,加载历史原因与板块
        his_reasons = cls.get_latest_infos(code, 10, False)
        his_reasons = set([r[0] for r in his_reasons])
        cls.__CodesPlateKeysManager.set_history_limit_up_reason(code, his_reasons)
        try:
            if not cls.__CodesPlateKeysManager.get_blocks(code):
                results = kpl_api.getStockIDPlate(code)
                bs = [r[1] for r in results]
                cls.__CodesPlateKeysManager.set_blocks(code, bs)
        except Exception as e:
            pass
    @classmethod
    def save_record(cls, day, records):
@@ -76,6 +67,7 @@
                code_block_dict[code].add(b)
                # 设置涨停数据
        if records:
            cls.latest_origin_datas = records
            cls.__LimitUpCodesPlateKeyManager.set_today_limit_up([(r[0], r[5]) for r in records])
        # 涨停数据记录
@@ -96,7 +88,7 @@
            if not result:
                mysqldb.execute(
                    f"insert into kpl_limit_up_record(_id,_day,_hot_block_name,_code,_code_name,_limit_up_time,_blocks,_latest_limit_up_time,_update_time,_create_time,_hot_block_code_count,_limit_up_high_info,_zylt_val) values('{_id}','{day}','{d[5]}','{d[0]}','{d[1]}','{d[2]}','{d[6]}','{d[3]}',now(),now(),{d[10]},'{d[4]}',{d[7]})")
                cls.__load_hist_and_blocks(code)
            else:
                if _id in cls.latest_datas and json.dumps(cls.latest_datas.get(_id)) != json.dumps(d):
                    mysqldb.execute(
@@ -126,6 +118,8 @@
    def load_total_datas(cls):
        cls.total_datas = KPLLimitUpDataRecordManager.list_all(tool.get_now_date_str())
        cls.__LimitUpCodesPlateKeyManager.set_today_total_limit_up([(r[3], r[2]) for r in cls.total_datas])
        for d in cls.total_datas:
            cls.__load_hist_and_blocks(d[3])
    @staticmethod
    def list_all(day):
@@ -216,6 +210,28 @@
                return json.loads(lines[0])
        return None
    # 获取最近几天的数据,根据日期倒序返回
    def get_latest_from_file(self, type, count):
        files = os.listdir(constant.CACHE_PATH)
        file_name_list = []
        for f in files:
            if f[10:] == f"_{type.value}.log":
                file_name_list.append((f.split("_")[0], f))
        file_name_list.sort(key=lambda x: x[0], reverse=True)
        file_name_list = file_name_list[:count]
        fresults = []
        for file in file_name_list:
            path = f"{constant.CACHE_PATH}/{file[1]}"
            if not os.path.exists(path):
                continue
            with open(path, 'r') as f:
                lines = f.readlines()
                if lines:
                    fresults.append((file[0], json.loads(lines[0])))
        return fresults
    def save_data(self, type, datas):
        self.__latest_datas[type] = datas
        self.__save_in_file(type, datas)
@@ -231,11 +247,11 @@
def load_history_limit_up():
    for file_name in os.listdir("D:/kpl/his"):
    for file_name in os.listdir(f"{constant.get_path_prefix()}/kpl/his"):
        if file_name.find("HisDaBanList_1.log") < 0:
            continue
        day = file_name[:10]
        with open(f"D:/kpl/his/{file_name}", 'r', encoding="utf-16") as f:
        with open(f"{constant.get_path_prefix()}/kpl/his/{file_name}", 'r', encoding="utf-16") as f:
            lines = f.readlines()
            line = lines[0]
            result = json.loads(line)
@@ -248,6 +264,34 @@
            # print(day, list_)
# 历史涨停列表
__limit_up_list_records_dict = {}
# 获取最近几天的实时涨停信息
# 返回格式([日期,数据])
def get_current_limit_up_data_records(count):
    fresults = []
    day = tool.get_now_date_str()
    datas = []
    if day in __limit_up_list_records_dict:
        datas = __limit_up_list_records_dict[day]
    else:
        datas = KPLDataManager().get_latest_from_file(KPLDataType.LIMIT_UP, 10)
        if datas:
            # 保存数据
            __limit_up_list_records_dict[day] = datas
    for i in range(len(datas)):
        if datas[i][0] == day:
            continue
        fresults.append(datas[i])
        if len(fresults) >= count:
            break
    return fresults
if __name__ == "__main__":
    ds = set(["1", "2", "3"])
    print(ds.pop())
    fresults = get_current_limit_up_data_records(2)
    for d in fresults:
        print(d)
    get_current_limit_up_data_records(2)