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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
package ibd
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"ibd-trader/internal/database"
"ibd-trader/internal/utils"
"github.com/Rhymond/go-money"
"golang.org/x/net/html"
)
func (c *Client) StockInfo(ctx context.Context, uri string) (*database.StockInfo, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
_, cookie, err := c.getCookie(ctx, nil)
if err != nil {
return nil, err
}
req.AddCookie(cookie)
// Set required query parameters
params := url.Values{}
params.Set("list", "ibd50")
params.Set("type", "weekly")
req.URL.RawQuery = params.Encode()
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.Result.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"unexpected status code %d: %s",
resp.Result.StatusCode,
resp.Result.Content,
)
}
node, err := html.Parse(strings.NewReader(resp.Result.Content))
if err != nil {
return nil, err
}
name, symbol, err := extractNameAndSymbol(node)
if err != nil {
return nil, fmt.Errorf("failed to extract name and symbol: %w", err)
}
chartAnalysis, err := extractChartAnalysis(node)
if err != nil {
return nil, fmt.Errorf("failed to extract chart analysis: %w", err)
}
ratings, err := extractRatings(node)
if err != nil {
return nil, fmt.Errorf("failed to extract ratings: %w", err)
}
price, err := extractPrice(node)
if err != nil {
return nil, fmt.Errorf("failed to extract price: %w", err)
}
return &database.StockInfo{
Symbol: symbol,
Name: name,
ChartAnalysis: chartAnalysis,
Ratings: ratings,
Price: price,
}, nil
}
func extractNameAndSymbol(node *html.Node) (name string, symbol string, err error) {
// Find span with ID "quote-symbol"
quoteSymbolNode := findId(node, "quote-symbol")
if quoteSymbolNode == nil {
return "", "", fmt.Errorf("could not find `quote-symbol` span")
}
// Get the text of the quote-symbol span
name = strings.TrimSpace(extractText(quoteSymbolNode))
// Find span with ID "qteSymb"
qteSymbNode := findId(node, "qteSymb")
if qteSymbNode == nil {
return "", "", fmt.Errorf("could not find `qteSymb` span")
}
// Get the text of the qteSymb span
symbol = strings.TrimSpace(extractText(qteSymbNode))
// Get index of last closing parenthesis
lastParenIndex := strings.LastIndex(name, ")")
if lastParenIndex == -1 {
return
}
// Find the last opening parenthesis before the closing parenthesis
lastOpenParenIndex := strings.LastIndex(name[:lastParenIndex], "(")
if lastOpenParenIndex == -1 {
return
}
// Remove the parenthesis pair
name = strings.TrimSpace(name[:lastOpenParenIndex] + name[lastParenIndex+1:])
return
}
func extractPrice(node *html.Node) (*money.Money, error) {
// Find the div with the ID "lstPrice"
lstPriceNode := findId(node, "lstPrice")
if lstPriceNode == nil {
return nil, fmt.Errorf("could not find `lstPrice` div")
}
// Get the text of the lstPrice div
priceStr := strings.TrimSpace(extractText(lstPriceNode))
// Parse the price
price, err := utils.ParseMoney(priceStr)
if err != nil {
return nil, fmt.Errorf("failed to parse price: %w", err)
}
return price, nil
}
func extractRatings(node *html.Node) (ratings database.Ratings, err error) {
// Find the div with class "smartContent"
smartSelectNode := findClass(node, "smartContent")
if smartSelectNode == nil {
return ratings, fmt.Errorf("could not find `smartContent` div")
}
// Iterate over children, looking for "smartRating" divs
for c := smartSelectNode.FirstChild; c != nil; c = c.NextSibling {
if !isClass(c, "smartRating") {
continue
}
err = processSmartRating(c, &ratings)
if err != nil {
return
}
}
return
}
// processSmartRating extracts the rating from a "smartRating" div and updates the ratings struct.
//
// The node should look like this:
//
// <ul class="smartRating">
// <li><a><span>Composite Rating</span></a></li>
// <li>94</li>
// ...
// </ul>
func processSmartRating(node *html.Node, ratings *database.Ratings) error {
// Check that the node is a ul
if node.Type != html.ElementNode || node.Data != "ul" {
return fmt.Errorf("expected ul node, got %s", node.Data)
}
// Get all `li` children
children := findChildren(node, func(node *html.Node) bool {
return node.Type == html.ElementNode && node.Data == "li"
})
// Extract the rating name
ratingName := strings.TrimSpace(extractText(children[0]))
// Extract the rating value
ratingValueStr := strings.TrimSpace(extractText(children[1]))
switch ratingName {
case "Composite Rating":
ratingValue, err := strconv.ParseUint(ratingValueStr, 10, 8)
if err != nil {
return fmt.Errorf("failed to parse Composite Rating: %w", err)
}
ratings.Composite = uint8(ratingValue)
case "EPS Rating":
ratingValue, err := strconv.ParseUint(ratingValueStr, 10, 8)
if err != nil {
return fmt.Errorf("failed to parse EPS Rating: %w", err)
}
ratings.EPS = uint8(ratingValue)
case "RS Rating":
ratingValue, err := strconv.ParseUint(ratingValueStr, 10, 8)
if err != nil {
return fmt.Errorf("failed to parse RS Rating: %w", err)
}
ratings.RelStr = uint8(ratingValue)
case "Group RS Rating":
ratingValue, err := database.LetterRatingFromString(ratingValueStr)
if err != nil {
return fmt.Errorf("failed to parse Group RS Rating: %w", err)
}
ratings.GroupRelStr = ratingValue
case "SMR Rating":
ratingValue, err := database.LetterRatingFromString(ratingValueStr)
if err != nil {
return fmt.Errorf("failed to parse SMR Rating: %w", err)
}
ratings.SMR = ratingValue
case "Acc/Dis Rating":
ratingValue, err := database.LetterRatingFromString(ratingValueStr)
if err != nil {
return fmt.Errorf("failed to parse Acc/Dis Rating: %w", err)
}
ratings.AccDis = ratingValue
default:
return fmt.Errorf("unknown rating name: %s", ratingName)
}
return nil
}
func extractChartAnalysis(node *html.Node) (string, error) {
// Find the div with class "chartAnalysis"
chartAnalysisNode := findClass(node, "chartAnalysis")
if chartAnalysisNode == nil {
return "", fmt.Errorf("could not find `chartAnalysis` div")
}
// Get the text of the chart analysis div
chartAnalysis := strings.TrimSpace(extractText(chartAnalysisNode))
return chartAnalysis, nil
}
|