fix(api,codec): 修复 BUG-1 分时采样走错命令 + BUG-2 财务文件名纠正
BUG-1: A股 get_minutes(sampling=True) 原走 MacChartSamplingCmd(0x254D) 这是 EX 协议命令,7709 主机无响应(3×8s 超时≈24s)。 修复:A股采样由 MacTickChartCmd(0x122D) 分时数据派生, EX 市场(hk)仍走 0x254D。4 个 A股 场景均<0.1s 返回 240 点。 BUG-2: tdxfin/gpcw.txt 索引声明 gpcwYYYYMMDD.zip, 但服务器实际只有 tdxfin/gpcwYYYYMMDD.dat(未压缩,~13MB), .zip 请求返回 0-1 字节空壳。 修复:_fetch_financial_dat() 自动候选回退 tdxfin/*.zip → tdxfin/*.dat → 原始文件名,加 looks_like_gpcw_dat 校验。 实测 gpcw20260630.zip→5568 条,gpcw20260331.zip→5547 条。 新增 codec/financial.py: looks_like_gpcw_dat, extract_gpcw_dat(含 zip/未压缩双通道)。 同步更新 README/API_REFERENCE/docs/cli/cookbook/CHANGELOG 共 6 处文档。 tests: +18(总 182),ruff 无新增 findings,MkDocs strict 通过。
This commit is contained in:
1 parent
b78d0fabf5
commit
8951ec2678
10 files changed
+391
-39
No files matched your search
+68
-20
@@ -22,6 +22,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
import warnings
|
||||
from datetime import date as _date, datetime
|
||||
from pathlib import Path
|
||||
@@ -61,7 +62,7 @@ from .models import (
|
||||
)
|
||||
from .pool.ex_pool import ExPool
|
||||
from .pool.health import HealthEngine
|
||||
from .pool.pool import DynamicPool
|
||||
from .pool.pool import DynamicPool, PoolTimeoutError
|
||||
from .protocol.commands import (
|
||||
GetFinanceInfoCmd,
|
||||
GetIndexBarsCmd,
|
||||
@@ -75,6 +76,7 @@ from .protocol.commands import (
|
||||
MacKlineCmd,
|
||||
MacTransactionCmd,
|
||||
MAX_QUOTES,
|
||||
TdxDecodeError,
|
||||
)
|
||||
from .protocol.commands.ex_ext import (
|
||||
GetExHistoryTransactionDataCmd,
|
||||
@@ -895,8 +897,9 @@ class TdxData:
|
||||
|
||||
参数:
|
||||
date: ``None``=今天;``YYYYMMDD``=历史某日(A股支持历史分时)。
|
||||
sampling: ``True`` 取缩略采样(约240个价格点,无时间;A股 0x254D,
|
||||
扩展市场同命令),``vol``/``avg_price`` 为 0,点位序号在 ``fields["i"]``。
|
||||
sampling: ``True`` 取缩略采样(约 240 个价格点,无时间),``vol``/
|
||||
``avg_price`` 为 0,点位序号在 ``fields["i"]``。A股由分时数据派生,
|
||||
扩展市场走 0x254D。
|
||||
|
||||
返回:
|
||||
``list[MinuteBar]``,按入参标的顺序拼接,每条带 ``symbol``/``market``/
|
||||
@@ -920,8 +923,17 @@ class TdxData:
|
||||
|
||||
async def fetch() -> bytes:
|
||||
if sampling:
|
||||
run = self._execute_ex if sym.is_ex else self._execute_mac
|
||||
prices = await run(MacChartSamplingCmd(sym.market, sym.code))
|
||||
if sym.is_ex:
|
||||
prices = await self._execute_ex(
|
||||
MacChartSamplingCmd(sym.market, sym.code))
|
||||
else:
|
||||
# ⚠️ 不能用 MacChartSamplingCmd(0x254D):那是 **EX 协议**命令,
|
||||
# A股/标准通道的 7709 主机对它一律不响应(实测 3 台全 8s 超时,
|
||||
# 叠加连接池 3 次重试 ≈24s 才报错)。改由分时(0x122D)派生价格点:
|
||||
# 分时本身就是 240 点,语义与「缩略采样」一致且天然可用。
|
||||
ticks = await self._execute_mac(
|
||||
MacTickChartCmd(sym.market, sym.code, ymd))
|
||||
prices = [b.price for b in ticks]
|
||||
bars = [MinuteBar(price=float(p), vol=0.0, symbol=sym.text,
|
||||
market=sym.market, code=sym.code, date=ymd,
|
||||
fields={"i": float(i)})
|
||||
@@ -1273,28 +1285,60 @@ class TdxData:
|
||||
return parse_financial_file_list(await self.get_file("tdxfin/gpcw.txt"))
|
||||
|
||||
async def get_financial_records_parsed(self, filename: str) -> list[FinancialRecord]:
|
||||
"""财务 zip(``tdxfin/gpcw*.zip``)解压解析 -> ``list[FinancialRecord]``。
|
||||
"""财报数据解析 -> ``list[FinancialRecord]``。
|
||||
|
||||
可直接把 :meth:`get_financial_file_infos` 条目里的 ``filename`` 原样传进来:
|
||||
内部会自动纠正「索引名与服务器实际文件名不一致」的问题(索引写
|
||||
``gpcwYYYYMMDD.zip`` 且不带 ``tdxfin/`` 前缀,服务器实际只提供未压缩的
|
||||
``tdxfin/gpcwYYYYMMDD.dat``),并兼容 zip 包与未压缩 dat 两种形态。
|
||||
|
||||
报告期优先取文件名中的 8 位日期,缺失时回退到 .dat 头部内嵌值。
|
||||
"""
|
||||
import io
|
||||
import re
|
||||
import zipfile
|
||||
|
||||
from .codec.financial import parse_financial_dat
|
||||
|
||||
zip_data = await self.get_file(filename)
|
||||
if not zip_data:
|
||||
dat_data = await self._fetch_financial_dat(filename)
|
||||
if not dat_data:
|
||||
return []
|
||||
with zipfile.ZipFile(io.BytesIO(zip_data)) as zf:
|
||||
dat_names = [n for n in zf.namelist() if n.endswith(".dat")]
|
||||
if not dat_names:
|
||||
return []
|
||||
dat_data = zf.read(dat_names[0])
|
||||
match = re.search(r"(\d{8})", filename)
|
||||
match = re.search(r"(\d{8})", str(filename))
|
||||
report_date = int(match.group(1)) if match else 0
|
||||
return parse_financial_dat(dat_data, report_date)
|
||||
|
||||
async def _fetch_financial_dat(self, filename: str) -> bytes:
|
||||
"""[内部] 取回 gpcw 财报 ``.dat`` 原始字节,自动纠正服务器文件名差异。
|
||||
|
||||
``tdxfin/gpcw.txt`` 索引声明的名字(``gpcwYYYYMMDD.zip``、无目录前缀)与服务器
|
||||
实际提供的文件(``tdxfin/gpcwYYYYMMDD.dat``、未压缩)不一致,且对不存在的名字
|
||||
服务器**不报错、只回 0~1 字节空壳**。这里按候选名逐个尝试,并校验取回的字节
|
||||
是否真的是 ``.dat``,返回第一个有效结果;全部失败返回 ``b""``。
|
||||
|
||||
候选顺序:索引声明的 ``.zip`` → 服务器实际的 ``.dat`` → 调用方原样名字(兜底)。
|
||||
"""
|
||||
from .codec.financial import extract_gpcw_dat
|
||||
|
||||
raw = str(filename).strip().replace("\\", "/")
|
||||
base = raw.rsplit("/", 1)[-1]
|
||||
stem = re.sub(r"\.(?:zip|dat)$", "", base, flags=re.IGNORECASE)
|
||||
candidates = list(dict.fromkeys([
|
||||
f"tdxfin/{stem}.zip",
|
||||
f"tdxfin/{stem}.dat",
|
||||
base,
|
||||
]))
|
||||
for name in candidates:
|
||||
try:
|
||||
blob = await self.get_file(name)
|
||||
except (ConnectionError, OSError, TimeoutError,
|
||||
TdxDecodeError, ValueError, PoolTimeoutError) as exc:
|
||||
# 单个候选取回失败就换下一个(服务器对不存在的名字不一定报错)
|
||||
log.debug("财报候选 %s 取回失败: %s", name, str(exc)[:80])
|
||||
continue
|
||||
dat = extract_gpcw_dat(blob)
|
||||
if dat:
|
||||
log.debug("财报文件命中: %s(%d 字节)", name, len(dat))
|
||||
return dat
|
||||
log.debug("财报候选 %s 非有效 gpcw 数据(%d 字节),继续尝试",
|
||||
name, len(blob))
|
||||
return b""
|
||||
|
||||
# ================================================================== #
|
||||
# 内部执行(连接池 + 失败换连接 + 分页)
|
||||
# ================================================================== #
|
||||
@@ -1553,9 +1597,13 @@ class TdxData:
|
||||
return [l.strip() for l in text.splitlines() if l.strip()]
|
||||
|
||||
async def get_financial_records(self, filename: str) -> bytes:
|
||||
"""已废弃 -> :meth:`get_financial_records_parsed`。"""
|
||||
"""已废弃 -> :meth:`get_financial_records_parsed`。
|
||||
|
||||
返回 gpcw 财报 ``.dat`` 原始字节(服务器给未压缩 ``.dat`` 时自动回退,
|
||||
不再像旧实现那样静默返回 0~1 字节空壳);取不到时返回 ``b""``。
|
||||
"""
|
||||
_deprecated("get_financial_records", "get_financial_records_parsed")
|
||||
return await self.get_file(filename)
|
||||
return await self._fetch_financial_dat(filename)
|
||||
|
||||
async def get_company_info_category(self, market, code) -> list:
|
||||
"""已废弃 -> :meth:`get_f10`。"""
|
||||
|
||||
@@ -17,13 +17,19 @@
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import struct
|
||||
import zipfile
|
||||
|
||||
from ..models import FinancialFileInfo, FinancialRecord
|
||||
|
||||
_HEADER_FMT = "<1hI1H3L"
|
||||
_INDEX_FMT = "<6sBL"
|
||||
|
||||
_HEADER_SIZE = struct.calcsize(_HEADER_FMT)
|
||||
_INDEX_SIZE = struct.calcsize(_INDEX_FMT)
|
||||
_ZIP_MAGIC = b"PK\x03\x04"
|
||||
|
||||
|
||||
def parse_financial_file_list(data: bytes) -> list[FinancialFileInfo]:
|
||||
"""解析 tdxfin/gpcw.txt 内容(每行 ``filename,md5hash,filesize``)。"""
|
||||
@@ -46,8 +52,58 @@ def parse_financial_file_list(data: bytes) -> list[FinancialFileInfo]:
|
||||
return out
|
||||
|
||||
|
||||
def looks_like_gpcw_dat(data: bytes) -> bool:
|
||||
"""粗略判定字节流是否为 gpcw ``.dat``(校验 20 字节 header 的自洽性)。
|
||||
|
||||
用于把「真的取到了财报数据」与「服务器对不存在的文件名回的 0/1 字节空壳」区分开:
|
||||
空壳要么长度不足、要么 header 全零,一律判否。
|
||||
|
||||
Args:
|
||||
data: 待判定的字节流。
|
||||
|
||||
Returns:
|
||||
可疑时返回 True。
|
||||
"""
|
||||
if len(data) < _HEADER_SIZE:
|
||||
return False
|
||||
_unknown, _date, max_count, _u1, report_size, _u2 = struct.unpack(
|
||||
_HEADER_FMT, data[:_HEADER_SIZE]
|
||||
)
|
||||
if max_count <= 0 or report_size <= 0 or report_size % 4 != 0:
|
||||
return False
|
||||
# 索引区必须放得下 max_count 条 11 字节条目
|
||||
return _HEADER_SIZE + max_count * _INDEX_SIZE <= len(data)
|
||||
|
||||
|
||||
def extract_gpcw_dat(data: bytes) -> bytes:
|
||||
"""从「zip 包」或「未压缩 dat」中取出 ``.dat`` 字节;无法识别时返回 ``b""``。
|
||||
|
||||
服务器**实际只提供未压缩的** ``tdxfin/gpcwYYYYMMDD.dat``,而 ``tdxfin/gpcw.txt``
|
||||
索引里写的是 ``gpcwYYYYMMDD.zip``,因此两种形态都要能走通。
|
||||
|
||||
Args:
|
||||
data: zip 包字节或未压缩 ``.dat`` 字节。
|
||||
|
||||
Returns:
|
||||
内层 / 原始的 ``.dat`` 字节;不是有效财报数据时返回 ``b""``。
|
||||
"""
|
||||
if not data:
|
||||
return b""
|
||||
if data.startswith(_ZIP_MAGIC):
|
||||
try:
|
||||
with zipfile.ZipFile(io.BytesIO(data)) as zf:
|
||||
names = [n for n in zf.namelist() if n.lower().endswith(".dat")]
|
||||
if not names:
|
||||
return b""
|
||||
inner = zf.read(names[0])
|
||||
except (zipfile.BadZipFile, OSError, KeyError):
|
||||
return b""
|
||||
return inner if looks_like_gpcw_dat(inner) else b""
|
||||
return data if looks_like_gpcw_dat(data) else b""
|
||||
|
||||
|
||||
def parse_financial_dat(data: bytes, report_date: int = 0) -> list[FinancialRecord]:
|
||||
"""解析 gpcw*.zip 内的 .dat 二进制。
|
||||
"""解析 gpcw ``.dat`` 二进制(zip 内的或服务器直接给的未压缩形式都行)。
|
||||
|
||||
Args:
|
||||
data: .dat 文件完整字节。
|
||||
@@ -56,7 +112,7 @@ def parse_financial_dat(data: bytes, report_date: int = 0) -> list[FinancialReco
|
||||
Returns:
|
||||
FinancialRecord 列表;数据不足或字段非法时返回空列表。
|
||||
"""
|
||||
header_size = struct.calcsize(_HEADER_FMT)
|
||||
header_size = _HEADER_SIZE
|
||||
if len(data) < header_size:
|
||||
return []
|
||||
|
||||
@@ -72,7 +128,7 @@ def parse_financial_dat(data: bytes, report_date: int = 0) -> list[FinancialReco
|
||||
if num_fields <= 0:
|
||||
return []
|
||||
|
||||
index_size = struct.calcsize(_INDEX_FMT)
|
||||
index_size = _INDEX_SIZE
|
||||
report_fmt = f"<{num_fields}f"
|
||||
report_pack_size = struct.calcsize(report_fmt)
|
||||
|
||||
|
||||
Reference in new issue
Block a user