mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
fix(stock_data):修复股票代码实时数据获取时的异常字符问题
- 在获取昨日收盘价时,使用 strutil.ReplaceWithMap函数去除引号 - 移除了测试代码中不必要的 JSON 序列化和数据库操作 - 在测试函数中添加了新的股票代码参数
This commit is contained in:
parent
0864806770
commit
7417caa778
@ -555,7 +555,7 @@ func ParseUSStockData(datas []string) (map[string]string, error) {
|
||||
result["股票代码"] = code
|
||||
result["股票名称"] = parts[0]
|
||||
result["今日开盘价"] = parts[5]
|
||||
result["昨日收盘价"] = parts[len(parts)-1]
|
||||
result["昨日收盘价"] = strutil.ReplaceWithMap(parts[len(parts)-1], map[string]string{"\"": ""})
|
||||
result["今日最高价"] = parts[6]
|
||||
result["今日最低价"] = parts[7]
|
||||
result["当前价格"] = parts[1]
|
||||
|
@ -105,32 +105,12 @@ func TestParseFullSingleStockData(t *testing.T) {
|
||||
logger.SugaredLogger.Error(er.Error())
|
||||
}
|
||||
logger.SugaredLogger.Infof("%+#v", result)
|
||||
marshal, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
logger.SugaredLogger.Errorf("json.Marshal error:%s", err.Error())
|
||||
}
|
||||
logger.SugaredLogger.Infof("json:%s", string(marshal))
|
||||
stockInfo := &StockInfo{}
|
||||
err = json.Unmarshal(marshal, &stockInfo)
|
||||
if err != nil {
|
||||
logger.SugaredLogger.Errorf("json.Unmarshal error:%s", err.Error())
|
||||
}
|
||||
logger.SugaredLogger.Infof("%+#v", stockInfo)
|
||||
stockData := stockInfo
|
||||
db.Init("../../data/stock.db")
|
||||
var count int64
|
||||
db.Dao.Model(&StockInfo{}).Where("code = ?", stockData.Code).Count(&count)
|
||||
if count == 0 {
|
||||
db.Dao.Model(&StockInfo{}).Create(stockData)
|
||||
} else {
|
||||
db.Dao.Model(&StockInfo{}).Where("code = ?", stockData.Code).Updates(stockData)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewStockDataApi(t *testing.T) {
|
||||
db.Init("../../data/stock.db")
|
||||
stockDataApi := NewStockDataApi()
|
||||
datas, _ := stockDataApi.GetStockCodeRealTimeData("sh600859", "sh600745")
|
||||
datas, _ := stockDataApi.GetStockCodeRealTimeData("sh600859", "sh600745", "gb_tsla")
|
||||
for _, data := range *datas {
|
||||
t.Log(data)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user