diff --git a/models/Meal.go b/models/Meal.go new file mode 100644 index 0000000..ef05b43 --- /dev/null +++ b/models/Meal.go @@ -0,0 +1,7 @@ +package models + +type Meal struct { + Name string + Price string + Attributes string +} diff --git a/models/Menu.go b/models/Menu.go new file mode 100644 index 0000000..8236a80 --- /dev/null +++ b/models/Menu.go @@ -0,0 +1,6 @@ +package models + +type Menu struct { + Name string + Meal []Meal +} diff --git a/models/day.go b/models/day.go new file mode 100644 index 0000000..49d43ed --- /dev/null +++ b/models/day.go @@ -0,0 +1,6 @@ +package models + +type Day struct { + DayName string + Menu []Menu +} diff --git a/models/mensa.go b/models/mensa.go new file mode 100644 index 0000000..e92cd1e --- /dev/null +++ b/models/mensa.go @@ -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) +} diff --git a/scrape/mensa.go b/scrape/mensa.go index 7820beb..66e28a7 100644 --- a/scrape/mensa.go +++ b/scrape/mensa.go @@ -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, "
", " ", -1) + l = strings.Replace(l, "
", " ", -1) + l = strings.Replace(l, "
", " ", -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 } diff --git a/server/mensa.go b/server/mensa.go index 4c1f364..3ed93d4 100644 --- a/server/mensa.go +++ b/server/mensa.go @@ -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) }