-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
53 lines (41 loc) · 1.3 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
package main
import (
"os"
"strings"
"time"
)
// SearchInput is a representation of the input request
type SearchInput struct {
Departure string
Arrival string
Date string
}
const dataDir = "flight-data/"
func init() {
if _, err := os.Stat(dataDir); os.IsNotExist(err) {
err = os.Mkdir(dataDir, os.ModeDir|0755)
LogFatalAndExitIfNotNull(err)
}
}
func main() {
searchInput, recipient := ParseFlags(os.Args[0], os.Args[1:])
url := "https://www.kayak.it/flights/" + searchInput.Departure + "-" + searchInput.Arrival + "/" + searchInput.Date + "?sort=price_a&fs=stops=~0"
now := time.Now().Format("20060102-150405")
LogInfo("Fetching results...")
doc := FetchAndStorePage(url, dataDir+now+"_response.html")
LogInfo("Parsing results...")
results := ParseKayakResults(doc)
if len(results) == 0 {
LogWarning("No results parsed.")
os.Exit(0)
}
LogInfo("Storing JSON results...")
StoreJSONResults(results, dataDir+now+"_results.json")
LogInfo("Storing JSON queries...")
StoreJSONQueries(searchInput, recipient, dataDir+now+"_results.json")
LogInfo("Building results template...")
mailTemplate := BuildResultsHTMLTemplate(results, searchInput)
LogInfo("Sending results...")
SendEmailNotification(&SMTPServer{}, strings.Split(recipient, ","), mailTemplate, searchInput)
LogInfo("Done.")
}