-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
168 lines (131 loc) · 3.39 KB
/
controller.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
"gopkg.in/redis.v5"
)
const baseUrl string = "http://www.ace.utoronto.ca/bookings/f?p=200:3:0::NO::"
func fetch(url string) (*http.Response, error) {
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
// TODO: Figure out how to this without hard-coding a cookie value
req.AddCookie(&http.Cookie{
Name: "WWV_CUSTOM-F_1410000632844518_200",
Value: "845B97E883105AC19173D1B9E65DE4B4",
})
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func scrapeBuildingRooms(client *redis.Client, building *Building) error {
resp, err := fetch(fmt.Sprintf("%sP3_BLDG:%s", baseUrl, building.Code))
if err != nil {
return err
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return err
}
// Get building name
building.Name = strings.TrimSpace(strings.TrimLeft(
doc.Find("select#P3_BLDG option[selected=\"selected\"]").Text(),
building.Code))
// Get list of rooms for this building
var rooms []string
doc.Find("select#P3_ROOM option").Each(func(i int, s *goquery.Selection) {
value, exists := s.Attr("value")
if !exists || value == "%null%" {
return
}
rooms = append(rooms, value)
})
building.Rooms = make([]Room, len(rooms))
var wg sync.WaitGroup
wg.Add(len(rooms))
for i, roomNumber := range rooms {
go func(i int, roomNumber string) {
defer wg.Done()
room := Room{Number: roomNumber}
scrapeSingleRoom(client, building.Code, &room)
building.Rooms[i] = room
}(i, roomNumber)
}
wg.Wait()
return nil
}
func scrapeSingleRoom(client *redis.Client, buildingCode string, room *Room) error {
key := fmt.Sprintf("calendar:%s:%s", buildingCode, room.Number)
val, err := client.Get(key).Result()
if err != nil && err != redis.Nil {
return err
}
if err != redis.Nil {
err := json.Unmarshal([]byte(val), &room.Schedule)
if err != nil {
return err
}
return nil
}
resp, err := fetch(fmt.Sprintf("%sP3_BLDG,P3_ROOM:%s,%s", baseUrl, buildingCode, room.Number))
if err != nil {
return err
}
doc, err := goquery.NewDocumentFromResponse(resp)
if err != nil {
return err
}
dateMap := make(map[string][]Booking)
var dates []string
doc.Find("table.t3WeekCalendarAlternative1").Find("td").Each(func(i int, s *goquery.Selection) {
if s.HasClass("t3Hour") {
return
}
rawDate, exists := s.Find("input[type=\"hidden\"]").Attr("value")
if !exists {
return
}
date := rawDate[:8]
// Remove seconds (190000 -> 1900)
time := rawDate[8 : len(rawDate)-2]
if time == "0000" {
return
}
text := strings.TrimSpace(s.Find("div#apex_cal_data_grid_src").Text())
// Replace multiple spaces with single space
text = regexp.MustCompile(`[\n\r\s]+`).ReplaceAllString(text, " ")
dateMap[date] = append(dateMap[date], Booking{
Time: time,
Description: text,
})
dates = append(dates, date)
})
sort.Strings(dates)
for date, bookings := range dateMap {
room.Schedule = append(room.Schedule, Date{date, bookings})
}
b, err := json.Marshal(room.Schedule)
if err != nil {
return err
}
err = client.Set(key, b, time.Hour*4).Err()
if err != nil {
return err
}
return nil
}