/
scraper.go
170 lines (137 loc) · 3.55 KB
/
scraper.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
package yahoo
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"strconv"
"sync"
"time"
"github.com/ahmethakanbesel/finance-api/scraper"
)
const (
downloadEndpoint = "https://query1.finance.yahoo.com/v7/finance/download/%s?%s"
dateFormat = "2006-01-02"
chunkSize = 1250
)
type Scraper struct {
workers int
wg sync.WaitGroup
}
var _ scraper.Scraper = (*Scraper)(nil)
func NewScraper(options ...func(*Scraper)) *Scraper {
s := &Scraper{}
for _, o := range options {
o(s)
}
return s
}
func WithWorkers(workers int) func(*Scraper) {
return func(s *Scraper) {
s.workers = workers
}
}
type (
chunk struct {
symbol string
startDate time.Time
endDate time.Time
}
)
func (s *Scraper) GetSymbolData(symbol string, startDate, endDate time.Time) (<-chan *scraper.SymbolPrice, error) {
if symbol == "" {
return nil, fmt.Errorf("symbol cannot be empty")
}
if startDate.IsZero() {
return nil, fmt.Errorf("start date cannot be empty")
}
if endDate.IsZero() {
endDate = time.Now()
}
if startDate.After(endDate) {
return nil, fmt.Errorf("start date cannot be after end date")
}
dataChan := make(chan *scraper.SymbolPrice, chunkSize*s.workers)
chunkChan := s.createChunks(symbol, startDate, endDate)
for i := 0; i < s.workers; i++ {
s.wg.Add(1)
go s.worker(chunkChan, dataChan)
}
s.wg.Wait()
close(dataChan)
return dataChan, nil
}
func (s *Scraper) createChunks(symbol string, startDate, endDate time.Time) <-chan *chunk {
chunkChan := make(chan *chunk)
go func() {
defer close(chunkChan)
for currentStartDate := startDate; currentStartDate.Before(endDate); currentStartDate = currentStartDate.AddDate(0, 0, chunkSize) {
currentEndDate := currentStartDate.AddDate(0, 0, chunkSize-1)
if currentEndDate.After(endDate) {
currentEndDate = endDate
}
chunkChan <- &chunk{
symbol: symbol,
startDate: currentStartDate,
endDate: currentEndDate,
}
}
}()
return chunkChan
}
func (s *Scraper) worker(chunkChan <-chan *chunk, dataChan chan<- *scraper.SymbolPrice) {
defer s.wg.Done()
for chunk := range chunkChan {
chunkData, err := getSymbolData(chunk.symbol, chunk.startDate, chunk.endDate)
if err != nil {
slog.Error("error retrieving tefas data", "fund", chunk.symbol, "startDate", chunk.startDate, "endDate", chunk.endDate, "error", err)
continue
}
csvReader := csv.NewReader(bytes.NewReader(chunkData))
data, err := csvReader.ReadAll()
if err != nil {
slog.Error("error parsing csv", "error", err)
continue
}
for _, row := range data {
if len(row) < 5 {
continue
}
parsedPrice, err := strconv.ParseFloat(row[4], 64)
if err != nil {
continue
}
dataChan <- &scraper.SymbolPrice{
Date: row[0],
Close: parsedPrice,
}
}
}
}
func getSymbolData(symbol string, startDate, endDate time.Time) ([]byte, error) {
client := &http.Client{}
params := url.Values{}
params.Add("events", "history")
params.Add("interval", "1d")
params.Add("corsDomain", "finance.yahoo.com")
params.Add("period1", strconv.FormatInt(startDate.Unix(), 10))
params.Add("period2", strconv.FormatInt(endDate.Unix(), 10))
req, err := http.NewRequest("GET", fmt.Sprintf(downloadEndpoint, symbol, params.Encode()), nil)
if err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
slog.Info("retrieved yahoo data", "symbol", symbol, "startDate", startDate, "endDate", endDate)
return body, nil
}