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 19:14:48 +00:00
|
|
|
var mensaName = ""
|
|
|
|
var mensaLocation = ""
|
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) {
|
2024-09-15 19:14:48 +00:00
|
|
|
mensaName = s.Text()
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2024-09-15 19:14:48 +00:00
|
|
|
mensaLocation = l
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
2024-09-15 17:10:19 +00:00
|
|
|
|
2024-09-15 19:14:48 +00:00
|
|
|
mensa.SetMensa(mensaName, mensaLocation)
|
|
|
|
|
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) {
|
2024-09-15 19:14:48 +00:00
|
|
|
day.SetDay(s.Text())
|
2024-09-15 17:10:19 +00:00
|
|
|
})
|
2024-09-15 18:26:08 +00:00
|
|
|
|
|
|
|
//Menu
|
|
|
|
doc.Find("div.aw-meal-category").Each(func(i int, s *goquery.Selection) {
|
|
|
|
|
2024-09-15 19:01:15 +00:00
|
|
|
var menu models.Menu
|
|
|
|
|
2024-09-15 18:26:08 +00:00
|
|
|
s.Find("h3.aw-meal-category-name").Each(func(i int, t *goquery.Selection) {
|
2024-09-15 19:14:48 +00:00
|
|
|
menu.SetMenu(t.Text())
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
//Meal
|
|
|
|
var meal models.Meal
|
|
|
|
|
|
|
|
s.Find("div.aw-meal").Each(func(i int, t *goquery.Selection) {
|
2024-09-15 19:14:48 +00:00
|
|
|
mealName := ""
|
|
|
|
mealPrice := ""
|
|
|
|
mealAttributes := ""
|
2024-09-15 18:26:08 +00:00
|
|
|
t.Find("p.aw-meal-description").First().Each(func(i int, u *goquery.Selection) {
|
2024-09-15 19:14:48 +00:00
|
|
|
mealName = u.Text()
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
|
|
|
t.Find("div.aw-meal-price").First().Each(func(i int, u *goquery.Selection) {
|
2024-09-15 19:14:48 +00:00
|
|
|
mealPrice = u.Text()
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
|
|
|
t.Find("p.aw-meal-attributes").First().Each(func(i int, u *goquery.Selection) {
|
2024-09-15 19:14:48 +00:00
|
|
|
mealAttributes = u.Text()
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
2024-09-15 19:14:48 +00:00
|
|
|
meal.SetMeal(mealName, mealPrice, mealAttributes)
|
|
|
|
menu.AddMeal(meal)
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
2024-09-15 19:14:48 +00:00
|
|
|
day.AddMenu(menu)
|
2024-09-15 18:26:08 +00:00
|
|
|
})
|
|
|
|
mensa.AddDay(day)
|
|
|
|
|
|
|
|
return mensa
|
2024-09-15 17:10:19 +00:00
|
|
|
|
|
|
|
}
|