Skip to content

Commit

Permalink
add search fund func
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoxin committed Jul 16, 2021
1 parent 60b5e74 commit 3530d0d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
55 changes: 55 additions & 0 deletions datacenter/eastmoney/fund_search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 关键词搜索

package eastmoney

import (
"context"
"fmt"
"regexp"
"time"

"github.com/axiaoxin-com/goutils"
"github.com/axiaoxin-com/logging"
"go.uber.org/zap"
)

// SearchFundInfo 关键词搜索基金结构
type SearchFundInfo struct {
Code string
Name string
Type string
}

// SearchFund 关键词搜索, 股票、代码、拼音
func (e EastMoney) SearchFund(ctx context.Context, kw string) (results []SearchFundInfo, err error) {
count := 10
apiurl := fmt.Sprintf("https://fundsuggest.eastmoney.com/FundCodeNew.aspx?input=%s&count=%d&cb=x", kw, count)
logging.Debug(ctx, "EastMoney SearchFund "+apiurl+" begin")
beginTime := time.Now()
resp, err := goutils.HTTPGETRaw(ctx, e.HTTPClient, apiurl)
strresp := string(resp)
latency := time.Now().Sub(beginTime).Milliseconds()
logging.Debug(ctx, "EastMoney SearchFund "+apiurl+" end", zap.Int64("latency(ms)", latency), zap.Any("resp", strresp))
if err != nil {
return nil, err
}

if len(strresp) < 6 {
logging.Warnf(ctx, "SearchFund invalid resp: %s", strresp)
return nil, fmt.Errorf("无法找到相关基金")
}
reg, err := regexp.Compile(`"(?P<code>\d{6}),.+?,(?P<name>.+?),(?P<type>.+?),"`)
if err != nil {
logging.Error(ctx, "regexp error:"+err.Error())
return nil, err
}
matched := reg.FindAllStringSubmatch(strresp, -1)
for _, m := range matched {
results = append(results, SearchFundInfo{
Code: m[1],
Name: m[2],
Type: m[3],
})
}
return
}
13 changes: 13 additions & 0 deletions datacenter/eastmoney/fund_search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eastmoney

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSearchFund(t *testing.T) {
results, err := _em.SearchFund(_ctx, "半导体")
require.Nil(t, err)
t.Log(results)
}
4 changes: 2 additions & 2 deletions datacenter/sina/keyword_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ type SearchResult struct {
}

// KeywordSearch 关键词搜索, 股票、代码、拼音
func (q Sina) KeywordSearch(ctx context.Context, kw string) (results []SearchResult, err error) {
func (s Sina) KeywordSearch(ctx context.Context, kw string) (results []SearchResult, err error) {
apiurl := fmt.Sprintf("https://suggest3.sinajs.cn/suggest/key=%s", kw)
logging.Debug(ctx, "Sina KeywordSearch "+apiurl+" begin")
beginTime := time.Now()
resp, err := goutils.HTTPGETRaw(ctx, q.HTTPClient, apiurl)
resp, err := goutils.HTTPGETRaw(ctx, s.HTTPClient, apiurl)
utf8resp := transcode.FromString(string(resp)).Decode("GBK").ToString()
latency := time.Now().Sub(beginTime).Milliseconds()
logging.Debug(ctx, "Sina KeywordSearch "+apiurl+" end", zap.Int64("latency(ms)", latency), zap.Any("resp", utf8resp))
Expand Down

0 comments on commit 3530d0d

Please sign in to comment.