verochka/parser/parser.go

57 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-03-05 18:02:55 +00:00
package parser
import (
"strings"
2022-09-04 08:45:52 +00:00
"time"
2023-01-10 19:00:07 +00:00
"github.com/erizocosmico/go-ics"
2022-03-05 18:02:55 +00:00
)
type Lesson struct {
2023-01-10 19:00:07 +00:00
Number string
TimeStart time.Time
TimeMsg string
Name string
User string
Place string
2022-03-05 18:02:55 +00:00
}
2023-01-10 19:00:07 +00:00
func ParseByDay(day string) []Lesson {
2022-03-05 18:02:55 +00:00
return selectLessonsByDay(parse(), day)
}
func parse() []Lesson {
2023-01-10 19:00:07 +00:00
link := "https://www.asu.ru/timetable/students/12/2129440415/?file=2129440415.ics"
calendar, err := ics.ParseCalendar(link, 0, nil)
2022-09-04 08:45:52 +00:00
if err != nil {
panic(err)
}
2022-03-05 18:02:55 +00:00
var lessons []Lesson
2023-01-10 19:00:07 +00:00
for _, event := range calendar.Events {
l := Lesson{
Number: "",
TimeStart: event.Start,
2023-02-10 08:51:04 +00:00
TimeMsg: event.Start.Format("15:04") + " - " + event.End.Format("15:04"),
2023-01-10 19:00:07 +00:00
Name: event.Summary,
User: event.Description,
Place: event.Location,
2022-03-05 18:02:55 +00:00
}
2023-01-10 19:00:07 +00:00
if l.Place == "" {
l.Place = "не приходи"
2022-03-05 18:02:55 +00:00
}
2023-01-10 19:00:07 +00:00
lessons = append(lessons, l)
}
2022-03-05 18:02:55 +00:00
return lessons
}
2023-01-10 19:00:07 +00:00
func selectLessonsByDay(schedule []Lesson, day string) []Lesson {
2022-03-05 18:02:55 +00:00
var todayLessons []Lesson
for _, l := range schedule {
2023-01-10 19:00:07 +00:00
if day == "" || strings.Contains(l.TimeStart.String(), day) {
2022-03-05 18:02:55 +00:00
todayLessons = append(todayLessons, l)
}
}
return todayLessons
}