finctx 0.3.0__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.
@@ -0,0 +1,17 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.py[cod]
4
+ .pytest_cache/
5
+ .mypy_cache/
6
+ .ruff_cache/
7
+ .coverage
8
+ htmlcov/
9
+ build/
10
+ dist/
11
+ *.egg-info/
12
+ .env
13
+ .env.*
14
+ !.env.example
15
+ .DS_Store
16
+ skill-setup/skills/
17
+ skill-setup/.skill-package-*/
finctx-0.3.0/LICENSE ADDED
@@ -0,0 +1,186 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction, and
10
+ distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by the
13
+ copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other
16
+ entities that control, are controlled by, or are under common control with
17
+ that entity. For the purposes of this definition, "control" means (i) the
18
+ power, direct or indirect, to cause the direction or management of such
19
+ entity, whether by contract or otherwise, or (ii) ownership of fifty percent
20
+ (50%) or more of the outstanding shares, or (iii) beneficial ownership of
21
+ such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
24
+ permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation source,
28
+ and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical transformation
31
+ or translation of a Source form, including but not limited to compiled
32
+ object code, generated documentation, and conversions to other media types.
33
+
34
+ "Work" shall mean the work of authorship, whether in Source or Object form,
35
+ made available under the License, as indicated by a copyright notice that is
36
+ included in or attached to the work (an example is provided in the Appendix
37
+ below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object form,
40
+ that is based on (or derived from) the Work and for which the editorial
41
+ revisions, annotations, elaborations, or other modifications represent, as a
42
+ whole, an original work of authorship. For the purposes of this License,
43
+ Derivative Works shall not include works that remain separable from, or
44
+ merely link (or bind by name) to the interfaces of, the Work and Derivative
45
+ Works thereof.
46
+
47
+ "Contribution" shall mean any work of authorship, including the original
48
+ version of the Work and any modifications or additions to that Work or
49
+ Derivative Works thereof, that is intentionally submitted to Licensor for
50
+ inclusion in the Work by the copyright owner or by an individual or Legal
51
+ Entity authorized to submit on behalf of the copyright owner. For the
52
+ purposes of this definition, "submitted" means any form of electronic,
53
+ verbal, or written communication sent to the Licensor or its representatives,
54
+ including but not limited to communication on electronic mailing lists, source
55
+ code control systems, and issue tracking systems that are managed by, or on
56
+ behalf of, the Licensor for the purpose of discussing and improving the Work,
57
+ but excluding communication that is conspicuously marked or otherwise
58
+ designated in writing by the copyright owner as "Not a Contribution."
59
+
60
+ "Contributor" shall mean Licensor and any individual or Legal Entity on
61
+ behalf of whom a Contribution has been received by Licensor and subsequently
62
+ incorporated within the Work.
63
+
64
+ 2. Grant of Copyright License. Subject to the terms and conditions of this
65
+ License, each Contributor hereby grants to You a perpetual, worldwide,
66
+ non-exclusive, no-charge, royalty-free, irrevocable copyright license to
67
+ reproduce, prepare Derivative Works of, publicly display, publicly perform,
68
+ sublicense, and distribute the Work and such Derivative Works in Source or
69
+ Object form.
70
+
71
+ 3. Grant of Patent License. Subject to the terms and conditions of this
72
+ License, each Contributor hereby grants to You a perpetual, worldwide,
73
+ non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
74
+ section) patent license to make, have made, use, offer to sell, sell, import,
75
+ and otherwise transfer the Work, where such license applies only to those
76
+ patent claims licensable by such Contributor that are necessarily infringed
77
+ by their Contribution(s) alone or by combination of their Contribution(s)
78
+ with the Work to which such Contribution(s) was submitted. If You institute
79
+ patent litigation against any entity (including a cross-claim or counterclaim
80
+ in a lawsuit) alleging that the Work or a Contribution incorporated within
81
+ the Work constitutes direct or contributory patent infringement, then any
82
+ patent licenses granted to You under this License for that Work shall
83
+ terminate as of the date such litigation is filed.
84
+
85
+ 4. Redistribution. You may reproduce and distribute copies of the Work or
86
+ Derivative Works thereof in any medium, with or without modifications, and in
87
+ Source or Object form, provided that You meet the following conditions:
88
+
89
+ (a) You must give any other recipients of the Work or Derivative Works a copy
90
+ of this License; and
91
+
92
+ (b) You must cause any modified files to carry prominent notices stating that
93
+ You changed the files; and
94
+
95
+ (c) You must retain, in the Source form of any Derivative Works that You
96
+ distribute, all copyright, patent, trademark, and attribution notices from the
97
+ Source form of the Work, excluding those notices that do not pertain to any
98
+ part of the Derivative Works; and
99
+
100
+ (d) If the Work includes a "NOTICE" text file as part of its distribution,
101
+ then any Derivative Works that You distribute must include a readable copy of
102
+ the attribution notices contained within such NOTICE file, excluding those
103
+ notices that do not pertain to any part of the Derivative Works, in at least
104
+ one of the following places: within a NOTICE text file distributed as part of
105
+ the Derivative Works; within the Source form or documentation, if provided
106
+ along with the Derivative Works; or, within a display generated by the
107
+ Derivative Works, if and wherever such third-party notices normally appear.
108
+ The contents of the NOTICE file are for informational purposes only and do
109
+ not modify the License. You may add Your own attribution notices within
110
+ Derivative Works that You distribute, alongside or as an addendum to the
111
+ NOTICE text from the Work, provided that such additional attribution notices
112
+ cannot be construed as modifying the License.
113
+
114
+ You may add Your own copyright statement to Your modifications and may
115
+ provide additional or different license terms and conditions for use,
116
+ reproduction, or distribution of Your modifications, or for any such
117
+ Derivative Works as a whole, provided Your use, reproduction, and distribution
118
+ of the Work otherwise complies with the conditions stated in this License.
119
+
120
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any
121
+ Contribution intentionally submitted for inclusion in the Work by You to the
122
+ Licensor shall be under the terms and conditions of this License, without any
123
+ additional terms or conditions. Notwithstanding the above, nothing herein
124
+ shall supersede or modify the terms of any separate license agreement you may
125
+ have executed with Licensor regarding such Contributions.
126
+
127
+ 6. Trademarks. This License does not grant permission to use the trade names,
128
+ trademarks, service marks, or product names of the Licensor, except as
129
+ required for reasonable and customary use in describing the origin of the Work
130
+ and reproducing the content of the NOTICE file.
131
+
132
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
133
+ writing, Licensor provides the Work (and each Contributor provides its
134
+ Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
135
+ KIND, either express or implied, including, without limitation, any warranties
136
+ or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
137
+ PARTICULAR PURPOSE. You are solely responsible for determining the
138
+ appropriateness of using or redistributing the Work and assume any risks
139
+ associated with Your exercise of permissions under this License.
140
+
141
+ 8. Limitation of Liability. In no event and under no legal theory, whether in
142
+ tort (including negligence), contract, or otherwise, unless required by
143
+ applicable law (such as deliberate and grossly negligent acts) or agreed to in
144
+ writing, shall any Contributor be liable to You for damages, including any
145
+ direct, indirect, special, incidental, or consequential damages of any
146
+ character arising as a result of this License or out of the use or inability
147
+ to use the Work (including but not limited to damages for loss of goodwill,
148
+ work stoppage, computer failure or malfunction, or any and all other
149
+ commercial damages or losses), even if such Contributor has been advised of
150
+ the possibility of such damages.
151
+
152
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work
153
+ or Derivative Works thereof, You may choose to offer, and charge a fee for,
154
+ acceptance of support, warranty, indemnity, or other liability obligations
155
+ and/or rights consistent with this License. However, in accepting such
156
+ obligations, You may act only on Your own behalf and on Your sole
157
+ responsibility, not on behalf of any other Contributor, and only if You agree
158
+ to indemnify, defend, and hold each Contributor harmless for any liability
159
+ incurred by, or claims asserted against, such Contributor by reason of your
160
+ accepting any such warranty or additional liability.
161
+
162
+ END OF TERMS AND CONDITIONS
163
+
164
+ APPENDIX: How to apply the Apache License to your work.
165
+
166
+ To apply the Apache License to your work, attach the following boilerplate
167
+ notice, with the fields enclosed by brackets "[]" replaced with your own
168
+ identifying information. (Don't include the brackets!) The text should be
169
+ enclosed in the appropriate comment syntax for the file format. We also
170
+ recommend that a file or class name and description of purpose be included on
171
+ the same "printed page" as the copyright notice for easier identification
172
+ within third-party archives.
173
+
174
+ Copyright [yyyy] [name of copyright owner]
175
+
176
+ Licensed under the Apache License, Version 2.0 (the "License");
177
+ you may not use this file except in compliance with the License.
178
+ You may obtain a copy of the License at
179
+
180
+ http://www.apache.org/licenses/LICENSE-2.0
181
+
182
+ Unless required by applicable law or agreed to in writing, software
183
+ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
184
+ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
185
+ See the License for the specific language governing permissions and
186
+ limitations under the License.
finctx-0.3.0/PKG-INFO ADDED
@@ -0,0 +1,197 @@
1
+ Metadata-Version: 2.5
2
+ Name: finctx
3
+ Version: 0.3.0
4
+ Summary: Finctx SDK for the Finctx Data API
5
+ Project-URL: Homepage, https://www.ultrastiching.com
6
+ Author: Finctx
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Typing :: Typed
13
+ Requires-Python: >=3.9
14
+ Requires-Dist: requests<3,>=2.31
15
+ Requires-Dist: urllib3<2,>=1.26.20; python_version < '3.10'
16
+ Requires-Dist: websocket-client<1.9,>=1.8
17
+ Provides-Extra: arrow
18
+ Requires-Dist: pyarrow>=14; extra == 'arrow'
19
+ Provides-Extra: marketdb
20
+ Requires-Dist: duckdb<1.6,>=1.4.1; extra == 'marketdb'
21
+ Provides-Extra: test
22
+ Requires-Dist: duckdb<1.6,>=1.4.1; extra == 'test'
23
+ Requires-Dist: pyarrow>=14; extra == 'test'
24
+ Requires-Dist: pytest<10,>=8; extra == 'test'
25
+ Description-Content-Type: text/markdown
26
+
27
+ # Finctx Python SDK
28
+
29
+ 在 Python 中查询股票、基金、财务和宏观数据,读取主题跟踪资讯,或订阅实时快讯。
30
+
31
+ ## 1. 安装
32
+
33
+ 需要 Python 3.9 或更高版本。安装下载的 SDK 包:
34
+
35
+ ```bash
36
+ python -m pip install ./finctx-0.3.0-py3-none-any.whl
37
+ ```
38
+
39
+ 也可以直接安装源码包:
40
+
41
+ ```bash
42
+ python -m pip install ./finctx-0.3.0.tar.gz
43
+ ```
44
+
45
+ 需要处理 Arrow 数据或使用本地数据库时,再安装相应扩展:
46
+
47
+ ```bash
48
+ python -m pip install "finctx[arrow]==0.3.0" # Arrow 数据转换
49
+ python -m pip install "finctx[marketdb]==0.3.0" # 本地数据库
50
+ ```
51
+
52
+ ## 2. 配置 API Key
53
+
54
+ 在 [Finctx 网站](https://www.ultrastiching.com)创建 API Key,然后在运行 Python 的环境中设置:
55
+
56
+ ```bash
57
+ export FINCTX_API_KEY="<你的 API Key>"
58
+ ```
59
+
60
+ Windows PowerShell 使用 `$env:FINCTX_API_KEY = "<你的 API Key>"`。不要将真实 Key 写入代码或提交到 Git。
61
+
62
+ 也可以将 `FINCTX_API_KEY=<你的 API Key>` 保存到 `~/.finctx/credentials.env`,并将文件权限设为仅当前用户可读写(Linux/macOS 使用 `chmod 600 ~/.finctx/credentials.env`)。SDK 按“传入的 `api_key` → 环境变量 → 用户凭据文件”的顺序读取 Key,不会自动加载项目的 `.env`。
63
+
64
+ ## 3. 第一次查询
65
+
66
+ 下面查询证券简称,并把结果提取为 Python 字典列表:
67
+
68
+ ```python
69
+ from finctx import FinctxClient
70
+
71
+ with FinctxClient() as client:
72
+ result = client.get_s_stockname(code=["600745.SH"])
73
+ rows = client.rows(result)
74
+ print(rows)
75
+ ```
76
+
77
+ 接口调用需要相应权限,并按账号的额度和计费规则使用。`rows()` 适用于包含 `data_frame` 的表格结果;如果响应格式不符,会抛出错误。
78
+
79
+ ## 查找接口和参数
80
+
81
+ 这些查询在本地执行,无需 API Key:
82
+
83
+ ```python
84
+ from finctx import FinctxClient
85
+
86
+ print(FinctxClient.endpoints()["daily"]["parameters"])
87
+ print(FinctxClient.fields("daily", search="收盘"))
88
+ help(FinctxClient.daily)
89
+ ```
90
+
91
+ `endpoints()` 列出可调用的接口及其参数,`fields()` 查找返回字段,`help()` 查看方法签名。调用业务方法时使用关键字参数;具体返回内容取决于所选接口。
92
+
93
+ ## 处理 Arrow 数据
94
+
95
+ 安装 `arrow` 扩展后,可以把 Arrow 结果转换成表格:
96
+
97
+ ```python
98
+ from finctx import FinctxClient
99
+
100
+ with FinctxClient() as client:
101
+ raw = client.arrow(
102
+ handler="get_s_stockname",
103
+ params={"code": ["600745.SH"]},
104
+ fields=["code", "sename"],
105
+ )
106
+ table = client.arrow_to_table(raw)
107
+ print(table.to_pylist())
108
+ ```
109
+
110
+ Arrow 接口返回原始 bytes,转换由调用方显式执行。
111
+
112
+ ## 读取主题跟踪资讯
113
+
114
+ 读取关注范围的内容、搜索资讯,或查看关注的主题:
115
+
116
+ ```python
117
+ from finctx import FinctxClient
118
+
119
+ with FinctxClient() as client:
120
+ feed = client.get_tracer_feed(limit=20)
121
+ print(feed["items"])
122
+
123
+ page = client.search_tracer_content(q="美债", scope="following", limit=20)
124
+ print(page["items"])
125
+
126
+ topics = client.list_tracer_topics()
127
+ print(topics)
128
+ ```
129
+
130
+ 资讯结果使用 `items`,直接读取,不调用 `rows()`。新闻搜索返回 `has_more=True` 时,下一页传入 `next_cursor`,并保留相同筛选条件及返回的时间范围;Python 参数 `from_`、`to` 分别使用 `window["from"]`、`window["to"]`。
131
+
132
+ 主题创建使用 `create_tracer_topic(query=...)`;关注和取消关注使用 `follow_tracer_topic(id=...)`、`unfollow_tracer_topic(id=...)`,主题 ID 从主题列表取得。创建回执表示任务已受理,不代表内容已生成完成。
133
+
134
+ ## 订阅实时快讯
135
+
136
+ ```python
137
+ from finctx import FinctxClient
138
+
139
+ with FinctxClient() as client:
140
+ with client.ws_newsflash(limit=10) as stream:
141
+ print(stream.receive_json())
142
+ ```
143
+
144
+ SDK 不自动重连。持续订阅时,由应用保存已处理消息的游标并处理断线。主题跟踪的 SSE 推送需使用支持流式读取的 HTTP 客户端,SDK 的 `request()` 不支持持续读取 SSE。
145
+
146
+ ## 使用本地数据库
147
+
148
+ 安装 `marketdb` 扩展后,可以将数据保存到本地 DuckDB 文件,并用 SQL 查询:
149
+
150
+ ```python
151
+ from finctx.marketdb import DATASETS, MarketDB
152
+
153
+ db = MarketDB()
154
+ db.init()
155
+ print(sorted(DATASETS)) # 查看支持同步的数据集
156
+ print(db.status()) # 查看本地数据和同步记录
157
+ ```
158
+
159
+ 通过 `db.sync(数据集名称, **查询参数)` 下载数据,`db.sql(SQL)` 执行只读查询,`db.export(表名, 文件路径)` 导出 CSV 或 Parquet。
160
+
161
+ 同步后检查返回的 `status`:`complete` 表示本次查询范围已完整写入,`incomplete` 表示未确认完整,不写入数据。单次同步上限为 100,000 行或约 64 MiB;超限时缩小查询范围。`validate()` 检查本地数据库,不验证远端是否还有更多历史数据。
162
+
163
+ ## 调整地址和超时
164
+
165
+ 通常只需配置 API Key。需要覆盖默认设置时使用:
166
+
167
+ | 设置 | 默认值 | 配置方式 |
168
+ | --- | --- | --- |
169
+ | HTTP 服务地址 | `https://api.ultrastiching.com` | `base_url=` 或 `FINCTX_BASE_URL` |
170
+ | HTTP 超时 | 30 秒 | `timeout=` |
171
+ | WebSocket 服务地址 | `wss://ws.ultrastiching.com` | `websocket_base_url=` |
172
+ | WebSocket 超时 | 30 秒 | `websocket_timeout=` |
173
+ | 本地数据库路径 | `~/.finctx/data/marketdb.duckdb` | `MarketDB(path=...)` 或 `FINCTX_DB` |
174
+
175
+ 显式参数优先于环境变量。服务地址需包含协议,如 `https://api.example.com`,可带端口,不附带接口路径、查询参数或片段;HTTP 地址设置不会同时修改 WebSocket 地址。
176
+
177
+ ## 处理错误
178
+
179
+ ```python
180
+ from finctx import FinctxApiError, FinctxClient, FinctxTransportError
181
+
182
+ try:
183
+ with FinctxClient() as client:
184
+ result = client.get_s_stockname(code=["600745.SH"])
185
+ except FinctxApiError as error:
186
+ print(error.status_code, error.error_code, error.request_id)
187
+ except FinctxTransportError as error:
188
+ print(error.cause)
189
+ ```
190
+
191
+ `FinctxApiError` 提供 HTTP 状态、服务端错误码、请求编号和原始响应;`FinctxTransportError` 提供网络错误原因。响应无法解析为 JSON 时抛出 `FinctxResponseError`,表格结果格式不符时抛出 `FinctxResultError`。根据具体错误处理,SDK 默认不自动重试。
192
+
193
+ ## 从旧版升级
194
+
195
+ 0.3.0 的安装包与导入名统一为 `finctx`,客户端为 `FinctxClient`,异常类使用 `Finctx` 前缀。升级时更新导入、异常捕获和环境变量;新版不自动读取旧配置或旧用户目录。已有数据库可通过 `FINCTX_DB` 或 `MarketDB(path=...)` 继续打开。
196
+
197
+ 许可证:[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)。
finctx-0.3.0/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # Finctx Python SDK
2
+
3
+ 在 Python 中查询股票、基金、财务和宏观数据,读取主题跟踪资讯,或订阅实时快讯。
4
+
5
+ ## 1. 安装
6
+
7
+ 需要 Python 3.9 或更高版本。安装下载的 SDK 包:
8
+
9
+ ```bash
10
+ python -m pip install ./finctx-0.3.0-py3-none-any.whl
11
+ ```
12
+
13
+ 也可以直接安装源码包:
14
+
15
+ ```bash
16
+ python -m pip install ./finctx-0.3.0.tar.gz
17
+ ```
18
+
19
+ 需要处理 Arrow 数据或使用本地数据库时,再安装相应扩展:
20
+
21
+ ```bash
22
+ python -m pip install "finctx[arrow]==0.3.0" # Arrow 数据转换
23
+ python -m pip install "finctx[marketdb]==0.3.0" # 本地数据库
24
+ ```
25
+
26
+ ## 2. 配置 API Key
27
+
28
+ 在 [Finctx 网站](https://www.ultrastiching.com)创建 API Key,然后在运行 Python 的环境中设置:
29
+
30
+ ```bash
31
+ export FINCTX_API_KEY="<你的 API Key>"
32
+ ```
33
+
34
+ Windows PowerShell 使用 `$env:FINCTX_API_KEY = "<你的 API Key>"`。不要将真实 Key 写入代码或提交到 Git。
35
+
36
+ 也可以将 `FINCTX_API_KEY=<你的 API Key>` 保存到 `~/.finctx/credentials.env`,并将文件权限设为仅当前用户可读写(Linux/macOS 使用 `chmod 600 ~/.finctx/credentials.env`)。SDK 按“传入的 `api_key` → 环境变量 → 用户凭据文件”的顺序读取 Key,不会自动加载项目的 `.env`。
37
+
38
+ ## 3. 第一次查询
39
+
40
+ 下面查询证券简称,并把结果提取为 Python 字典列表:
41
+
42
+ ```python
43
+ from finctx import FinctxClient
44
+
45
+ with FinctxClient() as client:
46
+ result = client.get_s_stockname(code=["600745.SH"])
47
+ rows = client.rows(result)
48
+ print(rows)
49
+ ```
50
+
51
+ 接口调用需要相应权限,并按账号的额度和计费规则使用。`rows()` 适用于包含 `data_frame` 的表格结果;如果响应格式不符,会抛出错误。
52
+
53
+ ## 查找接口和参数
54
+
55
+ 这些查询在本地执行,无需 API Key:
56
+
57
+ ```python
58
+ from finctx import FinctxClient
59
+
60
+ print(FinctxClient.endpoints()["daily"]["parameters"])
61
+ print(FinctxClient.fields("daily", search="收盘"))
62
+ help(FinctxClient.daily)
63
+ ```
64
+
65
+ `endpoints()` 列出可调用的接口及其参数,`fields()` 查找返回字段,`help()` 查看方法签名。调用业务方法时使用关键字参数;具体返回内容取决于所选接口。
66
+
67
+ ## 处理 Arrow 数据
68
+
69
+ 安装 `arrow` 扩展后,可以把 Arrow 结果转换成表格:
70
+
71
+ ```python
72
+ from finctx import FinctxClient
73
+
74
+ with FinctxClient() as client:
75
+ raw = client.arrow(
76
+ handler="get_s_stockname",
77
+ params={"code": ["600745.SH"]},
78
+ fields=["code", "sename"],
79
+ )
80
+ table = client.arrow_to_table(raw)
81
+ print(table.to_pylist())
82
+ ```
83
+
84
+ Arrow 接口返回原始 bytes,转换由调用方显式执行。
85
+
86
+ ## 读取主题跟踪资讯
87
+
88
+ 读取关注范围的内容、搜索资讯,或查看关注的主题:
89
+
90
+ ```python
91
+ from finctx import FinctxClient
92
+
93
+ with FinctxClient() as client:
94
+ feed = client.get_tracer_feed(limit=20)
95
+ print(feed["items"])
96
+
97
+ page = client.search_tracer_content(q="美债", scope="following", limit=20)
98
+ print(page["items"])
99
+
100
+ topics = client.list_tracer_topics()
101
+ print(topics)
102
+ ```
103
+
104
+ 资讯结果使用 `items`,直接读取,不调用 `rows()`。新闻搜索返回 `has_more=True` 时,下一页传入 `next_cursor`,并保留相同筛选条件及返回的时间范围;Python 参数 `from_`、`to` 分别使用 `window["from"]`、`window["to"]`。
105
+
106
+ 主题创建使用 `create_tracer_topic(query=...)`;关注和取消关注使用 `follow_tracer_topic(id=...)`、`unfollow_tracer_topic(id=...)`,主题 ID 从主题列表取得。创建回执表示任务已受理,不代表内容已生成完成。
107
+
108
+ ## 订阅实时快讯
109
+
110
+ ```python
111
+ from finctx import FinctxClient
112
+
113
+ with FinctxClient() as client:
114
+ with client.ws_newsflash(limit=10) as stream:
115
+ print(stream.receive_json())
116
+ ```
117
+
118
+ SDK 不自动重连。持续订阅时,由应用保存已处理消息的游标并处理断线。主题跟踪的 SSE 推送需使用支持流式读取的 HTTP 客户端,SDK 的 `request()` 不支持持续读取 SSE。
119
+
120
+ ## 使用本地数据库
121
+
122
+ 安装 `marketdb` 扩展后,可以将数据保存到本地 DuckDB 文件,并用 SQL 查询:
123
+
124
+ ```python
125
+ from finctx.marketdb import DATASETS, MarketDB
126
+
127
+ db = MarketDB()
128
+ db.init()
129
+ print(sorted(DATASETS)) # 查看支持同步的数据集
130
+ print(db.status()) # 查看本地数据和同步记录
131
+ ```
132
+
133
+ 通过 `db.sync(数据集名称, **查询参数)` 下载数据,`db.sql(SQL)` 执行只读查询,`db.export(表名, 文件路径)` 导出 CSV 或 Parquet。
134
+
135
+ 同步后检查返回的 `status`:`complete` 表示本次查询范围已完整写入,`incomplete` 表示未确认完整,不写入数据。单次同步上限为 100,000 行或约 64 MiB;超限时缩小查询范围。`validate()` 检查本地数据库,不验证远端是否还有更多历史数据。
136
+
137
+ ## 调整地址和超时
138
+
139
+ 通常只需配置 API Key。需要覆盖默认设置时使用:
140
+
141
+ | 设置 | 默认值 | 配置方式 |
142
+ | --- | --- | --- |
143
+ | HTTP 服务地址 | `https://api.ultrastiching.com` | `base_url=` 或 `FINCTX_BASE_URL` |
144
+ | HTTP 超时 | 30 秒 | `timeout=` |
145
+ | WebSocket 服务地址 | `wss://ws.ultrastiching.com` | `websocket_base_url=` |
146
+ | WebSocket 超时 | 30 秒 | `websocket_timeout=` |
147
+ | 本地数据库路径 | `~/.finctx/data/marketdb.duckdb` | `MarketDB(path=...)` 或 `FINCTX_DB` |
148
+
149
+ 显式参数优先于环境变量。服务地址需包含协议,如 `https://api.example.com`,可带端口,不附带接口路径、查询参数或片段;HTTP 地址设置不会同时修改 WebSocket 地址。
150
+
151
+ ## 处理错误
152
+
153
+ ```python
154
+ from finctx import FinctxApiError, FinctxClient, FinctxTransportError
155
+
156
+ try:
157
+ with FinctxClient() as client:
158
+ result = client.get_s_stockname(code=["600745.SH"])
159
+ except FinctxApiError as error:
160
+ print(error.status_code, error.error_code, error.request_id)
161
+ except FinctxTransportError as error:
162
+ print(error.cause)
163
+ ```
164
+
165
+ `FinctxApiError` 提供 HTTP 状态、服务端错误码、请求编号和原始响应;`FinctxTransportError` 提供网络错误原因。响应无法解析为 JSON 时抛出 `FinctxResponseError`,表格结果格式不符时抛出 `FinctxResultError`。根据具体错误处理,SDK 默认不自动重试。
166
+
167
+ ## 从旧版升级
168
+
169
+ 0.3.0 的安装包与导入名统一为 `finctx`,客户端为 `FinctxClient`,异常类使用 `Finctx` 前缀。升级时更新导入、异常捕获和环境变量;新版不自动读取旧配置或旧用户目录。已有数据库可通过 `FINCTX_DB` 或 `MarketDB(path=...)` 继续打开。
170
+
171
+ 许可证:[Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)。
finctx-0.3.0/demo.py ADDED
@@ -0,0 +1,40 @@
1
+ """Finctx Python SDK 源码交付调用示例。"""
2
+
3
+ from finctx import (
4
+ FinctxApiError,
5
+ FinctxClient,
6
+ FinctxResponseError,
7
+ FinctxTransportError,
8
+ )
9
+
10
+
11
+ # 使用时替换为实际 API 地址和凭证;不要将真实 API Key 提交到 Git。
12
+ base_url = "https://api.ultrastiching.com"
13
+ api_key = "replace-with-your-api-key"
14
+
15
+
16
+ def main() -> None:
17
+ try:
18
+ with FinctxClient(
19
+ base_url=base_url,
20
+ api_key=api_key,
21
+ ) as client:
22
+ result = client.get_s_majorbiz(
23
+ code=["600745", "603986"],
24
+ trade_date=["20240705", "20240704"],
25
+ )
26
+ print(result)
27
+ except FinctxApiError as error:
28
+ print(
29
+ f"API 调用失败:status={error.status_code}, "
30
+ f"code={error.error_code}, message={error.message}, "
31
+ f"request_id={error.request_id}"
32
+ )
33
+ except FinctxTransportError as error:
34
+ print(f"网络调用失败:{error}")
35
+ except FinctxResponseError as error:
36
+ print(f"响应不是合法 JSON:request_id={error.request_id}")
37
+
38
+
39
+ if __name__ == "__main__":
40
+ main()
@@ -0,0 +1,42 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.27"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "finctx"
7
+ dynamic = ["version"]
8
+ description = "Finctx SDK for the Finctx Data API"
9
+ readme = "README.md"
10
+ license = "Apache-2.0"
11
+ license-files = ["LICENSE"]
12
+ requires-python = ">=3.9"
13
+ authors = [{ name = "Finctx" }]
14
+ dependencies = [
15
+ "requests>=2.31,<3",
16
+ "urllib3>=1.26.20,<2; python_version < '3.10'",
17
+ "websocket-client>=1.8,<1.9",
18
+ ]
19
+ classifiers = [
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3 :: Only",
22
+ "Programming Language :: Python :: 3.9",
23
+ "Typing :: Typed",
24
+ ]
25
+
26
+ [project.urls]
27
+ Homepage = "https://www.ultrastiching.com"
28
+
29
+ [project.optional-dependencies]
30
+ arrow = ["pyarrow>=14"]
31
+ marketdb = ["duckdb>=1.4.1,<1.6"]
32
+ test = ["pytest>=8,<10", "duckdb>=1.4.1,<1.6", "pyarrow>=14"]
33
+
34
+ [tool.hatch.build.targets.wheel]
35
+ packages = ["src/finctx"]
36
+
37
+ [tool.hatch.version]
38
+ path = "src/finctx/_version.py"
39
+
40
+ [tool.pytest.ini_options]
41
+ testpaths = ["tests"]
42
+ pythonpath = ["src"]
@@ -0,0 +1,24 @@
1
+ """Public interface for the Finctx Data API Python SDK."""
2
+
3
+ from ._version import __version__
4
+ from .client import FinctxClient, FinctxWebSocket
5
+ from .exceptions import (
6
+ FinctxApiError,
7
+ FinctxError,
8
+ FinctxResponseError,
9
+ FinctxResultError,
10
+ FinctxTransportError,
11
+ FinctxWebSocketMessageError,
12
+ )
13
+
14
+ __all__ = [
15
+ "FinctxApiError",
16
+ "FinctxClient",
17
+ "FinctxError",
18
+ "FinctxResponseError",
19
+ "FinctxResultError",
20
+ "FinctxTransportError",
21
+ "FinctxWebSocket",
22
+ "FinctxWebSocketMessageError",
23
+ "__version__",
24
+ ]
@@ -0,0 +1,3 @@
1
+ """Single source of truth for the Finctx Python SDK version."""
2
+
3
+ __version__ = "0.3.0"