admin
2025-06-10 568c763084b926a6f2d632b7ac65b9ec8280752f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# 开盘啦涨停数据管理
import json
import os
import threading
import time
 
from utils import tool
from kpl import kpl_util
from kpl import kpl_api
 
 
class KPLLimitUpDataManager:
    __instance = None
    limit_up_records_dict = {}
    limit_up_current_dict = {}
    __limit_up_history_datas_cache = {}
    __limit_up_current_datas_cache = {}
 
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(KPLLimitUpDataManager, cls).__new__(cls, *args, **kwargs)
            cls.__load_data()
        return cls.__instance
 
    @classmethod
    def __load_data(cls, day=tool.get_now_date_str()):
        record_apth = cls.__get_record_path(day)
        current_path = cls.__get_current_path(day)
        limit_up_records_dict = {}
        limit_up_current_dict = {}
 
        # 获取历史涨停
        if os.path.exists(record_apth):
            children = os.listdir(record_apth)
            for c in children:
                with open(f"{record_apth}\\{c}", mode="r") as f:
                    line = f.readline().strip()
                    if line:
                        try:
                            data = json.loads(line)
                            limit_up_records_dict[data[0]] = data
                        except:
                             pass
 
        # 获取实时涨停
        if os.path.exists(current_path):
            with open(current_path, mode="r") as f:
                line = f.readline().strip()
                if line:
                    try:
                        datas = json.loads(line)
                        for d in datas:
                            limit_up_current_dict[d[0]] = d
                    except:
                        pass
        if day == tool.get_now_date_str():
            cls.limit_up_records_dict = limit_up_records_dict
            cls.limit_up_current_dict = limit_up_current_dict
        return limit_up_records_dict, limit_up_current_dict
 
    def __save_data(self, limit_up_datas):
        record_path = self.__get_record_path()
        current_path = self.__get_current_path()
        if not os.path.exists(record_path):
            os.makedirs(record_path)
        for d in limit_up_datas:
            code = d[0]
            # 保存记录数据
            with open(f"{record_path}\\{code}.log", mode='w') as f:
                f.write(json.dumps(d))
 
        with open(current_path, mode='w') as f:
            f.write(json.dumps(limit_up_datas))
 
    def add_data(self, limit_up_datas):
        temp_limit_up_dict = {}
        for d in limit_up_datas:
            temp_limit_up_dict[d[0]] = d
            self.limit_up_records_dict[d[0]] = d
        self.limit_up_current_dict = temp_limit_up_dict
        # 保存文件
        threading.Thread(target=self.__save_data, args=(limit_up_datas,), daemon=True).start()
 
    def get_limit_up_current_datas(self, day=tool.get_now_date_str()):
        if day == tool.get_now_date_str():
            temps = [self.limit_up_current_dict[k] for k in self.limit_up_current_dict]
        else:
            records, current = self.__load_data(day)
            temps = [current[k] for k in current]
        temps.sort(key=lambda x: x[2], reverse=True)
        return temps
 
    def get_limit_up_history_datas(self, day=tool.get_now_date_str()):
        if day == tool.get_now_date_str():
            temps = [self.limit_up_records_dict[k] for k in self.limit_up_records_dict]
        else:
            records, current = self.__load_data(day)
            temps = [records[k] for k in records]
        temps.sort(key=lambda x: x[2], reverse=True)
        return temps
 
    # 从缓存文件获取某天的涨停数据
    def get_limit_up_current_datas_from_file(self, day):
        current_path = self.__get_current_path(day)
        # 获取实时涨停
        if os.path.exists(current_path):
            with open(current_path, mode="r") as f:
                line = f.readline().strip()
                if line:
                    datas = json.loads(line)
                    return datas
        return None
 
    # 从缓存文件获取某天的历史涨停数据
    def get_limit_up_history_datas__from_file(self, day):
        record_apth = self.__get_record_path(day)
        if os.path.exists(record_apth):
            fdatas = []
            children = os.listdir(record_apth)
            for c in children:
                with open(f"{record_apth}\\{c}", mode="r") as f:
                    line = f.readline().strip()
                    if line:
                        data = json.loads(line)
                        fdatas.append(data)
            return fdatas
        return None
 
    def get_limit_up_history_datas_cache(self, day):
        if day not in self.__limit_up_history_datas_cache:
            results = self.get_limit_up_history_datas__from_file(day)
            if results:
                self.__limit_up_history_datas_cache[day] = results
        return self.__limit_up_history_datas_cache.get(day)
 
    def get_limit_up_current_datas_cache(self, day):
        if day not in self.__limit_up_current_datas_cache:
            results = self.get_limit_up_current_datas_from_file(day)
            if results:
                self.__limit_up_current_datas_cache[day] = results
        return self.__limit_up_current_datas_cache.get(day)
 
    @classmethod
    def __get_record_path(cls, day=None):
        base_path = cls.__get_base_path(day)
        return f"{base_path}\\record"
 
    @classmethod
    def __get_current_path(cls, day=None):
        base_path = cls.__get_base_path(day)
        return f"{base_path}\\limit_up.log"
 
    @classmethod
    def __get_base_path(cls, day=None):
        if not day:
            day = tool.get_now_date_str()
        return f"{os.getcwd()}\\datas\\{day}"
 
    def run(self):
        while True:
            try:
                if not tool.is_trade_time(tool.get_now_time_str()):
                    continue
                result = kpl_api.getLimitUpInfoNew()
                limit_up_datas = kpl_util.parseLimitUpData(result)
                KPLLimitUpDataManager().add_data(limit_up_datas)
            except:
                pass
            finally:
                time.sleep(3)
 
 
if __name__ == '__main__':
    print(KPLLimitUpDataManager().get_limit_up_current_datas())
    print(KPLLimitUpDataManager().get_limit_up_history_datas())
    input()