-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsched.go
220 lines (220 loc) · 5.98 KB
/
sched.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
// Jack Schefer, began 9/3/16
// Purpose: to print out the schedule using the ION API.
//
package main
//
import (
"os"
"fmt"
"time"
"strconv"
"strings"
"net/http"
"encoding/json"
"io/ioutil"
)
//
const (
CYAN string = "\033[96m"
GREEN string = "\033[92m"
BOLD string = "\033[1m"
END string = "\033[0m"
)
//
///////////////////////////////////////////////////////////
//
type Block struct {
Order int `json:"order"`
Name string `json:"name"`
Start string `json:"start"`
End string `json:"end"`
}
//
type Day struct {
Name string `json:"name"`
Special bool `json:"special"`
Blocks []Block `json:"blocks"`
}
//
type Result struct {
Url string `json:"url"`
Date string `json:"date"`
DayType Day `json:"day_type"`
}
//
type Response struct {
Next string `json:"next"`
Results []Result `json:"results"`
}
//
///////////////////////////////////////////////////////////
//
func main() {
if len(os.Args) > 1 && os.Args[1] == "week" {
show_weekly_schedule()
//
} else {
//
data := Response{}
//
// 1. Get and print out the current date and time.
//
now := time.Now()
print_current_time( now )
//
// 2. Get the schedule data from Ion
url_params := ""
if now.Weekday() != 0 && now.Weekday() != 6 && now.Hour() > 16 { //after school on a weekday
url_params += "&page=2"
}
res, err := http.Get("https://ion.tjhsst.edu/api/schedule?format=json")
check(err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
check(err)
json.Unmarshal(body, &data)
//
// 3. check if there's school today
schedule_date := data.Results[0].Date
today := false
if now.Format("2006-01-02") == schedule_date {
today = true
}
//
// 4. if so, print it out
title := data.Results[0].DayType.Name
title = strings.Replace(title, "<br>", " ", -1)
fmt.Println(title)
//
max_name_length := 0
for _,b := range data.Results[0].DayType.Blocks {
if len(strings.Replace( b.Name + ":", "<br>", " ", -1)) > max_name_length {
max_name_length = len(strings.Replace( b.Name + ":", "<br>", " ", -1))
}
}
//
for _, b := range data.Results[0].DayType.Blocks {
name := strings.Replace( b.Name + ":", "<br>", " ", -1)
for i := len(name); i < max_name_length; i++ {
name += " "
}
//
start := b.Start
end := b.End
//
shrs,serr := strconv.Atoi(start[0:strings.Index(start, ":")])
smin,smer := strconv.Atoi(start[strings.Index(start, ":") + 1:])
ehrs,eerr := strconv.Atoi(end[0:strings.Index(end, ":")])
emin,emer := strconv.Atoi(end[strings.Index(end, ":") + 1:])
if serr == nil && shrs > 12 {
start = strings.Replace(start, strconv.Itoa(shrs), strconv.Itoa(shrs - 12), 1)
}
if eerr == nil && ehrs > 12 {
end = strings.Replace(end, strconv.Itoa(ehrs), strconv.Itoa(ehrs - 12), 1)
}
var isCurrentBlock bool
if serr == nil && eerr == nil && smer == nil && emer == nil {
isCurrentBlock = withinTimes(now.Hour(), now.Minute(), shrs, smin, ehrs, emin)
}
//
if today && isCurrentBlock {
fmt.Printf("%s", CYAN)
}
fmt.Printf("%s\t%s\t- %s\n", name, start, end)
if today && isCurrentBlock {
fmt.Printf("%s", END)
}
}
}
fmt.Println()
}
//
///////////////////////////////////////////////////////////
//
// HELPER METHODS
//
///////////////////////////////////////////////////////////
//
func show_weekly_schedule() {
// show the order of days in the week
fmt.Println("\nRemainder of the week's schedule.\n")
_, this_week := time.Now().ISOWeek()
if time.Now().Weekday() == 6 { // if it's saturday, display the next week anyway
this_week = (this_week + 1) % 53
}
//
var i int = 1
next_name, next_date := parseJSON(1)
t, err := time.Parse("2006-01-02", next_date)
check(err)
_, week := t.ISOWeek()
for week == this_week {
fmt.Printf("%s: %s\n", t.Weekday(), next_name)
i = i + 1
next_name, next_date = parseJSON(i)
t, err = time.Parse("2006-01-02", next_date)
check(err)
_, week = t.ISOWeek()
// so that next_name isn't unused
foo := strings.Index(next_name, "foo")
if foo > -1 {
fmt.Println("name contains foo!")
}
}
}
//
///////////////////////////////////////////////////////////
//
func print_current_time( now time.Time ) {
fmt.Println()
var tsuffix string
var zero string = ""
fmt.Printf("%v, %v %v\n", now.Weekday(), now.Month(), now.Day())
if now.Hour() / 12 == 0 {
tsuffix = "am"
} else {
tsuffix = "pm"
}
if now.Minute() < 10 {
zero = "0"
}
hr := now.Hour() % 12
if hr == 0 {
hr = 12
}
fmt.Printf("Time: %v:%s%v %s\n", hr, zero, now.Minute(), tsuffix)
fmt.Println()
}
//
///////////////////////////////////////////////////////////
//
func parseJSON(index int) (string, string) {
data := Response{}
res, err := http.Get("https://ion.tjhsst.edu/api/schedule?format=json&page=" + strconv.Itoa(index))
check(err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
check(err)
json.Unmarshal(body, &data)
//
name := data.Results[0].DayType.Name
name = strings.Replace(name, "<br>", " ", -1)
date := data.Results[0].Date
return name, date
}
//
///////////////////////////////////////////////////////////
//
func withinTimes(nhrs int, nmin int, shrs int, smin int, ehrs int, emin int) bool {
return shrs * 60 + smin <= nhrs * 60 + nmin && nhrs * 60 + nmin < ehrs * 60 + emin
}
//
///////////////////////////////////////////////////////////
//
func check(e error) {
if e != nil {
panic(e.Error())
}
}
//
// End of file.