add all_lessons

This commit is contained in:
2023-01-11 02:00:07 +07:00
parent 78705f95ea
commit 6325033fdf
8 changed files with 66 additions and 104 deletions
+2 -1
View File
@@ -4,9 +4,10 @@ import (
"fmt"
"student_bot/date"
"student_bot/parser"
"time"
)
func main() {
lessons := parser.ParseByDay(date.Today())
lessons := parser.ParseByDay(date.Today(-48*time.Hour))
fmt.Println(lessons)
}
+24 -96
View File
@@ -1,126 +1,54 @@
package parser
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/erizocosmico/go-ics"
)
const scheduleURLTemplate = "https://www.asu.ru/timetable/students/12/2129440415/?date=%s-%s"
type Lesson struct {
Day int
Number string
Time string
Name string
User string
Place string
Number string
TimeStart time.Time
TimeMsg string
Name string
User string
Place string
}
func ParseByDay(day int) []Lesson {
func ParseByDay(day string) []Lesson {
return selectLessonsByDay(parse(), day)
}
func parse() []Lesson {
location, err := time.LoadLocation("Asia/Novosibirsk")
link := "https://www.asu.ru/timetable/students/12/2129440415/?file=2129440415.ics"
calendar, err := ics.ParseCalendar(link, 0, nil)
if err != nil {
panic(err)
}
now := time.Now().In(location)
url := fmt.Sprintf(scheduleURLTemplate, now.Format("20060102"), now.Add(24*time.Hour).Format("20060102"))
// Get html
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
// Find the review items
k := 0
var lessons []Lesson
var l Lesson
doc.Find(".schedule_table tr td").Each(func(i int, s *goquery.Selection) {
text := strings.TrimSpace(s.Text())
if strings.Contains(text, "Понедельник") ||
strings.Contains(text, "Вторник") ||
strings.Contains(text, "Среда") ||
strings.Contains(text, "Четверг") ||
strings.Contains(text, "Пятница") ||
strings.Contains(text, "Суббота") {
k = 0
l = Lesson{}
if strings.Contains(text, "Понедельник") {
l.Day = 1
}
if strings.Contains(text, "Вторник") {
l.Day = 2
}
if strings.Contains(text, "Среда") {
l.Day = 3
}
if strings.Contains(text, "Четверг") {
l.Day = 4
}
if strings.Contains(text, "Пятница") {
l.Day = 5
}
if strings.Contains(text, "Суббота") {
l.Day = 6
}
for _, event := range calendar.Events {
l := Lesson{
Number: "",
TimeStart: event.Start,
TimeMsg: event.Start.Format("15:04") + " - " + event.End.Format("15:04"),
Name: event.Summary,
User: event.Description,
Place: event.Location,
}
n := (k - 1) % 6
if n == 0 {
l.Number = trim(text)
if l.Place == "" {
l.Place = "не приходи"
}
if n == 1 {
l.Time = trim(text)
}
if n == 2 {
l.Name = trim(text)
}
if n == 3 {
l.User = trim(text)
}
if n == 4 {
l.Place = trim(text)
if l.Place == "" {
l.Place = "не приходи"
}
lessons = append(lessons, l)
}
k++
})
lessons = append(lessons, l)
}
return lessons
}
func trim(s string) string {
s = strings.Replace(s, "пр.з.", "пр.", -1)
s = strings.Replace(s, " ", "", -1)
s = strings.Replace(s, "\n", " ", -1)
return s
}
func selectLessonsByDay(schedule []Lesson, day int) []Lesson {
func selectLessonsByDay(schedule []Lesson, day string) []Lesson {
var todayLessons []Lesson
for _, l := range schedule {
if l.Day == day {
if day == "" || strings.Contains(l.TimeStart.String(), day) {
todayLessons = append(todayLessons, l)
}
}