-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters