Mensa-API/scrape/mensa.go

73 lines
1.6 KiB
Go
Raw Normal View History

2024-09-15 17:10:19 +00:00
package scrape
import (
"io"
"strings"
2024-09-15 18:26:08 +00:00
"github.com/LeRoid-hub/Mensa-API/models"
2024-09-15 17:10:19 +00:00
"github.com/PuerkitoBio/goquery"
)
2024-09-15 18:26:08 +00:00
func ScrapeMensa(h io.ReadCloser) models.Mensa {
var mensa models.Mensa
2024-09-15 17:10:19 +00:00
doc, err := goquery.NewDocumentFromReader(h)
if err != nil {
2024-09-15 18:26:08 +00:00
return models.Mensa{}
2024-09-15 17:10:19 +00:00
}
2024-09-15 18:26:08 +00:00
doc.Find("h1.aw-title-header-title").First().Each(func(i int, s *goquery.Selection) {
mensa.Name = s.Text()
})
doc.Find("a.panel-body").Each(func(i int, s *goquery.Selection) {
l, err := s.Html()
if err != nil {
return
}
l = strings.Replace(l, "<br/>", " ", -1)
l = strings.Replace(l, "<br>", " ", -1)
l = strings.Replace(l, "</br>", " ", -1)
mensa.Location = l
})
2024-09-15 17:10:19 +00:00
2024-09-15 18:26:08 +00:00
//Day
var day models.Day
2024-09-15 17:10:19 +00:00
2024-09-15 18:26:08 +00:00
doc.Find("h2.aw-menu-title").Each(func(i int, s *goquery.Selection) {
day.DayName = s.Text()
2024-09-15 17:10:19 +00:00
})
2024-09-15 18:26:08 +00:00
//Menu
var menu models.Menu
doc.Find("div.aw-meal-category").Each(func(i int, s *goquery.Selection) {
s.Find("h3.aw-meal-category-name").Each(func(i int, t *goquery.Selection) {
menu.Name = t.Text()
})
//Meal
var meal models.Meal
s.Find("div.aw-meal").Each(func(i int, t *goquery.Selection) {
t.Find("p.aw-meal-description").First().Each(func(i int, u *goquery.Selection) {
meal.Name = u.Text()
})
t.Find("div.aw-meal-price").First().Each(func(i int, u *goquery.Selection) {
meal.Price = u.Text()
})
t.Find("p.aw-meal-attributes").First().Each(func(i int, u *goquery.Selection) {
meal.Attributes = u.Text()
})
menu.Meal = append(menu.Meal, meal)
})
day.Menu = append(day.Menu, menu)
})
mensa.AddDay(day)
return mensa
2024-09-15 17:10:19 +00:00
}