ezKit 1.8.3__tar.gz → 1.8.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ezKit
3
- Version: 1.8.3
3
+ Version: 1.8.4
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -0,0 +1,256 @@
1
+ """财联社数据"""
2
+ import re
3
+
4
+ import pandas as pd
5
+ import requests
6
+ from loguru import logger
7
+
8
+ from . import stock, utils
9
+
10
+
11
+ def up_down_analysis(
12
+ target: str = "up_pool",
13
+ df: bool = False
14
+ ) -> list | pd.DataFrame | None:
15
+ """涨停跌停数据"""
16
+
17
+ if not utils.v_true(target, str):
18
+ logger.error(f"error type: {target}")
19
+ return None
20
+
21
+ info: str = "获取涨停池股票"
22
+ match True:
23
+ case True if target == "up_pool":
24
+ info = "获取涨停池股票"
25
+ case True if target == "continuous_up_pool":
26
+ info = "获取连板池股票"
27
+ case True if target == "up_open_pool":
28
+ info = "获取炸板池股票"
29
+ case True if target == "down_pool":
30
+ info = "获取跌停池股票"
31
+ case _:
32
+ pass
33
+
34
+ try:
35
+ logger.info(f"{info} ......")
36
+
37
+ user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
38
+ headers = {"User-Agent": user_agent}
39
+
40
+ # 涨停池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=up_pool
41
+ # 连板池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=continuous_up_pool
42
+ # 炸板池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=up_open_pool
43
+ # 跌停池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=down_pool
44
+ api = f"https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type={target}"
45
+
46
+ response = requests.get(api, headers=headers, timeout=10)
47
+
48
+ response_dict: dict = response.json()
49
+
50
+ result: list = []
51
+
52
+ for i in response_dict["data"]:
53
+
54
+ # if re.match(r"^(sz00|sh60)", i["secu_code"]):
55
+ # print(i["secu_code"])
56
+
57
+ # if re.search(r"ST|银行", i["secu_name"]):
58
+ # print(i["secu_name"])
59
+
60
+ # 主板, 非ST, 非银行, 非证券
61
+ if (not re.match(r"^(sz00|sh60)", i["secu_code"])) or re.search(r"ST|银行|证券", i["secu_name"]):
62
+ continue
63
+
64
+ if target in ["up_pool", "up_pool"]:
65
+ result.append({
66
+ "code": stock.coderename(i["secu_code"], restore=True),
67
+ "name": i["secu_name"],
68
+ "up_days": i["limit_up_days"],
69
+ "reason": i["up_reason"]
70
+ })
71
+
72
+ if target in ["up_open_pool", "down_pool"]:
73
+ result.append({
74
+ "code": stock.coderename(i["secu_code"], restore=True),
75
+ "name": i["secu_name"]
76
+ })
77
+
78
+ if utils.v_true(df, bool) is False:
79
+ logger.success(f"{info} [成功]")
80
+ return result
81
+
82
+ # data: pd.DataFrame = pd.DataFrame(response_dict["data"], columns=["secu_code", "secu_name", "limit_up_days", "up_reason"])
83
+ # data = data.rename(columns={"secu_code": "code", "secu_name": "name", "limit_up_days": "up_days", "up_reason": "reason"})
84
+
85
+ return pd.DataFrame(data=pd.DataFrame(result))
86
+
87
+ except Exception as e:
88
+ logger.error(f"{info} [失败]")
89
+ logger.exception(e)
90
+ return None
91
+
92
+
93
+ # --------------------------------------------------------------------------------------------------
94
+
95
+
96
+ def latest_data(
97
+ payload: str | dict,
98
+ data_type: str = "stock",
99
+ df: bool = False
100
+ ) -> list | pd.DataFrame | None:
101
+ """股票或板块的最新数据"""
102
+
103
+ # ----------------------------------------------------------------------------------------------
104
+
105
+ # 判断参数类型
106
+ match True:
107
+ case True if True not in [isinstance(payload, str), isinstance(payload, dict)]:
108
+ logger.error("Incorrect function argument type: payload")
109
+ return None
110
+ case True if False in [isinstance(data_type, str), utils.v_true(data_type, str)]:
111
+ logger.error("Incorrect function argument type: data_type")
112
+ return None
113
+ case _:
114
+ pass
115
+
116
+ # ----------------------------------------------------------------------------------------------
117
+
118
+ # 判断数据类型. 数据类型: 个股, 板块 (产业链: industry)
119
+ if data_type not in ["stock", "plate"]:
120
+ logger.error("data_type error")
121
+ return None
122
+
123
+ # ----------------------------------------------------------------------------------------------
124
+
125
+ # 日志信息
126
+
127
+ # 个股 (默认)
128
+ info: str = "获取股票最新数据"
129
+
130
+ # 板块
131
+ if data_type == "plate":
132
+ info = "获取板块最新数据"
133
+
134
+ # match True:
135
+ # case True if data_type == "plate":
136
+ # info = "获取板块最新数据"
137
+ # case True if data_type == "industry":
138
+ # info = "获取产业链最新数据"
139
+ # case _:
140
+ # pass
141
+
142
+ # ----------------------------------------------------------------------------------------------
143
+
144
+ try:
145
+
146
+ logger.info(f"{info} ......")
147
+
148
+ # ------------------------------------------------------------------------------------------
149
+
150
+ # HTTP User Agent
151
+ user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
152
+
153
+ # HTTP Headers
154
+ headers = {"User-Agent": user_agent}
155
+
156
+ # ------------------------------------------------------------------------------------------
157
+
158
+ # 请求参数
159
+ params: dict = {}
160
+
161
+ # 默认请求参数
162
+ if isinstance(payload, str) and utils.v_true(payload, str):
163
+ params = {"secu_code": payload}
164
+
165
+ # 请求参数
166
+ if isinstance(payload, dict) and utils.v_true(payload, dict):
167
+ params = payload
168
+
169
+ # ------------------------------------------------------------------------------------------
170
+
171
+ # 不直接在API后面跟参数, 使用 params 传递参数
172
+
173
+ # API: 股票
174
+ # api: str = f"https://x-quote.cls.cn/quote/stock/basic?secu_code={code}"
175
+ api: str = "https://x-quote.cls.cn/quote/stock/basic"
176
+
177
+ # API: 板块
178
+ if data_type == "plate":
179
+ # api = f"https://x-quote.cls.cn/web_quote/plate/stocks?secu_code={code}"
180
+ api = "https://x-quote.cls.cn/web_quote/plate/stocks"
181
+
182
+ # match True:
183
+ # case True if data_type == "plate":
184
+ # # 板块
185
+ # # api = f"https://x-quote.cls.cn/web_quote/plate/stocks?secu_code={code}"
186
+ # api = "https://x-quote.cls.cn/web_quote/plate/stocks"
187
+ # case True if data_type == "industry":
188
+ # # 产业链
189
+ # # api = f"https://x-quote.cls.cn/web_quote/plate/industry?secu_code={code}"
190
+ # api = "https://x-quote.cls.cn/web_quote/plate/industry"
191
+ # case _:
192
+ # pass
193
+
194
+ # ------------------------------------------------------------------------------------------
195
+
196
+ # 获取数据
197
+ # response = requests.get(api, headers=headers, timeout=10)
198
+ response = requests.get(api, headers=headers, params=params, timeout=10)
199
+
200
+ # 转换数据类型
201
+ response_dict: dict = response.json()
202
+
203
+ # ------------------------------------------------------------------------------------------
204
+
205
+ # 个股
206
+
207
+ if data_type == "stock":
208
+
209
+ # 停牌, 返回 None
210
+ if response_dict["data"]["trade_status"] == "STOPT":
211
+ logger.error(f"{info} [停牌]")
212
+ return None
213
+
214
+ # pd.DataFrame 数据
215
+ if utils.v_true(df, bool) is True:
216
+ df_data = {
217
+ # "date": [pd.to_datetime(date_today)],
218
+ "open": [float(response_dict["data"]["open_px"])],
219
+ "close": [float(response_dict["data"]["last_px"])],
220
+ "high": [float(response_dict["data"]["high_px"])],
221
+ "low": [float(response_dict["data"]["low_px"])],
222
+ "volume": [int(response_dict["data"]["business_amount"])],
223
+ "turnover": [float(response_dict["data"]["tr"])]
224
+ }
225
+ logger.success(f"{info} [成功]")
226
+ return pd.DataFrame(data=df_data)
227
+
228
+ # 默认返回的数据
229
+ logger.success(f"{info} [成功]")
230
+ return response_dict["data"]
231
+
232
+ # ------------------------------------------------------------------------------------------
233
+
234
+ # 板块
235
+
236
+ # 板块数据不能转换为 pd.DataFrame
237
+ if (data_type == "plate") and (utils.v_true(df, bool) is True):
238
+ logger.error(f"{info} [错误]")
239
+ return None
240
+
241
+ # 数据结果
242
+ result: list = []
243
+
244
+ # 筛选 主板, 非ST, 非银行, 非证券 的股票
245
+ for i in response_dict["data"]["stocks"]:
246
+ if (re.match(r"^(sz00|sh60)", i["secu_code"])) and (not re.search(r"ST|银行|证券", i["secu_name"])):
247
+ result.append(i)
248
+
249
+ # 返回数据
250
+ logger.success(f"{info} [成功]")
251
+ return result
252
+
253
+ except Exception as e:
254
+ logger.error(f"{info} [失败]")
255
+ logger.exception(e)
256
+ return None
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ezKit
3
- Version: 1.8.3
3
+ Version: 1.8.4
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -3,7 +3,7 @@ from setuptools import find_packages, setup
3
3
 
4
4
  setup(
5
5
  name='ezKit',
6
- version='1.8.3',
6
+ version='1.8.4',
7
7
  author='septvean',
8
8
  author_email='septvean@gmail.com',
9
9
  description='Easy Kit',
ezkit-1.8.3/ezKit/cls.py DELETED
@@ -1,170 +0,0 @@
1
- """财联社数据"""
2
- import re
3
-
4
- import pandas as pd
5
- import requests
6
- from loguru import logger
7
-
8
- from . import stock, utils
9
-
10
-
11
- def up_down_analysis(
12
- target: str = "up_pool",
13
- df: bool = False
14
- ) -> list | pd.DataFrame | None:
15
- """涨停跌停数据"""
16
-
17
- if not utils.v_true(target, str):
18
- logger.error(f"error type: {target}")
19
- return None
20
-
21
- info: str = "获取涨停池股票"
22
- match True:
23
- case True if target == "up_pool":
24
- info = "获取涨停池股票"
25
- case True if target == "continuous_up_pool":
26
- info = "获取连板池股票"
27
- case True if target == "up_open_pool":
28
- info = "获取炸板池股票"
29
- case True if target == "down_pool":
30
- info = "获取跌停池股票"
31
- case _:
32
- pass
33
-
34
- try:
35
- logger.info(f"{info} ......")
36
-
37
- user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
38
- headers = {"User-Agent": user_agent}
39
-
40
- # 涨停池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=up_pool
41
- # 连板池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=continuous_up_pool
42
- # 炸板池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=up_open_pool
43
- # 跌停池: https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type=down_pool
44
- api = f"https://x-quote.cls.cn/quote/index/up_down_analysis?rever=1&way=last_px&type={target}"
45
-
46
- response = requests.get(api, headers=headers, timeout=10)
47
-
48
- response_dict: dict = response.json()
49
-
50
- result: list = []
51
-
52
- for i in response_dict["data"]:
53
-
54
- # if re.match(r"^(sz00|sh60)", i["secu_code"]):
55
- # print(i["secu_code"])
56
-
57
- # if re.search(r"ST|银行", i["secu_name"]):
58
- # print(i["secu_name"])
59
-
60
- # 主板, 非ST, 非银行, 非证券
61
- if (not re.match(r"^(sz00|sh60)", i["secu_code"])) or re.search(r"ST|银行|证券", i["secu_name"]):
62
- continue
63
-
64
- if target in ["up_pool", "up_pool"]:
65
- result.append({
66
- "code": stock.coderename(i["secu_code"], restore=True),
67
- "name": i["secu_name"],
68
- "up_days": i["limit_up_days"],
69
- "reason": i["up_reason"]
70
- })
71
-
72
- if target in ["up_open_pool", "down_pool"]:
73
- result.append({
74
- "code": stock.coderename(i["secu_code"], restore=True),
75
- "name": i["secu_name"]
76
- })
77
-
78
- if utils.v_true(df, bool) is False:
79
- logger.success(f"{info} [成功]")
80
- return result
81
-
82
- # data: pd.DataFrame = pd.DataFrame(response_dict["data"], columns=["secu_code", "secu_name", "limit_up_days", "up_reason"])
83
- # data = data.rename(columns={"secu_code": "code", "secu_name": "name", "limit_up_days": "up_days", "up_reason": "reason"})
84
-
85
- return pd.DataFrame(data=pd.DataFrame(result))
86
-
87
- except Exception as e:
88
- logger.error(f"{info} [失败]")
89
- logger.exception(e)
90
- return None
91
-
92
-
93
- # --------------------------------------------------------------------------------------------------
94
-
95
-
96
- def latest_data(
97
- payload: dict,
98
- end: str = "basic",
99
- data_type: str = "stock",
100
- df: bool = False
101
- ) -> dict | pd.DataFrame | None:
102
- """股票或板块的最新数据"""
103
-
104
- match True:
105
- case True if isinstance(payload, dict) is False:
106
- logger.error("Incorrect function argument type: payload")
107
- return None
108
- case True if isinstance(end, str) is False:
109
- logger.error("Incorrect function argument type: end")
110
- return None
111
- case True if isinstance(data_type, str) is False:
112
- logger.error("Incorrect function argument type: data_type")
113
- return None
114
- case _:
115
- pass
116
-
117
- info: str = "获取股票最新数据"
118
-
119
- if utils.v_true(data_type, str) is True and data_type == "plate":
120
- info = "获取板块最新数据"
121
-
122
- try:
123
-
124
- logger.info(f"{info} ......")
125
-
126
- user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
127
- headers = {"User-Agent": user_agent}
128
-
129
- # https://x-quote.cls.cn/quote/stock/basic?secu_code={code}
130
- api = f"https://x-quote.cls.cn/quote/stock/{end}"
131
-
132
- if data_type == "plate":
133
- # https://x-quote.cls.cn/web_quote/plate/stocks?secu_code=cls80382
134
- # https://x-quote.cls.cn/web_quote/plate/stocks?secu_code={code}
135
- # https://x-quote.cls.cn/web_quote/plate/industry?secu_code={code}
136
- api = f"https://x-quote.cls.cn/web_quote/plate/{end}"
137
-
138
- # response = requests.get(api, headers=headers, timeout=10)
139
- response = requests.get(api, headers=headers, params=payload, timeout=10)
140
-
141
- response_dict: dict = response.json()
142
-
143
- if utils.v_true(df, bool) is False:
144
- logger.success(f"{info} [成功]")
145
- return response_dict
146
-
147
- if data_type == "plate" and utils.v_true(df, bool) is True:
148
- logger.error(f"{info} [错误]")
149
- return None
150
-
151
- if response_dict["data"]["trade_status"] == "STOPT":
152
- logger.error(f"{info} [停牌]")
153
- return None
154
-
155
- data_object = {
156
- # "date": [pd.to_datetime(date_today)],
157
- "open": [float(response_dict["data"]["open_px"])],
158
- "close": [float(response_dict["data"]["last_px"])],
159
- "high": [float(response_dict["data"]["high_px"])],
160
- "low": [float(response_dict["data"]["low_px"])],
161
- "volume": [int(response_dict["data"]["business_amount"])],
162
- "turnover": [float(response_dict["data"]["tr"])]
163
- }
164
- logger.success(f"{info} [成功]")
165
- return pd.DataFrame(data=data_object)
166
-
167
- except Exception as e:
168
- logger.error(f"{info} [失败]")
169
- logger.exception(e)
170
- return None
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes