-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.go
237 lines (201 loc) · 4.81 KB
/
crawler.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
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"context"
"fmt"
"math"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/chromedp/chromedp"
"github.com/faiface/beep"
"github.com/urfave/cli/v2"
)
var _ beep.Buffer
// Our crawler structure definition
type Crawler struct {
ctx *cli.Context
alertThreshold int
stocks map[string][]float64
m sync.Mutex
}
func(crawler *Crawler) lock() {
log.Debug("Aqcuire lock")
crawler.m.Lock()
}
func(crawler *Crawler) unlock() {
log.Debug("Release lock")
crawler.m.Unlock()
}
func NewCrawler(c *cli.Context, stocks ...string) *Crawler {
crawler := &Crawler{ctx: c,}
crawler.stocks = make(map[string][]float64)
for _, s := range stocks {
crawler.stocks[strings.Title(s)] = make([]float64, 0)
}
threshold := c.Int("threshold")
if threshold != 0 {
crawler.alertThreshold = threshold
}
return crawler
}
func fetchPriceFromGoogle(q string, res chan<- string) {
log.WithFields(log.Fields{
"Stock": q,
}).Debug("Fetch stock price")
url := "https://www.google.com"
inQ := q + " stock price"
inTextSel := `//input[@name='q']`
btnSel := `input[name="btnK"]`
outTextSel := `//span[@jsname="vWLAgc"]`
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// Wait for timeout.
timeoutContext, _ := context.WithTimeout(ctx, 20 * time.Second)
// run task list
var result string
err := chromedp.Run(
timeoutContext,
chromedp.Navigate(url),
chromedp.WaitVisible(inTextSel),
chromedp.SendKeys(inTextSel, inQ),
chromedp.Click(btnSel, chromedp.ByQuery),
chromedp.WaitVisible(outTextSel),
chromedp.Text(outTextSel, &result),
)
if err != nil {
log.WithFields(log.Fields{
"Stock": strings.Title(q),
"Error": err,
}).Error("Error scrapping stock price")
res <- ""
return
}
re := regexp.MustCompile("\\n")
res <- re.ReplaceAllString(strings.Title(q) + ": " + result, " ")
}
func (crawler *Crawler) spawnScrapers(res chan<- string) {
crawler.lock()
defer crawler.unlock()
for s, _ := range crawler.stocks {
go fetchPriceFromGoogle(s, res)
}
}
func (crawler *Crawler) scrapStockPrices(done chan<- bool) {
crawler.ppCmd()
res := make(chan string)
crawler.spawnScrapers(res)
crawler.lock()
defer crawler.unlock()
stkColl := make([]string, 0)
for i := 0; i < len(crawler.stocks); i++ {
select {
case val := <-res:
if val != "" {
stkColl = append(stkColl, val)
}
}
}
sort.Strings(stkColl)
for _, val := range stkColl {
log.Info(val)
}
done <- true
}
func (crawler *Crawler) analyze(val string, wg *sync.WaitGroup) {
log.WithFields(log.Fields{
"Stock": val,
}).Debug("Analyze stock")
crawler.lock()
defer crawler.unlock()
defer wg.Done()
valSlice := strings.Split(val, ":")
stock := valSlice[0]
temp := strings.Replace(strings.TrimSpace(valSlice[1]), ",", "", -1)
stockPrice, err := strconv.ParseFloat(temp, 32)
if err != nil {
log.Fatalf("Error converting string %s to float: %v", temp, err)
}
crawler.stocks[stock] = append(crawler.stocks[stock], stockPrice)
incCt := 0
decCt := 0
length := len(crawler.stocks[stock])
for idx := length-1; idx > 0; idx-- {
if crawler.stocks[stock][idx-1] >= crawler.stocks[stock][idx] {
break;
}
incCt++;
}
for idx := length-2; idx >= 0; idx-- {
if crawler.stocks[stock][idx] <= crawler.stocks[stock][idx + 1] {
break
}
decCt++;
}
log.WithFields(log.Fields{
"Stock": stock,
"incCt": incCt,
"decCt": decCt,
}).Debug()
incMaxInt := int(math.Max(float64(incCt), float64(crawler.alertThreshold)))
decMaxInt := int(math.Max(float64(decCt), float64(crawler.alertThreshold)))
if incCt >= crawler.alertThreshold {
log.WithFields(log.Fields{
"Stock": stock,
"Interval": incMaxInt,
"Prices": crawler.stocks[stock][length-incMaxInt:],
}).Warn("Consistent upward movements\n")
}
if decCt >= crawler.alertThreshold {
log.WithFields(log.Fields{
"Stock": stock,
"Interval": decMaxInt,
"Prices": crawler.stocks[stock][length-decMaxInt:],
}).Warn("Consistent downward movements\n")
}
}
func (crawler *Crawler) monitor(done chan<- bool, exit <-chan os.Signal) {
crawler.ppCmd()
ticker := time.NewTicker(60 * time.Second)
go func() {
select {
case <-exit:
log.Info("Exit request, return.")
done <- true
return
}
} ()
for {
select {
case <-ticker.C:
res := make(chan string)
crawler.spawnScrapers(res)
crawler.lock()
length := len(crawler.stocks)
crawler.unlock()
stkColl := make([]string, 0)
for i := 0; i <length; i++ {
select {
case val := <-res:
if val != "" {
stkColl = append(stkColl, val)
}
}
}
sort.Strings(stkColl)
var wg sync.WaitGroup
for _, val := range stkColl {
log.Info(val)
wg.Add(1)
go crawler.analyze(val, &wg)
}
wg.Wait()
}
fmt.Println()
}
}