-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (88 loc) · 2.86 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
//
// Program scrapes for covid availability in MA state run facilities.
//
package main
import (
"os"
"io/ioutil"
"strings"
"net/http"
"log"
"github.com/PuerkitoBio/goquery"
"gopkg.in/yaml.v2"
)
func LookupSiteAvailability(vaccineURL string, vaccineSites map[string]bool) (sucess, foundAvailableSite bool) {
foundAvailableSite = false
response, err := http.Get(vaccineURL)
if err != nil {
log.Fatal(err)
return false, false
}
defer response.Body.Close()
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Fatal(err)
return false, false
}
doc.Find(".table tr").Each(func(i int, rowNode *goquery.Selection){
for key, _ := range vaccineSites {
siteLocation := rowNode.Find("a").Text()
if strings.Contains(siteLocation, key) {
rowNode.Find(".text").Each(func(j int, columnNode *goquery.Selection){
if strings.Contains(columnNode.Text(), "Available") {
vaccineSites[key] = true
foundAvailableSite = true
}
})
}
}
})
return true, foundAvailableSite
}
type ProgConf struct {
LogFile string `yaml:"logFile"`
VaccineURL string `yaml:"vaccineURL"`
SiteTargets []string `yaml:"siteTargets,flow"`
EmailTargets []string `yaml:"emailTargets,flow"`
EmailHost string `yaml:"emailHost"`
EmailFrom string `yaml:"emailFrom"`
EmailPassword string `yaml:"emailPassword"`
EmailPort string `yaml:"emailPort"`
}
func ReadConfigurationFile(filepath string, conf *ProgConf) (bool) {
yamlFile, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal(err)
return false
}
err = yaml.Unmarshal(yamlFile, conf)
if err != nil {
log.Fatal(err)
return false
}
return true
}
func main() {
// Get program vars
var conf ProgConf
ReadConfigurationFile(os.Args[1], &conf)
// Set up logging to file
f, err := os.OpenFile(conf.LogFile, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
siteData := make(map[string]bool)
for i := 0; i < len(conf.SiteTargets); i++ {
siteData[conf.SiteTargets[i]] = false
}
sucess, foundSite := LookupSiteAvailability(conf.VaccineURL, siteData)
if sucess && foundSite {
log.Println("Found a valid site")
emailVars := EmailVars{conf.EmailHost, conf.EmailFrom, conf.EmailTargets, conf.EmailPassword, conf.EmailPort}
SendEmailSMTP(GenerateEmailBody(siteData), emailVars )
} else {
log.Println("No valid sites found.")
}
}