-
Notifications
You must be signed in to change notification settings - Fork 3
/
register.go
90 lines (83 loc) · 2.07 KB
/
register.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
)
type Register struct {
ID *string
Date *string
}
func (cmd *Register) Name() string { return "register" }
func (cmd *Register) Synopsis() string { return "Register for a class" }
func (cmd *Register) DefineFlags(fs *flag.FlagSet) {
cmd.ID = fs.String("id", "", "Class ID")
cmd.Date = fs.String("date", "", "Class date")
}
func (cmd *Register) Run() {
log.SetOutput(LogOutput())
if *cmd.ID == "" {
fmt.Println("Must provide classid.")
return
}
if *cmd.Date == "" {
fmt.Println("Must provide class date.")
return
}
// Load session
mboSession, err := LoadMBOSession()
if err != nil {
fmt.Println(err)
return
}
cookieJar, _ := cookiejar.New(nil)
client := &http.Client{Jar: cookieJar}
mbo_url, _ := url.Parse(MBO_URL)
client.Jar.SetCookies(mbo_url, mboSession.Cookies)
// Load the reservation page
// This contains the full POST URL we need to hit
preRegURL := fmt.Sprintf("%s/ASP/res_a.asp?classId=%s&classDate=%s",
MBO_URL, *cmd.ID, *cmd.Date)
log.Println("Getting", preRegURL)
resp, err := client.Get(preRegURL)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
contentsb, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
contents := string(contentsb)
// search for call to submitResForm()--this contains full URL for the POST to register
re := regexp.MustCompile("submitResForm\\('res_deb\\.asp\\?(.*)',")
submatch := re.FindStringSubmatch(contents)
log.Println("submatch", submatch)
if len(submatch) == 0 {
log.Println(re)
log.Println(contents)
fmt.Println("Session expired, please log in again.")
return
}
regURL := fmt.Sprintf("%s/ASP/res_deb.asp?%s", MBO_URL, submatch[1])
log.Println("posting to", regURL)
resp, err = client.PostForm(regURL, url.Values{})
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Println(resp)
fmt.Println("Unknown error")
return
}
fmt.Println("Successfully registered for class")
}