ezKit 1.8.3__py3-none-any.whl → 1.8.5__py3-none-any.whl

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.
ezKit/cls.py CHANGED
@@ -94,75 +94,194 @@ def up_down_analysis(
94
94
 
95
95
 
96
96
  def latest_data(
97
- payload: dict,
98
- end: str = "basic",
97
+ payload: str | dict,
99
98
  data_type: str = "stock",
100
99
  df: bool = False
101
- ) -> dict | pd.DataFrame | None:
100
+ ) -> list | pd.DataFrame | None:
102
101
  """股票或板块的最新数据"""
103
102
 
103
+ # ----------------------------------------------------------------------------------------------
104
+
105
+ # 判断参数类型
104
106
  match True:
105
- case True if isinstance(payload, dict) is False:
107
+ case True if True not in [isinstance(payload, str), isinstance(payload, dict)]:
106
108
  logger.error("Incorrect function argument type: payload")
107
109
  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:
110
+ case True if False in [isinstance(data_type, str), utils.v_true(data_type, str)]:
112
111
  logger.error("Incorrect function argument type: data_type")
113
112
  return None
114
113
  case _:
115
114
  pass
116
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
+ # 个股 (默认)
117
128
  info: str = "获取股票最新数据"
118
129
 
119
- if utils.v_true(data_type, str) is True and data_type == "plate":
130
+ # 板块
131
+ if data_type == "plate":
120
132
  info = "获取板块最新数据"
121
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
+
122
144
  try:
123
145
 
124
146
  logger.info(f"{info} ......")
125
147
 
148
+ # ------------------------------------------------------------------------------------------
149
+
150
+ # HTTP User Agent
126
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
127
154
  headers = {"User-Agent": user_agent}
128
155
 
129
- # https://x-quote.cls.cn/quote/stock/basic?secu_code={code}
130
- api = f"https://x-quote.cls.cn/quote/stock/{end}"
156
+ # ------------------------------------------------------------------------------------------
131
157
 
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}"
158
+ # 请求参数
159
+ params: dict = {}
160
+
161
+ # 默认请求参数
162
+ if isinstance(payload, str) and utils.v_true(payload, str):
163
+ params = {"secu_code": payload}
137
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
+ # 获取数据
138
197
  # response = requests.get(api, headers=headers, timeout=10)
139
- response = requests.get(api, headers=headers, params=payload, timeout=10)
198
+ response = requests.get(api, headers=headers, params=params, timeout=10)
140
199
 
200
+ # 转换数据类型
141
201
  response_dict: dict = response.json()
142
202
 
143
- if utils.v_true(df, bool) is False:
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
+ # 默认返回的数据
144
229
  logger.success(f"{info} [成功]")
145
- return response_dict
230
+ return response_dict["data"]
231
+
232
+ # ------------------------------------------------------------------------------------------
233
+
234
+ # 板块
146
235
 
147
- if data_type == "plate" and utils.v_true(df, bool) is True:
236
+ # 板块数据不能转换为 pd.DataFrame
237
+ if (data_type == "plate") and (utils.v_true(df, bool) is True):
148
238
  logger.error(f"{info} [错误]")
149
239
  return None
150
240
 
151
- if response_dict["data"]["trade_status"] == "STOPT":
152
- logger.error(f"{info} [停牌]")
153
- return None
241
+ # 数据结果
242
+ result: list = []
154
243
 
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
- }
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
+ # 返回数据
164
250
  logger.success(f"{info} [成功]")
165
- return pd.DataFrame(data=data_object)
251
+ return result
252
+
253
+ except Exception as e:
254
+ logger.error(f"{info} [失败]")
255
+ logger.exception(e)
256
+ return None
257
+
258
+
259
+ # --------------------------------------------------------------------------------------------------
260
+
261
+
262
+ def plate_codes(plate: str) -> list | None:
263
+ """获取板块成分股代码"""
264
+
265
+ if utils.v_true(plate, str) is False:
266
+ logger.error("Incorrect function argument type: plate")
267
+ return None
268
+
269
+ info: str = "获取板块成分股代码"
270
+
271
+ try:
272
+
273
+ logger.info(f"{info} ......")
274
+
275
+ items = latest_data(payload=plate, data_type="plate")
276
+
277
+ if isinstance(items, list):
278
+ codes: list = [stock.coderename(i["secu_code"], restore=True) for i in items]
279
+ codes.sort()
280
+ logger.success(f"{info} [成功]")
281
+ return codes
282
+
283
+ logger.error(f"{info} [失败]")
284
+ return None
166
285
 
167
286
  except Exception as e:
168
287
  logger.error(f"{info} [失败]")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ezKit
3
- Version: 1.8.3
3
+ Version: 1.8.5
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -2,7 +2,7 @@ ezKit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  ezKit/bottle.py,sha256=usKK1wVaZw4_D-4VwMYmOIc8jtz4TrpM30nck59HMFw,180178
3
3
  ezKit/bottle_extensions.py,sha256=LQikCbbYZBAa4AcihvrTvixWHHMY7OjCBsT02PqWMMM,1129
4
4
  ezKit/cipher.py,sha256=0T_StbjiNI4zgrjVgcfU-ffKgu1waBA9UDudAnqFcNM,2896
5
- ezKit/cls.py,sha256=RM0w2DFWTOtqBnrMUhi-Qx0mecgdKntjo7LxjFRxT2k,6176
5
+ ezKit/cls.py,sha256=Oy6uEYjtIisWFkhWRj9oLiARZm5yJg75zSa_e2K8Kng,10136
6
6
  ezKit/database.py,sha256=kYb0ybACxrdFZnwJ40bTbG8Obh2oEF3GQWmCbPVgH-c,6878
7
7
  ezKit/http.py,sha256=N2x86HCmJ5ejwA3-gHowEgSpaFTFT9nCWFjPuQ1T_do,1801
8
8
  ezKit/mongo.py,sha256=P6WTwFRxaaHixJK_PyKlOfPHkeJRxxrNLV77xy5LVBQ,2048
@@ -13,8 +13,8 @@ ezKit/stock.py,sha256=p-l6Tv0oZdJ3juxkddScphm7BW4vH-mBozBgSH1Qu0w,1730
13
13
  ezKit/token.py,sha256=9CAZhPdXiRiWoOIeWmP0q6L3j1zQAv4YcVWH95Tjefs,1755
14
14
  ezKit/utils.py,sha256=an7joZy_EEpYfN8zBtEWAnhP0YXYfPieabsK_HAxXl4,48921
15
15
  ezKit/xftp.py,sha256=XyIdr_2rxRVLqPofG6fIYWhAMVsFwTyp46dg5P9FLW4,7774
16
- ezKit-1.8.3.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
17
- ezKit-1.8.3.dist-info/METADATA,sha256=zBWEfYeVzFBpNoj8Cgqwdo1xJANBqF5O_yLGX1S-KTQ,190
18
- ezKit-1.8.3.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
19
- ezKit-1.8.3.dist-info/top_level.txt,sha256=aYLB_1WODsqNTsTFWcKP-BN0KCTKcV-HZJ4zlHkCFw8,6
20
- ezKit-1.8.3.dist-info/RECORD,,
16
+ ezKit-1.8.5.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
17
+ ezKit-1.8.5.dist-info/METADATA,sha256=aaBwl8bvxD60a9fHvL-IYjj-ZhM9ysT84zPfocVyNjE,190
18
+ ezKit-1.8.5.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
19
+ ezKit-1.8.5.dist-info/top_level.txt,sha256=aYLB_1WODsqNTsTFWcKP-BN0KCTKcV-HZJ4zlHkCFw8,6
20
+ ezKit-1.8.5.dist-info/RECORD,,
File without changes
File without changes