-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
197 lines (166 loc) · 4.24 KB
/
main.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
package main
import (
"encoding/csv"
"fmt"
"log"
"net/mail"
"os"
"strconv"
"time"
"github.com/gocolly/colly"
"github.com/spf13/viper"
gomail "gopkg.in/mail.v2"
)
func main() {
headers := []string{}
allStocksSlice := [][]string{}
c := colly.NewCollector(
colly.UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"),
colly.AllowedDomains("finance.yahoo.com"),
colly.MaxBodySize(0),
colly.AllowURLRevisit(),
colly.Async(true),
)
// Set max Parallelism and introduce a Random Delay
c.Limit(&colly.LimitRule{
DomainGlob: "*",
Parallelism: 2,
Delay: 500 * time.Millisecond,
})
c.OnRequest(func(r *colly.Request) {
log.Println("Visiting", r.URL.String())
})
c.OnHTML("thead", func(e *colly.HTMLElement) {
e.ForEach("tr", func(_ int, el *colly.HTMLElement) {
el.ForEach("th", func(_ int, el *colly.HTMLElement) {
//ignore 52 Week Range
if el.Text != "52 Week Range" {
headers = append(headers, el.Text)
}
})
})
})
c.OnHTML("tbody", func(e *colly.HTMLElement) {
e.ForEach("tr", func(_ int, el *colly.HTMLElement) {
dataSlice := []string{}
el.ForEach("td", func(_ int, el *colly.HTMLElement) {
dataSlice = append(dataSlice, el.Text)
})
//remove the last element which is the empty string
dataSlice = dataSlice[:len(dataSlice)-1]
//add to overall slice
allStocksSlice = append(allStocksSlice, dataSlice)
})
})
c.Visit("https://finance.yahoo.com/most-active/")
c.Wait()
printData(allStocksSlice, headers)
saveDataToFile(allStocksSlice, headers)
//ask user if they want to send email after checking if a config file exists
if _, err := os.Stat("config.email.env"); os.IsNotExist(err) {
noFileFound()
} else {
fmt.Println("Do you want to send email? (y/n)")
var input string
fmt.Scanln(&input)
if input == "y" {
sendToEmail("Daily_Actives.csv")
} else {
fmt.Println("Email not sent")
}
}
}
// helper functions
func saveDataToFile(data [][]string, headers []string) {
fmt.Println("Saving data to file... Removing old file if exists")
//delete file if exists
os.Remove("Daily_Actives.csv")
//open file
file, err := os.Create("Daily_Actives.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
//write to file
writer := csv.NewWriter(file)
//write headers
writer.Write(headers)
writer.Flush()
for _, value := range data {
err := writer.Write(value)
if err != nil {
log.Fatal(err)
}
}
writer.Flush()
fmt.Println("Data saved to file")
}
func printData(data [][]string, headers []string) {
//print headers
fmt.Println("Printing")
fmt.Println(headers)
for _, value := range data {
fmt.Println(value)
}
}
func LoadConfig(path string) (config EmailConfig, err error) {
viper.SetConfigName("config.email")
viper.AddConfigPath(path)
err = viper.ReadInConfig()
if err != nil {
return config, err
}
err = viper.Unmarshal(&config)
if err != nil {
return config, err
}
return config, err
}
func validateEmail(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil
}
func sendToEmail(fileName string) {
//send email
config, err := LoadConfig(".")
if err != nil {
log.Fatal(err)
}
//validate email
if !validateEmail(config.EMAIL) {
fmt.Println("Invalid email not sending")
return
}
//create email
m := gomail.NewMessage()
m.SetHeader("From", config.EMAIL)
m.SetHeader("To", config.EMAIL)
m.SetHeader("Subject", "Daily Actives")
m.SetBody("text/html", "Daily Actives")
m.Attach(fileName)
//convert string to int for SMTP_PORT
port, err := strconv.Atoi(config.SMTP_PORT)
if err != nil {
log.Fatal(err)
return
}
//create dialer
d := gomail.NewDialer(config.SMTP_HOST, port, config.EMAIL, config.PASSWORD)
//send email
if err := d.DialAndSend(m); err != nil {
panic(err)
}
fmt.Println("Email sent")
}
func noFileFound() {
fmt.Println("No config file found...")
fmt.Println("if you want to send an email..")
//wait 2 seconds
time.Sleep(1 * time.Second)
fmt.Println("Please create a config file named config.email.env with the following format..")
time.Sleep(1 * time.Second)
fmt.Println("EMAIL=EMAIL_ADDRESS")
fmt.Println("PASSWORD=PASSWORD")
fmt.Println("SMTP_HOST=SMTP_HOST")
fmt.Println("SMTP_PORT=SMTP_PORT")
}