-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (103 loc) · 2.81 KB
/
main.go
File metadata and controls
134 lines (103 loc) · 2.81 KB
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
package main
import (
_ "embed"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/ghodss/yaml"
"github.com/pkg/browser"
)
type ClassTime struct {
Day string `json:"day,omitempty"`
Time string `json:"time,omitempty"`
}
type Class struct {
Link string `json:"link,omitempty"`
Times []ClassTime `json:"times"`
}
type ClassConfig map[string]Class
type ClassToday struct {
name string
time time.Time
link string
}
var todaysClassLaunched map[string]bool
//go:embed sample.yaml
var sampleConfigStr []byte
func openClassLink(config ClassConfig) {
date := time.Now()
weekDay := strings.ToLower(date.Weekday().String())[0:3]
// classesToday
classesToday := getClassesToday(config, weekDay)
// Find the upcoming class
upcomingClass := ClassToday{}
for _, val := range classesToday {
if val.time.After(date) {
upcomingClass = val
break
}
}
// Check if all classes lanched
allClassesLaunched := true
for _, launched := range todaysClassLaunched {
if !launched {
allClassesLaunched = false
}
}
if (upcomingClass == ClassToday{} || allClassesLaunched) {
fmt.Fprintln(
color.Output,
color.YellowString("No more classes for today 🥳🥳🥳. Feel free to close this window."),
)
return
}
launchTime := upcomingClass.time.Add(-5 * time.Minute)
minuteStr := strconv.Itoa(upcomingClass.time.Minute())
if upcomingClass.time.Minute() < 10 {
minuteStr = "0" + minuteStr
}
if !todaysClassLaunched[upcomingClass.name] {
fmt.Fprintf(
color.Output,
color.BlueString("[RUNNING] Launching next class "+color.CyanString("%s")+" @ %d:%02d\n"),
upcomingClass.name,
launchTime.Hour(),
launchTime.Minute(),
)
}
if launchTime.Before(time.Now()) && !todaysClassLaunched[upcomingClass.name] {
fmt.Fprintf(color.Output, color.GreenString("[LAUNCHING] Launching %s\n"), upcomingClass.name)
link := upcomingClass.link
if len(link) == 0 {
link += "https://auto-class-launcher-alarm.vercel.app/?className="
link += url.QueryEscape(upcomingClass.name)
link += "&timing=" + strconv.Itoa(upcomingClass.time.Hour()) + ":" + minuteStr
}
browser.OpenURL(link)
todaysClassLaunched[upcomingClass.name] = true
}
}
func main() {
CONFIG_PATH := "./auto-class-launcher-timetable.yaml"
var config ClassConfig
// Now check if file exists
if data, err := os.ReadFile(CONFIG_PATH); err == nil {
yaml.Unmarshal(data, &config)
} else if os.IsNotExist(err) {
yaml.Unmarshal([]byte(sampleConfigStr), &config)
if os.WriteFile(CONFIG_PATH, sampleConfigStr, 0666) != nil {
fmt.Println("Unable to create timetable on disk")
}
}
welcomeMessage(CONFIG_PATH)
ticker := time.NewTicker(10 * time.Second)
// Initialize todaysClassLaunched here
todaysClassLaunched = make(map[string]bool)
for range ticker.C {
openClassLink(config)
}
}