-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
452 lines (349 loc) · 11.8 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"github.com/google/go-cmp/cmp"
"github.com/yhat/scrape"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
gomail "gopkg.in/gomail.v2"
)
type Dualis struct {
Client *http.Client
Semester []Semester
}
type Config struct {
Username string
Password string
SMTPHost string
SMTPPort int
SMTPUsername string
SMTPPassword string
NotificationRecipient string
UpdateIntervalMinutes int
}
type Semester struct {
Name string
Url string
Modules []Module
}
type Module struct {
Name string
Url string
Attempts []Attempt
}
type Attempt struct {
Label string
Events []Event
}
type Event struct {
Name string
Grade string // Yep, we will use strings because fuck Dualis
Exams []Exam
}
type Exam struct {
Semester string
Name string
Grade string
}
const (
DEBUG = true
baseURL = "https://dualis.dhbw.de/"
loginPath = "scripts/mgrqcgi?APPNAME=CampusNet&PRGNAME=EXTERNALPAGES&ARGUMENTS=-N000000000000001,-N000324,-Awelcome"
loginScriptPath = "/scripts/mgrqcgi"
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
)
func main() {
// Load config file
config, _ := parseConfig("config.json")
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return nil
},
Timeout: time.Duration(10 * time.Second),
}
dualis := Dualis{
Client: client,
}
homeUrl, _ := dualis.login(config.Username, config.Password)
dualis.initStructs(homeUrl)
dualis.pollGrades(config)
}
func (dualis *Dualis) pollGrades(config *Config) {
log.Printf("Scheduled polling for grades (Every %v minute(s)).\n", config.UpdateIntervalMinutes)
for {
time.Sleep(time.Duration(config.UpdateIntervalMinutes) * time.Minute)
log.Println("Polling for new grades.")
updatedModules := dualis.updateModules()
if len(updatedModules) > 0 {
log.Println("No new grades discovered.")
dualis.sendNotification(&updatedModules, config)
}
}
}
func (dualis *Dualis) sendNotification(modules *[]Module, config *Config) {
log.Printf("Sending notification for %v modules.\n", len(*modules))
tpl, err := template.ParseFiles("notification.tpl")
if err != nil {
panic(err)
}
var body bytes.Buffer
err = tpl.Execute(&body, modules)
if err != nil {
panic(err)
}
m := gomail.NewMessage()
m.SetHeader("From", config.SMTPUsername)
m.SetHeader("To", config.NotificationRecipient)
m.SetHeader("Subject", "New grades available!")
m.SetBody("text/html", body.String())
d := gomail.NewDialer(config.SMTPHost, config.SMTPPort, config.SMTPUsername, config.SMTPPassword)
if err := d.DialAndSend(m); err != nil {
panic(err)
}
}
func (dualis *Dualis) updateModules() (updatedModules []Module) {
for i, _ := range dualis.Semester {
for j, _ := range dualis.Semester[i].Modules {
var diffModule Module
diffModule = dualis.Semester[i].Modules[j]
newModule, _ := dualis.parseModule(&dualis.Semester[i].Modules[j])
dualis.Semester[i].Modules[j] = *newModule
if !reflect.DeepEqual(diffModule, dualis.Semester[i].Modules[j]) {
updatedModules = append(updatedModules, dualis.Semester[i].Modules[j])
log.Println("Found update for module:", dualis.Semester[i].Modules[j].Name)
}
}
}
return updatedModules
}
func parseConfig(filename string) (cfg *Config, ok bool) {
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal("Could not open config file.")
return nil, false
}
var config = Config{}
err = json.Unmarshal(file, &config)
if err != nil {
fmt.Print(err)
log.Fatal("Could not decode config file.")
return nil, false
}
return &config, true
}
func (dualis *Dualis) initStructs(homeUrl *url.URL) {
req, _ := http.NewRequest("GET", baseURL+homeUrl.String(), nil)
req.Header.Add("User-Agent", userAgent)
resp, _ := dualis.Client.Do(req)
navElementMatcher := func(n *html.Node) bool {
return scrape.Attr(n, "title") == "Prüfungsergebnisse"
}
root, _ := html.Parse(resp.Body)
htmlNavElement, _ := scrape.Find(root, navElementMatcher)
htmlNavLink, _ := scrape.Find(htmlNavElement, scrape.ByTag(atom.A))
dualis.discoverSemesters(scrape.Attr(htmlNavLink, "href"))
for i, semester := range dualis.Semester {
dualis.discoverModules(&semester)
dualis.Semester[i] = semester
}
}
func (dualis *Dualis) discoverModules(semester *Semester) {
req, _ := http.NewRequest("GET", baseURL+semester.Url, nil)
req.Header.Add("User-Agent", userAgent)
resp, _ := dualis.Client.Do(req)
root, _ := html.Parse(resp.Body)
htmlModuleTable, _ := scrape.Find(root, scrape.ByClass("nb"))
htmlModuleLinks := scrape.FindAll(htmlModuleTable, scrape.ByTag(atom.A))
for _, htmlModuleLink := range htmlModuleLinks {
module := Module{
Url: scrape.Attr(htmlModuleLink, "href"),
}
semester.Modules = append(semester.Modules, module)
}
log.Printf("Discovered %v new modules in semester: %s", len(semester.Modules), semester.Name)
}
func (dualis *Dualis) discoverSemesters(url string) {
req, _ := http.NewRequest("GET", baseURL+url, nil)
req.Header.Add("User-Agent", userAgent)
resp, _ := dualis.Client.Do(req)
root, _ := html.Parse(resp.Body)
semesterMatcher := func(n *html.Node) bool {
return n.DataAtom == atom.Option
}
htmlSemesterSelect, _ := scrape.Find(root, scrape.ById("semester"))
semesterBaseUrl := dualis.buildSemesterUrl(scrape.Attr(htmlSemesterSelect, "onchange"))
for _, htmlSemester := range scrape.FindAllNested(htmlSemesterSelect, semesterMatcher) {
semester := Semester{
Name: scrape.Text(htmlSemester),
Url: semesterBaseUrl + scrape.Attr(htmlSemester, "value"),
}
dualis.Semester = append(dualis.Semester, semester)
log.Println("Discovered new semester:", semester.Name)
}
}
func (dualis *Dualis) buildSemesterUrl(dirt string) (url string) {
regex := regexp.MustCompile(`(?:')(.*?)(?:')`)
var params []string
for _, match := range regex.FindAllStringSubmatch(dirt, -1) {
params = append(params, match[1])
}
url = params[0] + "?APPNAME=" + params[1] + "&PRGNAME=" + params[2] + "&ARGUMENTS=-N" + params[3] + ",-N" + params[4] + "," + params[5]
return url
}
func (dualis *Dualis) login(username, password string) (homeUrl *url.URL, ok bool) {
postData := url.Values{"usrname": {username},
"pass": {password},
"APPNAME": {"CampusNet"},
"PRGNAME": {"LOGINCHECK"},
"ARGUMENTS": {"clino,usrname,pass,menuno,menu_type,browser,platform"},
"clino": {"000000000000001"},
"menuno": {"000324"},
"menu_type": {"classic"},
"browser": {""},
"platform": {""},
}
req, _ := http.NewRequest("POST", baseURL+loginScriptPath, strings.NewReader(postData.Encode()))
req.Header.Add("User-Agent", userAgent)
resp, _ := dualis.Client.Do(req)
if len(resp.Header.Get("REFRESH")) == 0 {
log.Fatalln("Could not log in. Check credentials.")
var u *url.URL
return u, false
} else {
log.Println("Login successful. Following 1st startup redirect.")
}
_, ok = dualis.sessionCookie(resp)
if !ok {
log.Fatal("No session cookie configured.")
var u *url.URL
return u, false
}
u, _ := url.Parse("https://dualis.dhbw.de")
dualis.Client.Jar.SetCookies(u, resp.Cookies())
refreshUrl, _ := dualis.cleanRefreshURL(resp.Header.Get("REFRESH"))
req, _ = http.NewRequest("GET", baseURL+refreshUrl.String(), nil)
req.Header.Add("User-Agent", userAgent)
resp, _ = dualis.Client.Do(req)
root, _ := html.Parse(resp.Body)
elem, ok := scrape.Find(root, func(n *html.Node) bool {
return n.DataAtom == atom.Meta && n.Attr[0].Key == "http-equiv" && n.Attr[0].Val == "refresh"
})
if !ok {
log.Fatalln("Could not find 2nd startup redirect link.")
var u *url.URL
return u, false
}
redirectUrl, _ := dualis.cleanRefreshURL(elem.Attr[1].Val)
log.Println("Found 2nd redirect link. Home successfully discovered.")
return redirectUrl, true
}
func (module *Module) equal(b *Module) (same bool) {
return cmp.Equal(module, b)
}
func (dualis *Dualis) sessionCookie(resp *http.Response) (cookie string, ok bool) {
if len(resp.Cookies()) > 0 && resp.Cookies()[0].Name == "cnsc" {
log.Printf("Session cookie created (%s).\n", resp.Cookies()[0].Value)
return resp.Cookies()[0].Value, true
} else {
log.Println("Session cookie not created.")
return resp.Cookies()[0].Value, false
}
}
func (dualis *Dualis) cleanRefreshURL(dirt string) (cleanURL *url.URL, ok bool) {
regex, _ := regexp.Compile(`\bURL=(.*)`)
// This is probably the worst way of finding the correct string - TODO
match := regex.FindStringSubmatch(dirt)
cleanURL, error := url.Parse(match[1])
if error != nil {
var u *url.URL
return u, false
}
return cleanURL, true
}
func (dualis *Dualis) parseModule(module *Module) (mod *Module, ok bool) {
url := module.Url
req, _ := http.NewRequest("GET", baseURL+url, nil)
req.Header.Add("User-Agent", userAgent)
resp, _ := dualis.Client.Do(req)
root, _ := html.Parse(resp.Body)
rowMatcher := func(n *html.Node) bool {
return n.DataAtom == atom.Tr
}
columnMatcher := func(n *html.Node) bool {
return n.DataAtom == atom.Td
}
moduleNameMatcher := func(n *html.Node) bool {
return n.DataAtom == atom.H1
}
htmlRows := scrape.FindAll(root, rowMatcher)
htmlModuleName, _ := scrape.Find(root, moduleNameMatcher)
module.Name = strings.Replace(scrape.Text(htmlModuleName), "\n", "", -1)
// Reset module
module.Attempts = []Attempt{}
log.Println("===============================================================")
log.Println("MODULE: ", module.Name)
log.Println("===============================================================")
processingEvent := false
ProcessRows:
for _, row := range htmlRows {
htmlColumns := scrape.FindAll(row, columnMatcher)
//fmt.Println(scrape.Text(row))
switch scrape.Attr(htmlColumns[0], "class") {
case "level01":
log.Printf("┌ Attempt: %s\n", scrape.Text(htmlColumns[0]))
attempt := Attempt{
Label: scrape.Text(htmlColumns[0]),
}
module.Attempts = append(module.Attempts, attempt)
case "level02":
if processingEvent && len(htmlColumns) > 1 && scrape.Attr(htmlColumns[1], "class") != "level02 level02_chkbox_workaround_mpa" {
log.Printf("└ Event result: %s\n", scrape.Text(htmlColumns[3]))
processingEvent = false
// Intoducing these variables to combat heavy nesting of slice keys
//attempt := module.Attempts[len(module.Attempts)-1]
//event := attempt.Events[len(attempt.Events)-1]
//event.Grade = scrape.Text(htmlColumns[3])
module.Attempts[len(module.Attempts)-1].Events[len(module.Attempts[len(module.Attempts)-1].Events)-1].Grade = scrape.Text(htmlColumns[3])
} else {
log.Printf("├┬ New event: %s\n", scrape.Text(htmlColumns[0]))
processingEvent = true
event := Event{
Name: scrape.Text(htmlColumns[0]),
}
module.Attempts[len(module.Attempts)-1].Events = append(module.Attempts[len(module.Attempts)-1].Events, event)
}
case "tbdata":
log.Printf("│├─ Exam: %s\n", scrape.Text(htmlColumns[1]))
// Intoducing these variables to combat heavy nesting of slice keys
//attempt := module.Attempts[len(module.Attempts)-1]
//event := attempt.Events[len(attempt.Events)-1]
exam := Exam{
Semester: scrape.Text(htmlColumns[0]),
Name: scrape.Text(htmlColumns[1]),
Grade: scrape.Text(htmlColumns[3]),
}
//event.Exams = append(event.Exams, exam)
module.Attempts[len(module.Attempts)-1].Events[len(module.Attempts[len(module.Attempts)-1].Events)-1].Exams = append(module.Attempts[len(module.Attempts)-1].Events[len(module.Attempts[len(module.Attempts)-1].Events)-1].Exams, exam)
case "tbhead":
if scrape.Text(htmlColumns[0]) == "Pflichtbereich" {
break ProcessRows
}
}
}
processingEvent = false
return module, true
}