verochka/parser/parser.go

129 lines
2.6 KiB
Go
Raw Permalink Normal View History

2022-03-05 18:02:55 +00:00
package parser
import (
2022-09-04 08:45:52 +00:00
"fmt"
2022-03-05 18:02:55 +00:00
"io"
"log"
"net/http"
"strings"
2022-09-04 08:45:52 +00:00
"time"
"github.com/PuerkitoBio/goquery"
2022-03-05 18:02:55 +00:00
)
2022-09-04 08:45:52 +00:00
const scheduleURLTemplate = "https://www.asu.ru/timetable/students/12/2129440415/?date=%s-%s"
2022-03-05 18:02:55 +00:00
type Lesson struct {
Day int
Number string
Time string
Name string
User string
Place string
}
func ParseByDay(day int) []Lesson {
return selectLessonsByDay(parse(), day)
}
func parse() []Lesson {
2022-09-04 08:45:52 +00:00
location, err := time.LoadLocation("Asia/Novosibirsk")
if err != nil {
panic(err)
}
now := time.Now().In(location)
url := fmt.Sprintf(scheduleURLTemplate, now.Format("20060102"), now.Add(24*time.Hour).Format("20060102"))
2022-03-05 18:02:55 +00:00
// 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
2022-04-25 06:16:30 +00:00
doc.Find(".schedule_table tr td").Each(func(i int, s *goquery.Selection) {
2022-03-05 18:02:55 +00:00
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
}
}
n := (k - 1) % 6
if n == 0 {
l.Number = trim(text)
}
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++
})
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 {
var todayLessons []Lesson
for _, l := range schedule {
if l.Day == day {
todayLessons = append(todayLessons, l)
}
}
return todayLessons
}