-
Notifications
You must be signed in to change notification settings - Fork 102
/
finance.go
417 lines (350 loc) · 9.89 KB
/
finance.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package finance
import (
"context"
"encoding/json"
"io"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/piquette/finance-go/form"
)
// Printfer is an interface to be implemented by Logger.
type Printfer interface {
Printf(format string, v ...interface{})
}
// init sets initial logger defaults.
func init() {
Logger = log.New(os.Stderr, "", log.LstdFlags)
}
const (
// YFinBackend is a constant representing the yahoo service backend.
YFinBackend SupportedBackend = "yahoo"
// YFinURL is the URL of the yahoo service backend.
YFinURL string = "https://query1.finance.yahoo.com"
// BATSBackend is a constant representing the uploads service backend.
BATSBackend SupportedBackend = "bats"
// BATSURL is the URL of the uploads service backend.
BATSURL string = ""
// Private constants.
// ------------------
defaultHTTPTimeout = 80 * time.Second
yFinURL = "https://query1.finance.yahoo.com"
batsURL = ""
crumbURL = yFinURL + "/v1/test/getcrumb"
cookieURL = "https://login.yahoo.com"
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0"
)
var (
// LogLevel is the logging level for this library.
// 0: no logging
// 1: errors only
// 2: errors + informational (default)
// 3: errors + informational + debug
LogLevel = 0
// Logger controls how this library performs logging at a package level. It is useful
// to customise if you need it prefixed for your application to meet other
// requirements
Logger Printfer
// Private vars.
// -------------
httpClient = &http.Client{Timeout: defaultHTTPTimeout}
backends Backends
)
// SupportedBackend is an enumeration of supported api endpoints.
type SupportedBackend string
// Backends are the currently supported endpoints.
type Backends struct {
YFin, Bats Backend
mu sync.RWMutex
}
// BackendConfiguration is the internal implementation for making HTTP calls.
type BackendConfiguration struct {
Type SupportedBackend
URL string
HTTPClient *http.Client
}
// yahooConfiguration is a specialization that includes a crumb and cookies for the yahoo API
type yahooConfiguration struct {
BackendConfiguration
expiry time.Time
cookies string
crumb string
}
// Backend is an interface for making calls against an api service.
// This interface exists to enable mocking for during testing if needed.
type Backend interface {
Call(path string, body *form.Values, ctx *context.Context, v interface{}) error
}
// SetHTTPClient overrides the default HTTP client.
// This is useful if you're running in a Google AppEngine environment
// where the http.DefaultClient is not available.
func SetHTTPClient(client *http.Client) {
httpClient = client
}
// NewBackends creates a new set of backends with the given HTTP client. You
// should only need to use this for testing purposes or on App Engine.
func NewBackends(httpClient *http.Client) *Backends {
return &Backends{
YFin: &yahooConfiguration{
BackendConfiguration{YFinBackend, YFinURL, httpClient},
time.Time{},
"",
"",
},
Bats: &BackendConfiguration{
BATSBackend, BATSURL, httpClient,
},
}
}
// GetBackend returns the currently used backend in the binding.
func GetBackend(backend SupportedBackend) Backend {
switch backend {
case YFinBackend:
backends.mu.RLock()
ret := backends.YFin
backends.mu.RUnlock()
if ret != nil {
return ret
}
backends.mu.Lock()
defer backends.mu.Unlock()
backends.YFin = &yahooConfiguration{
BackendConfiguration{YFinBackend, YFinURL, httpClient},
time.Time{},
"",
"",
}
return backends.YFin
case BATSBackend:
backends.mu.RLock()
ret := backends.Bats
backends.mu.RUnlock()
if ret != nil {
return ret
}
backends.mu.Lock()
defer backends.mu.Unlock()
backends.Bats = &BackendConfiguration{backend, batsURL, httpClient}
return backends.Bats
}
return nil
}
// SetBackend sets the backend used in the binding.
func SetBackend(backend SupportedBackend, b Backend) {
switch backend {
case YFinBackend:
backends.YFin = b
case BATSBackend:
backends.Bats = b
}
}
func fetchCookies() (string, time.Time, error) {
client := http.Client{}
request, err := http.NewRequest("GET", cookieURL, nil)
if err != nil {
return "", time.Time{}, err
}
request.Header = http.Header{
"Accept": {"*/*"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"en-US,en;q=0.5"},
"Connection": {"keep-alive"},
"Host": {"login.yahoo.com"},
"Sec-Fetch-Dest": {"document"},
"Sec-Fetch-Mode": {"navigate"},
"Sec-Fetch-Site": {"none"},
"Sec-Fetch-User": {"?1"},
"TE": {"trailers"},
"Update-Insecure-Requests": {"1"},
"User-Agent": {userAgent},
}
response, err := client.Do(request)
if err != nil {
return "", time.Time{}, err
}
defer response.Body.Close()
var result string
// create a variable expiry that is ten years in the future
var expiry = time.Now().AddDate(10, 0, 0)
for _, cookie := range response.Cookies() {
if cookie.MaxAge <= 0 {
continue
}
cookieExpiry := time.Now().Add(time.Duration(cookie.MaxAge) * time.Second)
if cookie.Name != "AS" {
result += cookie.Name + "=" + cookie.Value + "; "
// set expiry to the latest cookie expiry if smaller than the current expiry
if cookie.Expires.Before(cookieExpiry) {
expiry = cookieExpiry
}
}
}
result = strings.TrimSuffix(result, "; ")
return result, expiry, nil
}
func fetchCrumb(cookies string) (string, error) {
client := http.Client{}
request, err := http.NewRequest("GET", crumbURL, nil)
if err != nil {
return "", err
}
request.Header = http.Header{
"Accept": {"*/*"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"en-US,en;q=0.5"},
"Connection": {"keep-alive"},
"Content-Type": {"text/plain"},
"Cookie": {cookies},
"Host": {"query1.finance.yahoo.com"},
"Sec-Fetch-Dest": {"empty"},
"Sec-Fetch-Mode": {"cors"},
"Sec-Fetch-Site": {"same-site"},
"TE": {"trailers"},
"User-Agent": {userAgent},
}
response, err := client.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(body[:]), nil
}
func (s *yahooConfiguration) refreshCrumb() error {
cookies, expiry, err := fetchCookies()
if err != nil {
return err
}
crumb, err := fetchCrumb(cookies)
if err != nil {
return err
}
s.crumb = crumb
s.expiry = expiry
s.cookies = cookies
return nil
}
// Call is the Backend.Call implementation for invoking market data APIs, using the Yahoo specialization
func (s *yahooConfiguration) Call(path string, form *form.Values, ctx *context.Context, v interface{}) error {
// Check if the cookies have expired.
if s.expiry.Before(time.Now()) {
// Refresh the cookies and crumb.
err := s.refreshCrumb()
if err != nil {
return err
}
}
if s.crumb != "" {
form.Add("crumb", s.crumb)
}
if form != nil && !form.Empty() {
path += "?" + form.Encode()
}
req, err := s.newRequest("GET", path, ctx)
if err != nil {
return err
}
if err := s.do(req, v); err != nil {
return err
}
return nil
}
// Call is the Backend.Call implementation for invoking market data APIs.
func (s *BackendConfiguration) Call(path string, form *form.Values, ctx *context.Context, v interface{}) error {
if form != nil && !form.Empty() {
path += "?" + form.Encode()
}
req, err := s.newRequest("GET", path, ctx)
if err != nil {
return err
}
if err := s.do(req, v); err != nil {
return err
}
return nil
}
func (s *yahooConfiguration) newRequest(method, path string, ctx *context.Context) (*http.Request, error) {
req, err := s.BackendConfiguration.newRequest(method, path, ctx)
if err != nil {
return nil, err
}
req.Header = http.Header{
"Accept": {"*/*"},
"Accept-Language": {"en-US,en;q=0.5"},
"Connection": {"keep-alive"},
"Content-Type": {"application/json"},
"Cookie": {s.cookies},
"Host": {"query1.finance.yahoo.com"},
"Origin": {"https://finance.yahoo.com"},
"Referer": {"https://finance.yahoo.com"},
"Sec-Fetch-Dest": {"empty"},
"Sec-Fetch-Mode": {"cors"},
"Sec-Fetch-Site": {"same-site"},
"TE": {"trailers"},
"User-Agent": {userAgent},
}
return req, nil
}
func (s *BackendConfiguration) newRequest(method, path string, ctx *context.Context) (*http.Request, error) {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
path = s.URL + path
req, err := http.NewRequest(method, path, nil)
if err != nil {
if LogLevel > 0 {
Logger.Printf("Cannot create api request: %v\n", err)
}
return nil, err
}
if ctx != nil {
req = req.WithContext(*ctx)
}
return req, nil
}
// do is used by Call to execute an API request and parse the response. It uses
// the backend's HTTP client to execute the request and unmarshals the response
// into v. It also handles unmarshaling errors returned by the API.
func (s *BackendConfiguration) do(req *http.Request, v interface{}) error {
if LogLevel > 1 {
Logger.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path)
}
start := time.Now()
res, err := s.HTTPClient.Do(req)
if LogLevel > 2 {
Logger.Printf("Completed in %v\n", time.Since(start))
}
if err != nil {
if LogLevel > 0 {
Logger.Printf("Request to api failed: %v\n", err)
}
return err
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
if LogLevel > 0 {
Logger.Printf("Cannot parse response: %v\n", err)
}
return err
}
if res.StatusCode >= 400 {
if LogLevel > 0 {
Logger.Printf("API error: %q\n", resBody)
}
return CreateRemoteErrorS("error response recieved from upstream api")
}
if LogLevel > 2 {
Logger.Printf("API response: %q\n", resBody)
}
if v != nil {
return json.Unmarshal(resBody, v)
}
return nil
}