/
profit_predict.go
82 lines (74 loc) · 2.15 KB
/
profit_predict.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 获取盈利预测
package eastmoney
import (
"context"
"fmt"
"strings"
"time"
"github.com/axiaoxin-com/goutils"
"github.com/axiaoxin-com/logging"
"go.uber.org/zap"
)
// RespProfitPredict 盈利预测接口返回结构
type RespProfitPredict struct {
Version string `json:"version"`
Result struct {
Pages int `json:"pages"`
Data []ProfitPredict `json:"data"`
Count int `json:"count"`
} `json:"result"`
Success bool `json:"success"`
Message string `json:"message"`
Code int `json:"code"`
}
// ProfitPredict 盈利预测
type ProfitPredict struct {
// 年份
PredictYear int `json:"PREDICT_YEAR"`
// 预测每股收益
Eps float64 `json:"EPS"`
// 预测市盈率
Pe float64 `json:"PE"`
}
// ProfitPredictList 预测列表
type ProfitPredictList []ProfitPredict
func (p ProfitPredictList) String() string {
s := []string{}
for _, i := range p {
s = append(s, fmt.Sprintf("%d | 预测每股收益:%f 预测市盈率:%f", i.PredictYear, i.Eps, i.Pe))
}
return strings.Join(s, "<br/>")
}
// QueryProfitPredict 获取盈利预测
func (e EastMoney) QueryProfitPredict(ctx context.Context, secuCode string) (ProfitPredictList, error) {
apiurl := "https://datacenter.eastmoney.com/securities/api/data/get"
params := map[string]string{
"source": "SECURITIES",
"client": "APP",
"type": "RPT_RES_PROFITPREDICT",
"sty": "PREDICT_YEAR,EPS,PE",
"filter": fmt.Sprintf(`(SECUCODE="%s")`, strings.ToUpper(secuCode)),
"sr": "1",
"st": "PREDICT_YEAR",
}
logging.Debug(ctx, "EastMoney QueryProfitPredict "+apiurl+" begin", zap.Any("params", params))
beginTime := time.Now()
apiurl, err := goutils.NewHTTPGetURLWithQueryString(ctx, apiurl, params)
if err != nil {
return nil, err
}
resp := RespProfitPredict{}
err = goutils.HTTPGET(ctx, e.HTTPClient, apiurl, nil, &resp)
latency := time.Now().Sub(beginTime).Milliseconds()
logging.Debug(ctx, "EastMoney QueryProfitPredict "+apiurl+" end",
zap.Int64("latency(ms)", latency),
// zap.Any("resp", resp),
)
if err != nil {
return nil, err
}
if resp.Code != 0 {
return nil, fmt.Errorf("%s %#v", secuCode, resp)
}
return resp.Result.Data, nil
}