This repository has been archived by the owner on Mar 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
143 lines (123 loc) · 3.5 KB
/
term.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
package uwquest
import (
"errors"
"fmt"
"net/http"
gq "github.com/PuerkitoBio/goquery"
ess "github.com/unixpickle/essentials"
)
// A Term represents a UW school term.
type Term struct {
Index int
Name string
Career string
Institution string
}
func (t *Term) String() string {
return fmt.Sprintf("Term{Index: %d, Name: %s, Career: %s, Institution: %s}",
t.Index, t.Name, t.Career, t.Institution)
}
// Terms fetches all the terms that a student has been enrolled for.
func (c *Client) Terms() ([]*Term, error) {
res, err := c.Session.Get(GradesURL)
if err != nil {
return nil, ess.AddCtx("uwquest: fetching grades page", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("uwquest: got non-200 status code while fetching "+
"grades page: got code %d", res.StatusCode)
}
defer res.Body.Close()
// Scrape response for data in the terms table.
doc, err := gq.NewDocumentFromReader(res.Body)
if err != nil {
return nil, ess.AddCtx("parsing response body with goquery", err)
}
sel := doc.Find(`#SSR_DUMMY_RECV1\$scroll\$0`).Children()
if sel.Length() != 1 {
return nil, errors.New("could not locate terms table")
}
terms, err := parseTerms(sel)
if err != nil {
return nil, ess.AddCtx("uwquest: parsing terms", err)
}
err = res.Body.Close()
return terms, ess.AddCtx("uwquest: closing response body", err)
}
// TermsWithSchedule fetches the study terms for which Quest has course
// schedules available.
func (c *Client) TermsWithSchedule() ([]*Term, error) {
res, err := c.Session.Get(SchedulesURL)
if err != nil {
return nil, ess.AddCtx("uwquest: fetching course schedule page", err)
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("uwquest: got non-200 status code while fetching "+
"schedule page: got code %d", res.StatusCode)
}
defer res.Body.Close()
// Scrape response for data in the terms table.
doc, err := gq.NewDocumentFromReader(res.Body)
if err != nil {
return nil, ess.AddCtx("parsing response body with goquery", err)
}
sel := doc.Find(`#SSR_DUMMY_RECV1\$scroll\$0`).Children().Find("tbody")
if sel.Length() != 1 {
return nil, errors.New("could not locate terms table")
}
terms, err := parseTerms(sel)
if err != nil {
return nil, ess.AddCtx("uwquest: parsing terms", err)
}
err = res.Body.Close()
return terms, ess.AddCtx("uwquest: closing response body", err)
}
func parseTerms(tableBody *gq.Selection) ([]*Term, error) {
var (
terms []*Term
err error
)
tableBody.Children().EachWithBreak(func(i int, row *gq.Selection) bool {
if _, ok := row.Attr("id"); !ok {
return true // continue
}
var term *Term
if term, err = parseTermRow(row); err != nil {
ess.AddCtxTo(fmt.Sprintf("row %d", i), &err)
return false
}
terms = append(terms, term)
return true
})
if err != nil {
return nil, ess.AddCtx("parsing terms table", err)
}
return terms, nil
}
func parseTermRow(row *gq.Selection) (*Term, error) {
var (
t = new(Term)
id, ok = row.Attr("id")
)
if !ok {
return nil, errors.New("row does not contain an 'id' attribute")
}
t.Index = int(id[len(id)-1]-'0') - 1
var (
scraper = indexedScraper{Index: t.Index, Sel: row}
sel, err = scraper.Find("TERM_CAR", "term name")
)
if err != nil {
return nil, err
}
t.Name = sel.Text()
if sel, err = scraper.Find("CAREER", "career info"); err != nil {
return nil, err
}
t.Career = sel.Text()
if sel, err = scraper.Find("INSTITUTION", "institution name"); err != nil {
return nil, err
}
t.Institution = sel.Text()
return t, nil
}