/
main.go
156 lines (130 loc) · 3.49 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
package main
import (
"encoding/csv"
"errors"
"github.com/yhat/scrape"
"golang.org/x/net/html"
"log"
"net/http"
"os"
"regexp"
"strconv"
"time"
)
type space struct {
Name, Status, Updated, Available string
}
type structure struct {
Name, URLCode string
Number int
Spaces []space
}
func main() {
c := make(chan structure)
go getStructures(c)
for i := range c {
writeData(i)
}
}
func getStructures(c chan<- structure) {
for i := 1; i < 9; i++ {
if i == 7 || i == 4 {
continue
}
s := structure{
Name: "Structure" + strconv.Itoa(i),
Number: i,
URLCode: strconv.Itoa(i + 88),
}
// Attempt the request a total of 5 times
var err error
for j := 0; j < 5; j++ {
s.Spaces, err = s.getSpaces()
if err != nil {
time.Sleep(time.Second * 5)
continue
}
break
}
if err != nil {
file, _ := os.OpenFile("/home/dsifford/Dropbox/ParkingData/errorlog.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
defer file.Close()
file.WriteString(err.Error())
}
c <- s
}
close(c)
}
func (s structure) getSpaces() ([]space, error) {
spaces := []space{
space{Name: "WSU Permit"},
space{Name: "Student OneCard"},
space{Name: "Visitor"},
}
re := map[string]*regexp.Regexp{
"avail": regexp.MustCompile(`([0-9]+|NONE)`),
"status": regexp.MustCompile(`(OPEN|CLOSED|FULL)`),
"updated": regexp.MustCompile(`(?P<a>^.+: )(?P<b>.+)`),
}
// Request
client := &http.Client{
Timeout: time.Second * 10,
}
req, err := http.NewRequest("GET", "http://m.wayne.edu/parking.php?location="+s.URLCode, nil)
if err != nil {
return spaces, errors.New("Request failed")
}
req.Header.Set("User-Agent", "Apple-iPhone6C1/")
// Response
resp, err := client.Do(req)
if err != nil {
return spaces, errors.New("Response failed")
}
defer resp.Body.Close()
body, err := html.Parse(resp.Body)
if err != nil {
return spaces, errors.New("Error parsing response body")
}
// Parse relevant response data
dataString, ok := scrape.Find(body, scrape.ByClass("available"))
if !ok {
return spaces, errors.New("Error: Line 105 - scrape.Find (available) -- not finding scrape info")
}
lastUpdated, ok := scrape.Find(body, scrape.ByClass("last_updated"))
if !ok {
return spaces, errors.New("Error: Line 109 - scrape.Find (last_updated) -- not finding scrape info")
}
avail := re["avail"].FindAllString(scrape.Text(dataString), -1)
if len(avail) == 0 {
avail = []string{"0", "0", "0"}
}
status := re["status"].FindAllString(scrape.Text(dataString), -1)
if len(status) != 3 {
return spaces, errors.New("Error: Line 118 - FindAllString (status) not returning 3 matches")
}
updated := re["updated"].FindStringSubmatch(scrape.Text(lastUpdated))
if len(updated) == 0 {
return spaces, errors.New("Error: Line 122 - FindAllStringSubmatch (updated) not finding a match")
}
for key := range spaces {
spaces[key].Available = avail[key]
spaces[key].Status = status[key]
spaces[key].Updated = updated[2]
}
return spaces, nil
}
func writeData(s structure) {
file, err := os.OpenFile("/home/dsifford/Dropbox/ParkingData/"+s.Name+".csv", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Fatalln("CSV file could not be created or opened:", err)
}
defer file.Close()
writer := csv.NewWriter(file)
if stat, _ := file.Stat(); stat.Size() == 0 {
writer.Write([]string{"Updated", "Type", "Status", "Spaces Available"})
}
for _, sp := range s.Spaces {
writer.Write([]string{sp.Updated, sp.Name, sp.Status, sp.Available})
}
writer.Flush()
}