-
Notifications
You must be signed in to change notification settings - Fork 3
/
schedule.go
102 lines (93 loc) · 2.51 KB
/
schedule.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
package main
import (
"bytes"
"flag"
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"regexp"
"strings"
"text/tabwriter"
"time"
)
type Schedule struct {
}
func (cmd *Schedule) Name() string { return "schedule" }
func (cmd *Schedule) Synopsis() string { return "Show your schedule" }
func (cmd *Schedule) DefineFlags(fs *flag.FlagSet) {
}
func (cmd *Schedule) Run() {
log.SetOutput(LogOutput())
// 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)
// Get schedule
resp, err := client.Get(fmt.Sprintf("%s/ASP/my_sch.asp", MBO_URL))
if err != nil || resp.StatusCode != 200 {
log.Println(err)
log.Println(resp)
fmt.Println("Error getting schedule")
}
defer resp.Body.Close()
// Read into buffer so it can be read multiple times
var body bytes.Buffer
if _, err := body.ReadFrom(resp.Body); err != nil {
fmt.Println(err)
return
}
// Detect logged in welcome message
if !IsLoggedIn(body.Bytes()) {
fmt.Println("Session has expired; please log in again.")
return
}
// Format in space-separated columns of minimal width 5 and at least
// one blank of padding (so wider column entries do not touch each other)
w := new(tabwriter.Writer)
w.Init(os.Stdout, 5, 0, 1, ' ', 0)
header := []string{
"Day",
"Time",
"Class",
"Coach",
"Visit ID",
}
fmt.Fprintln(w, strings.Join(header, "\t"))
doc, _ := goquery.NewDocumentFromReader(bytes.NewReader(body.Bytes()))
doc.Find("#mySchTable tr.scheduleRow").Each(func(i int, s *goquery.Selection) {
day, err := time.Parse("Mon 1/2/2006", Strip(s.Find("td.dateCell").Text()))
if err != nil {
log.Println("Could not parse time from", Strip(s.Find("td.dateCell").Text()))
}
// Find Visit ID in cancel cell
// <a href="javascript:jsConfirm(44270, 1)">Cancel</a>
visitID := ""
html, _ := s.Find("td.cancelCell").Html()
re := regexp.MustCompile("jsConfirm\\((\\d+),")
submatch := re.FindStringSubmatch(html)
log.Println("submatch", submatch)
if len(submatch) == 0 {
log.Println("could not find visit id", re, html)
} else {
visitID = submatch[1]
}
fmt.Fprintln(w, strings.Join([]string{
day.Format("Mon Jan 2"),
Strip(s.Find("td.timeCell").Text()),
Strip(s.Find("td.classNameCell").Text()),
Strip(s.Find("td.teacherCell").Text()),
visitID,
}, "\t"))
})
w.Flush()
}