Skip to content

Commit ab78059

Browse files
committed
init
0 parents  commit ab78059

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

go.mod

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/hduLib/CourseICS
2+
3+
go 1.19
4+
5+
require (
6+
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f
7+
github.com/hduLib/hdu v0.0.0-20220831153942-4b1d5a2b88f1
8+
github.com/teambition/rrule-go v1.7.2
9+
)
10+
11+
require (
12+
github.com/tidwall/gjson v1.14.2 // indirect
13+
github.com/tidwall/match v1.1.1 // indirect
14+
github.com/tidwall/pretty v1.2.0 // indirect
15+
)

go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f h1:feGUUxxvOtWVOhTko8Cbmp33a+tU0IMZxMEmnkoAISQ=
2+
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f/go.mod h1:2MKFUgfNMULRxqZkadG1Vh44we3y5gJAtTBlVsx1BKQ=
3+
github.com/hduLib/hdu v0.0.0-20220831153942-4b1d5a2b88f1 h1:yCZLPCjxuM3iOniBXNbt9aXxd5gqbrh7tOW8b9ZTvLI=
4+
github.com/hduLib/hdu v0.0.0-20220831153942-4b1d5a2b88f1/go.mod h1:rONuk9BD/HoKL6gJ8UITyl8ZEKnCU2daApH9WY6YGQc=
5+
github.com/teambition/rrule-go v1.7.2 h1:goEajFWYydfCgavn2m/3w5U+1b3PGqPUHx/fFSVfTy0=
6+
github.com/teambition/rrule-go v1.7.2/go.mod h1:mBJ1Ht5uboJ6jexKdNUJg2NcwP8uUMNvStWXlJD3MvU=
7+
github.com/tidwall/gjson v1.14.2 h1:6BBkirS0rAHjumnjHF6qgy5d2YAJ1TLIaFE2lzfOLqo=
8+
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
9+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
10+
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
11+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
12+
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=

main.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/emersion/go-ical"
7+
"github.com/hduLib/hdu/skl"
8+
"github.com/teambition/rrule-go"
9+
"log"
10+
"os"
11+
"time"
12+
)
13+
14+
var hduTimeList = []time.Duration{
15+
time.Hour*8 + time.Minute*5,
16+
time.Hour*8 + time.Minute*55,
17+
time.Hour*10 + time.Minute*0,
18+
time.Hour*10 + time.Minute*50,
19+
time.Hour*11 + time.Minute*40,
20+
time.Hour*13 + time.Minute*30,
21+
time.Hour*14 + time.Minute*20,
22+
time.Hour*15 + time.Minute*15,
23+
time.Hour*16 + time.Minute*5,
24+
time.Hour*18 + time.Minute*30,
25+
time.Hour*19 + time.Minute*20,
26+
time.Hour*20 + time.Minute*10,
27+
}
28+
29+
var weekday = []rrule.Weekday{rrule.SU, rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR, rrule.SA, rrule.SU}
30+
31+
func main() {
32+
var account, passwd string
33+
fmt.Println("智慧杭电账号:")
34+
if _, err := fmt.Scan(&account); err != nil {
35+
log.Fatalln(err)
36+
return
37+
}
38+
fmt.Println("智慧杭电密码:")
39+
if _, err := fmt.Scan(&passwd); err != nil {
40+
log.Fatalln(err)
41+
return
42+
}
43+
44+
user, err := skl.Login(account, passwd)
45+
if err != nil {
46+
log.Fatalln(err)
47+
return
48+
}
49+
startDay := time.Now()
50+
startDay = startDay.Add(-time.Duration(int(time.Hour)*startDay.Hour() + int(time.Minute)*startDay.Minute() + int(time.Second)*startDay.Second() + startDay.Nanosecond()))
51+
resp, err := user.Course(startDay)
52+
if err != nil {
53+
log.Fatalln(err)
54+
return
55+
}
56+
startDay = startDay.AddDate(0, 0, -((resp.Week-1)*7 + (int(startDay.Weekday())+6)%7))
57+
ics := ical.NewCalendar()
58+
ics.Props.SetText(ical.PropVersion, "2.0")
59+
ics.Props.SetText(ical.PropProductID, "-//student//hduLib//CN")
60+
ics.Props.SetText(ical.PropCalendarScale, "GREGORIAN")
61+
62+
alarm := ical.NewComponent(ical.CompAlarm)
63+
alarm.Props.SetText(ical.PropAction, ical.ParamDisplay)
64+
alarm.Props.SetText(ical.PropDescription, "距离上课还有30min")
65+
alarm.Props.SetText(ical.PropTrigger, "-P0DT0H30M0S")
66+
67+
for _, v := range resp.List {
68+
event := ical.NewEvent()
69+
event.Props.SetDateTime(ical.PropDateTimeStart, startDay.AddDate(0, 0, (v.StartWeek-1)*7+v.WeekDay-1).Add(hduTimeList[v.StartSection-1]))
70+
event.Props.SetDateTime(ical.PropDateTimeEnd, startDay.AddDate(0, 0, (v.StartWeek-1)*7+v.WeekDay-1).Add(hduTimeList[v.EndSection-1]).Add(time.Minute*45))
71+
event.Props.SetText(ical.PropSummary, fmt.Sprintf("%s %s %s %s", v.CourseName, v.ClassRoom, v.TeacherName, v.CourseSchema))
72+
event.Props.SetDateTime(ical.PropDateTimeStamp, startDay)
73+
// 我不理解,但是不这么做这个go-ical库不让我过编译
74+
event.Props.SetText(ical.PropUID, "")
75+
count := v.EndWeek - v.StartWeek + 1
76+
interval := 1
77+
if v.Period != "" {
78+
count = (count + 1) / 2
79+
interval = 2
80+
}
81+
if err != nil {
82+
log.Fatalln(err)
83+
return
84+
}
85+
86+
event.Props.SetRecurrenceRule(&rrule.ROption{
87+
Freq: rrule.WEEKLY,
88+
Interval: interval,
89+
Wkst: rrule.SU,
90+
Count: count,
91+
Byweekday: []rrule.Weekday{weekday[v.WeekDay]},
92+
})
93+
94+
event.Component.Children = append(event.Component.Children, alarm)
95+
96+
ics.Children = append(ics.Children, event.Component)
97+
}
98+
99+
buf := new(bytes.Buffer)
100+
encoder := ical.NewEncoder(buf)
101+
if err := encoder.Encode(ics); err != nil {
102+
log.Fatalln(err)
103+
return
104+
}
105+
if err := os.WriteFile("./course.ics", buf.Bytes(), os.ModePerm); err != nil {
106+
log.Fatalln(err)
107+
return
108+
}
109+
110+
}

0 commit comments

Comments
 (0)