-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
173 lines (138 loc) · 3.79 KB
/
client.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
// Package yahoofinance provides a way to fetch quotes for equities and
// currencies from Yahoo Finance.
package yahoofinance
import (
"encoding/csv"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// quoteURL of the remote service
const quotesURL string = "http://download.finance.yahoo.com/d/quotes.csv"
// Quote data
type Quote struct {
Symbol string `json:"symbol"`
Name string `json:"name"`
PreviousClose float64 `json:"previous_close"`
Open float64 `json:"open"`
Price float64 `json:"price"`
Change float64 `json:"change"`
ChangePct float64 `json:"change_pct"`
DayLow float64 `json:"day_low"`
DayHigh float64 `json:"day_high"`
LastTradeDate string `json:"last_trade_date"`
LastTradeTime string `json:"last_trade_time"`
LastTrade float64 `json:"last_trade"`
}
// Client provides access to the Quotes service.
type Client struct {
BaseURL string
HTTPClient *http.Client
}
// NewClient returns a new default Client
func NewClient() *Client {
return &Client{
BaseURL: quotesURL,
HTTPClient: defaultHTTPClient(),
}
}
// defaultHTTPClient returns http.Client with reasonable defaults
func defaultHTTPClient() *http.Client {
return &http.Client{
Timeout: time.Duration(2 * time.Second),
}
}
// GetQuotes return Quotes for the given symbols
func (client *Client) GetQuotes(symbols []string) ([]Quote, error) {
// get the body from the HTTP service
body, err := client.get(symbols)
if err != nil {
return nil, err
}
// get the csv data
rows := parseCSV(body)
// create a slice to hold each Quote
quotes := make([]Quote, len(symbols))
// create a Quote from each row from the CSV
for i, row := range rows {
quotes[i] = buildQuote(row)
}
return quotes, nil
}
// buildURL returns a url that can be used to request quotes for the
// given symbols.
func (client *Client) buildURL(symbols []string) string {
u, _ := url.Parse(client.BaseURL)
u.RawQuery = buildParameters(symbols).Encode()
return u.String()
}
// get makes the HTTP request to the service.
func (client *Client) get(symbols []string) (string, error) {
// get the full url
url := client.buildURL(symbols)
// get the HTTP response
response, err := client.HTTPClient.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
// read the body
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// formatSymbols returns symbols formatted for the query.
func formatSymbols(symbols []string) string {
return strings.Join(symbols, "+")
}
// buildParamaters consttucts parameters for the service.
//
// See Yahoo-data.htm for format details.
func buildParameters(symbols []string) url.Values {
return url.Values{
"s": {formatSymbols(symbols)},
"f": {"snpol1c1p2ghd1t1l1"},
}
}
// parseFloat returns a float64 from the value, handling "%" characters. If
// a value cannot be extract zero is returned.
func parseFloat(value string) float64 {
if value == "N/A" {
return 0
}
f, err := strconv.ParseFloat(strings.Replace(value, "%", "", 1), 64)
if err != nil {
return 0
}
return f
}
// buildQuote returns a quote from the array of data
func buildQuote(data []string) Quote {
q := Quote{}
q.Symbol = data[0]
q.Name = data[1]
q.PreviousClose = parseFloat(data[2])
q.Open = parseFloat(data[3])
q.Price = parseFloat(data[4])
q.Change = parseFloat(data[5])
q.ChangePct = parseFloat(data[6])
q.DayLow = parseFloat(data[7])
q.DayHigh = parseFloat(data[8])
q.LastTradeDate = data[9]
q.LastTradeTime = data[10]
q.LastTrade = parseFloat(data[11])
return q
}
// parseCSV extracts data from the csv body
func parseCSV(body string) [][]string {
reader := strings.NewReader(body)
csv := csv.NewReader(reader)
csv.LazyQuotes = true
data, _ := csv.ReadAll()
return data
}