From 43063fa7fb5d061d370ae425dd511990b1bed4dd Mon Sep 17 00:00:00 2001 From: ArvinLovegood Date: Sun, 29 Jun 2025 17:31:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(data):=20=E6=B7=BB=E5=8A=A0=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E8=82=A1=E7=A5=A8=20API=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了搜索股票 API 的请求和解析功能 - 添加了搜索股票的测试用例 --- backend/data/search_stock_api.go | 55 +++++++++++++++++++++++++++ backend/data/search_stock_api_test.go | 20 ++++++++++ 2 files changed, 75 insertions(+) create mode 100644 backend/data/search_stock_api.go create mode 100644 backend/data/search_stock_api_test.go diff --git a/backend/data/search_stock_api.go b/backend/data/search_stock_api.go new file mode 100644 index 0000000..4b1b7e2 --- /dev/null +++ b/backend/data/search_stock_api.go @@ -0,0 +1,55 @@ +package data + +import ( + "encoding/json" + "fmt" + "github.com/go-resty/resty/v2" + "go-stock/backend/logger" + "time" +) + +// @Author spark +// @Date 2025/6/28 21:02 +// @Desc +// ----------------------------------------------------------------------------------- +type SearchStockApi struct { + words string +} + +func NewSearchStockApi(words string) *SearchStockApi { + return &SearchStockApi{words: words} +} +func (s SearchStockApi) SearchStock() map[string]any { + url := "https://np-tjxg-g.eastmoney.com/api/smart-tag/stock/v3/pw/search-code" + resp, err := resty.New().SetTimeout(time.Duration(30)*time.Second).R(). + SetHeader("Host", "np-tjxg-g.eastmoney.com"). + SetHeader("Origin", "https://xuangu.eastmoney.com"). + SetHeader("Referer", "https://xuangu.eastmoney.com/"). + SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0"). + SetHeader("Content-Type", "application/json"). + SetBody(fmt.Sprintf(`{ + "keyWord": "%s", + "pageSize": 50, + "pageNo": 1, + "fingerprint": "e38b5faabf9378c8238e57219f0ebc9b", + "gids": [], + "matchWord": "", + "timestamp": "1751113883290349", + "shareToGuba": false, + "requestId": "8xTWgCDAjvQ5lmvz5mDA3Ydk2AE4yoiJ1751113883290", + "needCorrect": true, + "removedConditionIdList": [], + "xcId": "xc0af28549ab330013ed", + "ownSelectAll": false, + "dxInfo": [], + "extraCondition": "" + }`, s.words)).Post(url) + if err != nil { + logger.SugaredLogger.Errorf("SearchStock-err:%+v", err) + return map[string]any{} + } + respMap := map[string]any{} + json.Unmarshal(resp.Body(), &respMap) + logger.SugaredLogger.Infof("resp:%+v", respMap["data"]) + return respMap +} diff --git a/backend/data/search_stock_api_test.go b/backend/data/search_stock_api_test.go new file mode 100644 index 0000000..f324988 --- /dev/null +++ b/backend/data/search_stock_api_test.go @@ -0,0 +1,20 @@ +package data + +import ( + "go-stock/backend/db" + "go-stock/backend/logger" + "testing" +) + +func TestSearchStock(t *testing.T) { + db.Init("../../data/stock.db") + + res := NewSearchStockApi("换手率连续5日大于5%").SearchStock() + data := res["data"].(map[string]any) + result := data["result"].(map[string]any) + dataList := result["dataList"].([]any) + for _, v := range dataList { + logger.SugaredLogger.Infof("v:%+v", v) + } + +}