feat(data):为数据获取失败时添加错误反馈并设置超时

- 在获取股票价格、财报、市场资讯、股票资讯和电报资讯失败时,向用户发送错误信息
- 为 GetFinancialReports、SearchStockPriceInfo 和 SearchStockInfo 函数添加 30 秒超时设置
This commit is contained in:
spark 2025-02-11 12:29:49 +08:00
parent d79bdc8bc1
commit b459abb35d
2 changed files with 19 additions and 3 deletions

View File

@ -108,6 +108,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
messages := SearchStockPriceInfo(stockCode)
if messages == nil || len(*messages) == 0 {
logger.SugaredLogger.Error("获取股票价格失败")
ch <- "***获取股票价格失败,分析结果可能不准确***<hr>"
return
}
price := ""
@ -125,6 +126,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
messages := GetFinancialReports(stockCode)
if messages == nil || len(*messages) == 0 {
logger.SugaredLogger.Error("获取股票财报失败")
ch <- "***获取股票财报失败,分析结果可能不准确***<hr>"
return
}
for _, message := range *messages {
@ -140,6 +142,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
messages := GetTelegraphList()
if messages == nil || len(*messages) == 0 {
logger.SugaredLogger.Error("获取市场资讯失败")
ch <- "***获取市场资讯失败,分析结果可能不准确***<hr>"
return
}
for _, message := range *messages {
@ -155,6 +158,7 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
messages := SearchStockInfo(stock, "depth")
if messages == nil || len(*messages) == 0 {
logger.SugaredLogger.Error("获取股票资讯失败")
ch <- "***获取股票资讯失败,分析结果可能不准确***<hr>"
return
}
for _, message := range *messages {
@ -169,6 +173,8 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
messages := SearchStockInfo(stock, "telegram")
if messages == nil || len(*messages) == 0 {
logger.SugaredLogger.Error("获取股票电报资讯失败")
ch <- "***获取股票电报资讯失败,分析结果可能不准确***<hr>"
return
}
for _, message := range *messages {
@ -265,8 +271,11 @@ func (o OpenAi) NewChatStream(stock, stockCode string) <-chan string {
func GetFinancialReports(stockCode string) *[]string {
// 创建一个 chromedp 上下文
timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer timeoutCtxCancel()
ctx, cancel := chromedp.NewContext(
context.Background(),
timeoutCtx,
chromedp.WithLogf(logger.SugaredLogger.Infof),
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)

View File

@ -518,8 +518,12 @@ func (IndexBasic) TableName() string {
func SearchStockPriceInfo(stockCode string) *[]string {
url := "https://www.cls.cn/stock?code=" + stockCode
// 创建一个 chromedp 上下文
timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer timeoutCtxCancel()
ctx, cancel := chromedp.NewContext(
context.Background(),
timeoutCtx,
chromedp.WithLogf(logger.SugaredLogger.Infof),
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)
@ -588,8 +592,11 @@ func FetchPrice(ctx context.Context) (string, error) {
}
func SearchStockInfo(stock, msgType string) *[]string {
// 创建一个 chromedp 上下文
timeoutCtx, timeoutCtxCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer timeoutCtxCancel()
ctx, cancel := chromedp.NewContext(
context.Background(),
timeoutCtx,
chromedp.WithLogf(logger.SugaredLogger.Infof),
chromedp.WithErrorf(logger.SugaredLogger.Errorf),
)