fix: 腾讯历史K线接口已废弃,改进错误处理优雅降级到Tiingo
This commit is contained in:
parent
79105c339d
commit
9f82ad0a81
@ -102,34 +102,46 @@ public class TencentMarketService : ITencentMarketService
|
||||
var responseBytes = await _httpClient.GetByteArrayAsync(url);
|
||||
var response = Encoding.GetEncoding("GBK").GetString(responseBytes);
|
||||
|
||||
if (string.IsNullOrEmpty(response) || !response.Contains("kline"))
|
||||
if (string.IsNullOrEmpty(response))
|
||||
{
|
||||
_logger.LogWarning("腾讯财经返回无效数据: {Symbol}", symbol);
|
||||
return new List<MarketDataResponse>();
|
||||
throw new Exception($"腾讯财经历史数据接口返回空数据,标的: {symbol}");
|
||||
}
|
||||
|
||||
// 解析JSON
|
||||
var jsonStart = response.IndexOf('{');
|
||||
if (jsonStart < 0) return new List<MarketDataResponse>();
|
||||
if (jsonStart < 0)
|
||||
{
|
||||
throw new Exception($"腾讯财经历史数据接口返回非JSON格式,标的: {symbol}");
|
||||
}
|
||||
|
||||
var jsonStr = response.Substring(jsonStart);
|
||||
var jsonDoc = JsonDocument.Parse(jsonStr);
|
||||
using var jsonDoc = JsonDocument.Parse(jsonStr);
|
||||
var root = jsonDoc.RootElement;
|
||||
|
||||
// 检查API错误响应
|
||||
if (root.TryGetProperty("code", out var codeEl) && codeEl.GetInt32() != 0)
|
||||
{
|
||||
var errMsg = root.TryGetProperty("msg", out var msgEl) ? msgEl.GetString() : "未知错误";
|
||||
throw new Exception($"腾讯财经历史数据接口错误: {errMsg},标的: {symbol}");
|
||||
}
|
||||
|
||||
// 检查 data 字段类型(错误响应时 data 是空数组 [])
|
||||
if (!root.TryGetProperty("data", out var data) || data.ValueKind != JsonValueKind.Object)
|
||||
{
|
||||
throw new Exception($"腾讯财经历史数据接口返回无效数据结构,标的: {symbol}");
|
||||
}
|
||||
|
||||
// 数据路径: data -> us{symbol} -> qfq -> {klineType}
|
||||
var dataPath = $"us{symbol.ToUpper()}";
|
||||
if (!root.TryGetProperty("data", out var data) ||
|
||||
!data.TryGetProperty(dataPath, out var stockData) ||
|
||||
if (!data.TryGetProperty(dataPath, out var stockData) ||
|
||||
!stockData.TryGetProperty("qfq", out var qfq))
|
||||
{
|
||||
_logger.LogWarning("腾讯财经数据结构异常: {Symbol}", symbol);
|
||||
return new List<MarketDataResponse>();
|
||||
throw new Exception($"腾讯财经历史数据接口无该标的K线数据,标的: {symbol}");
|
||||
}
|
||||
|
||||
if (!qfq.TryGetProperty(klineType, out var klineData) || klineData.ValueKind != JsonValueKind.Array)
|
||||
{
|
||||
_logger.LogWarning("腾讯财经无K线数据: {Symbol}", symbol);
|
||||
return new List<MarketDataResponse>();
|
||||
throw new Exception($"腾讯财经历史数据接口K线数据格式异常,标的: {symbol}");
|
||||
}
|
||||
|
||||
var result = new List<MarketDataResponse>();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user