functional

This commit is contained in:
Jan 2024-09-15 20:26:08 +02:00
parent d9c073919e
commit f52bb05750
6 changed files with 95 additions and 13 deletions

7
models/Meal.go Normal file
View File

@ -0,0 +1,7 @@
package models
type Meal struct {
Name string
Price string
Attributes string
}

6
models/Menu.go Normal file
View File

@ -0,0 +1,6 @@
package models
type Menu struct {
Name string
Meal []Meal
}

6
models/day.go Normal file
View File

@ -0,0 +1,6 @@
package models
type Day struct {
DayName string
Menu []Menu
}

16
models/mensa.go Normal file
View File

@ -0,0 +1,16 @@
package models
type Mensa struct {
Name string
Location string
Days []Day
}
func (m *Mensa) SetMensa(name string, location string) {
m.Name = name
m.Location = location
}
func (m *Mensa) AddDay(day Day) {
m.Days = append(m.Days, day)
}

View File

@ -4,25 +4,69 @@ import (
"io"
"strings"
"github.com/LeRoid-hub/Mensa-API/models"
"github.com/PuerkitoBio/goquery"
)
func ScrapeMensa(h io.ReadCloser) []string {
var mensas []string
func ScrapeMensa(h io.ReadCloser) models.Mensa {
var mensa models.Mensa
doc, err := goquery.NewDocumentFromReader(h)
if err != nil {
return []string{}
return models.Mensa{}
}
doc.Find("a.primary").Each(func(i int, s *goquery.Selection) {
href, _ := s.Attr("href")
print(href)
mensa := strings.Split(href, "/")[1]
mensas = append(mensas, string(mensa))
doc.Find("h1.aw-title-header-title").First().Each(func(i int, s *goquery.Selection) {
mensa.Name = s.Text()
})
return mensas
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
})
//Day
var day models.Day
doc.Find("h2.aw-menu-title").Each(func(i int, s *goquery.Selection) {
day.DayName = s.Text()
})
//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
}

View File

@ -28,7 +28,7 @@ func mensa(c *gin.Context) {
return
}
resp, err := fetch.Fetch(mensa)
resp, err := fetch.Fetch(city + "/" + mensa)
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
@ -44,6 +44,9 @@ func mensa(c *gin.Context) {
return
}
scraped := scrape.ScrapeState(resp.Body)
scraped := scrape.ScrapeMensa(resp.Body)
//cache.SetCacheData(city+"/"+mensa, scraped)
c.JSON(200, scraped)
}