-
Notifications
You must be signed in to change notification settings - Fork 3
/
screener.go
224 lines (192 loc) · 5.17 KB
/
screener.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
// Copyright (c) 2022 James Bury. All rights reserved.
// Project site: https://github.com/d3an/finviz
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE file for the project.
package screener
import (
"fmt"
"io"
"net"
"net/http"
"strconv"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/cenkalti/backoff/v4"
"github.com/corpix/uarand"
"github.com/dnaeon/go-vcr/recorder"
"github.com/go-gota/gota/dataframe"
"github.com/pkg/errors"
"github.com/d3an/finviz/utils"
)
var (
once sync.Once
instance *Client
)
type Config struct {
userAgent string
recorder *recorder.Recorder
}
type Client struct {
*http.Client
config Config
}
func New(config *Config) *Client {
once.Do(func() {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 30 * time.Second,
}
client := &http.Client{
Timeout: 30 * time.Second,
Transport: transport,
}
if config != nil {
instance = &Client{Client: client, config: *config}
}
instance = &Client{
Client: client,
config: Config{userAgent: uarand.GetRandom()},
}
})
return instance
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", c.config.userAgent)
return c.Client.Do(req)
}
func (c *Client) RandomizeUserAgent() {
c.config.userAgent = uarand.GetRandom()
}
type scrapeResult struct {
Results []map[string]interface{}
Keys []string
PageCount int
Error error
}
func scrape(view int, doc *goquery.Document) *scrapeResult {
switch view / 10 {
case 10, 11, 12, 13, 14, 15, 16, 17:
return DefaultScrape(doc)
case 20, 21:
return ChartScrape(doc)
case 30, 31:
return BasicScrape(doc)
case 32:
return NewsScrape(doc)
case 33:
return DescriptionScrape(doc)
case 34:
return SnapshotScrape(doc)
case 35:
return TAScrape(doc)
case 40, 41:
return TickerScrape(doc)
case 50, 51:
return BulkScrape(doc)
case 52:
return BulkFullScrape(doc)
default:
return &scrapeResult{Error: fmt.Errorf("error view '%d' not found", view)}
}
}
func (c *Client) GetScreenerResults(url string) (*dataframe.DataFrame, error) {
var wg sync.WaitGroup
var results []map[string]interface{}
firstLook := make(chan scrapeResult, 1)
wg.Add(1)
go c.getData(url, &wg, &firstLook)
wg.Wait()
firstPage := <-firstLook
if firstPage.Error != nil {
return nil, errors.Wrapf(firstPage.Error, "error received while scraping screener")
}
results = append(results, firstPage.Results...)
pagesLeft := firstPage.PageCount - 1
if pagesLeft <= 0 {
return processScrapeResults(firstPage.Keys, results)
}
maxRows := len(results)
scrapeResults := make([]chan scrapeResult, pagesLeft)
for i := range scrapeResults {
scrapeResults[i] = make(chan scrapeResult, 1)
}
for i := 0; i < pagesLeft; i++ {
wg.Add(1)
go c.getData(fmt.Sprintf("%s&r=%d", url, (maxRows*(i+1))+1), &wg, &scrapeResults[i])
wg.Wait()
}
for i := 0; i < pagesLeft; i++ {
page := <-scrapeResults[i]
if page.Error != nil {
return nil, errors.Wrapf(page.Error, "error received while scraping screener: page '%d'", i+2)
}
results = append(results, page.Results...)
}
return processScrapeResults(firstPage.Keys, results)
}
func processScrapeResults(keys []string, results []map[string]interface{}) (*dataframe.DataFrame, error) {
rows, err := utils.GenerateRows(keys, results)
if err != nil {
return nil, errors.Wrap(err, "failed to generate rows from KVP map")
}
df := dataframe.LoadRecords(rows)
return utils.CleanFinvizDataFrame(&df), nil
}
func (c *Client) getData(url string, wg *sync.WaitGroup, scr *chan scrapeResult) {
defer wg.Done()
defer close(*scr)
req, err := http.NewRequest(http.MethodGet, url, http.NoBody)
if err != nil {
*scr <- scrapeResult{Error: err}
return
}
view, err := strconv.ParseInt(req.URL.Query().Get("v"), 10, 64)
if err != nil {
*scr <- scrapeResult{Error: err}
return
}
var body []byte
if err := backoff.RetryNotify(func() error {
resp, err := c.Do(req)
if err != nil {
return backoff.Permanent(err)
}
defer resp.Body.Close()
body, err = io.ReadAll(resp.Body)
if err != nil {
return backoff.Permanent(err)
}
if resp.StatusCode == http.StatusForbidden && string(body) == "error code: 1010" {
c.RandomizeUserAgent()
return fmt.Errorf("blocked by Cloudflare, status code: '%d', body: '%s'", resp.StatusCode, string(body))
} else if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to get url: '%s', status code: '%d', body: '%s'", url, resp.StatusCode, string(body))
}
if string(body) == "Too many requests." {
return fmt.Errorf("request rate limit reached")
}
return nil
}, backoff.NewExponentialBackOff(), func(err error, td time.Duration) {
fmt.Printf("[ERROR]: %v\n", err)
fmt.Printf("[WAIT_IN_SECONDS]: %v\n", 2*td.Seconds())
time.Sleep(td)
}); err != nil {
*scr <- scrapeResult{Error: err}
return
}
doc, err := utils.GenerateDocument(body)
if err != nil {
*scr <- scrapeResult{Error: err}
return
}
res := scrape(int(view), doc)
if res.Error != nil {
*scr <- scrapeResult{Error: res.Error}
return
}
fmt.Printf("[SUCCESS]: %v\n", url)
*scr <- *res
}