-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahoo_api.go
78 lines (68 loc) · 2.39 KB
/
yahoo_api.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type ChartResponse struct {
Chart struct {
Result []struct {
Meta struct {
Currency string `json:"currency"`
Symbol string `json:"symbol"`
ExchangeName string `json:"exchangeName"`
InstrumentType string `json:"instrumentType"`
FirstTradeDate int `json:"firstTradeDate"`
RegularMarketTime int `json:"regularMarketTime"`
HasPrePostMarketData bool `json:"hasPrePostMarketData"`
GMTOffset int `json:"gmtoffset"`
Timezone string `json:"timezone"`
ExchangeTimezoneName string `json:"exchangeTimezoneName"`
RegularMarketPrice float64 `json:"regularMarketPrice"`
ChartPreviousClose float64 `json:"chartPreviousClose"`
PreviousClose float64 `json:"previousClose"`
Scale int `json:"scale"`
PriceHint int `json:"priceHint"`
CurrentTradingPeriod struct {
Pre TradingPeriod `json:"pre"`
Regular TradingPeriod `json:"regular"`
Post TradingPeriod `json:"post"`
} `json:"currentTradingPeriod"`
TradingPeriods [][]TradingPeriod `json:"tradingPeriods"`
DataGranularity string `json:"dataGranularity"`
Range string `json:"range"`
ValidRanges []string `json:"validRanges"`
} `json:"meta"`
Timestamp []int64 `json:"timestamp"`
Indicators struct {
Quote []struct {
Close []float64 `json:"close"`
Low []float64 `json:"low"`
High []float64 `json:"high"`
Open []float64 `json:"open"`
} `json:"quote"`
} `json:"indicators"`
} `json:"result"`
} `json:"chart"`
}
type TradingPeriod struct {
Timezone string `json:"timezone"`
Start int64 `json:"start"`
End int64 `json:"end"`
GMToffset int `json:"gmtoffset"`
}
func fetchPrice(ticker string) (ChartResponse, error) {
var response ChartResponse
const baseUrl = "https://query1.finance.yahoo.com/v8/finance/chart/%s?region=US&lang=en-US&includePrePost=false&interval=2m&useYfid=true&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance"
fetchUrl := fmt.Sprintf(baseUrl, ticker)
res, err := http.Get(fetchUrl)
if err != nil {
return response, err
}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
log.Fatal(err)
}
return response, err
}