-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes.go
61 lines (48 loc) · 1.34 KB
/
quotes.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
// Package finance
package finance
import (
"fmt"
"strings"
)
const quoteURL = "http://download.finance.yahoo.com/d/quotes.csv"
// GetQuote fetches a single symbol's quote from Yahoo Finance.
func GetQuote(symbol string) (*Quote, error) {
params := map[string]string{
"s": symbol,
"f": strings.Join(quoteFields[:], ""),
"e": ".csv",
}
table, err := getQuotesTable(buildURL(quoteURL, params))
if err != nil {
return nil, err
}
return generateQuotes(table)[0], nil
}
// GetQuotes fetches multiple symbol's quotes from Yahoo Finance.
func GetQuotes(symbols []string) ([]*Quote, error) {
params := map[string]string{
"s": strings.Join(symbols[:], ","),
"f": strings.Join(quoteFields[:], ""),
"e": ".csv",
}
table, err := getQuotesTable(buildURL(quoteURL, params))
if err != nil {
return nil, err
}
return generateQuotes(table), nil
}
// getQuotesTable fetches the quotes data table from the endpoint.
func getQuotesTable(url string) ([][]string, error) {
table, err := requestCSV(url)
if err != nil {
return nil, fmt.Errorf("request table error: (error was: %s)\n", err.Error())
}
return table, nil
}
// generateQuotes turns the raw table data of quotes into proper quote structs.
func generateQuotes(table [][]string) (quotes []*Quote) {
for _, row := range table {
quotes = append(quotes, newQuote(row))
}
return quotes
}