From 78346cabaf135a30de1ec9e644c8862f49acee97 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 15 Sep 2024 19:10:19 +0200 Subject: [PATCH] cities --- scrape/mensa.go | 28 ++++++++++++++++++++++++++++ server/city.go | 41 +++++++++++++++++++++++++++++++++++++++++ server/mensa.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ server/server.go | 2 ++ 4 files changed, 119 insertions(+) create mode 100644 scrape/mensa.go diff --git a/scrape/mensa.go b/scrape/mensa.go new file mode 100644 index 0000000..7820beb --- /dev/null +++ b/scrape/mensa.go @@ -0,0 +1,28 @@ +package scrape + +import ( + "io" + "strings" + + "github.com/PuerkitoBio/goquery" +) + +func ScrapeMensa(h io.ReadCloser) []string { + var mensas []string + + doc, err := goquery.NewDocumentFromReader(h) + if err != nil { + return []string{} + } + + 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)) + }) + return mensas + +} diff --git a/server/city.go b/server/city.go index abb4e43..5c3738b 100644 --- a/server/city.go +++ b/server/city.go @@ -1 +1,42 @@ package server + +import ( + "github.com/LeRoid-hub/Mensa-API/cache" + "github.com/LeRoid-hub/Mensa-API/fetch" + "github.com/LeRoid-hub/Mensa-API/scrape" + "github.com/gin-gonic/gin" +) + +func city(c *gin.Context) { + city := c.Param("city") + if city == "" { + c.JSON(400, gin.H{ + "error": "city is required", + }) + return + } + + if cache.HasCacheData(city) { + c.JSON(200, cache.GetCacheData(city)) + return + } + + resp, err := fetch.Fetch(city) + if err != nil { + c.JSON(500, gin.H{ + "error": err.Error(), + }) + return + } + + defer resp.Body.Close() + if resp.StatusCode != 200 { + c.JSON(500, gin.H{ + "error": "status code is not 200", + }) + return + } + + scraped := scrape.ScrapeState(resp.Body) + c.JSON(200, scraped) +} diff --git a/server/mensa.go b/server/mensa.go index abb4e43..4c1f364 100644 --- a/server/mensa.go +++ b/server/mensa.go @@ -1 +1,49 @@ package server + +import ( + "github.com/LeRoid-hub/Mensa-API/cache" + "github.com/LeRoid-hub/Mensa-API/fetch" + "github.com/LeRoid-hub/Mensa-API/scrape" + "github.com/gin-gonic/gin" +) + +func mensa(c *gin.Context) { + mensa := c.Param("mensa") + if mensa == "" { + c.JSON(400, gin.H{ + "error": "mensa is required", + }) + return + } + city := c.Param("city") + if city == "" { + c.JSON(400, gin.H{ + "error": "city is required", + }) + return + } + + if cache.HasCacheData(city + "/" + mensa) { + c.JSON(200, cache.GetCacheData(mensa)) + return + } + + resp, err := fetch.Fetch(mensa) + if err != nil { + c.JSON(500, gin.H{ + "error": err.Error(), + }) + return + } + + defer resp.Body.Close() + if resp.StatusCode != 200 { + c.JSON(500, gin.H{ + "error": "status code is not 200", + }) + return + } + + scraped := scrape.ScrapeState(resp.Body) + c.JSON(200, scraped) +} diff --git a/server/server.go b/server/server.go index f55ca18..e191582 100644 --- a/server/server.go +++ b/server/server.go @@ -12,5 +12,7 @@ func Run() { }) }) r.GET("/state/:state", state) + r.GET("/city/:city", city) + r.GET("/mensa/:city/:mensa", mensa) r.Run() // listen and serve on }