From 62577d4c41ae61ed3a5ba8f08abf733d90ea6816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Barfu=C3=9F?= Date: Thu, 21 Mar 2024 19:32:40 +0100 Subject: [PATCH] Some Structure --- api/fetch/fetch.go | 14 -------- api/server.go | 78 --------------------------------------------- cache/cache.go | 18 +++++++++++ fetch/fetch.go | 16 ++++++++++ main.go | 9 ++++++ scrape/scrape.go | 29 +++++++++++++++++ server/server.go | 53 ++++++++++++++++++++++++++++++ types/bundesland.go | 1 + types/cache.go | 33 +++++++++++++++++++ types/mensa.go | 1 + 10 files changed, 160 insertions(+), 92 deletions(-) delete mode 100644 api/fetch/fetch.go delete mode 100644 api/server.go create mode 100644 cache/cache.go create mode 100644 fetch/fetch.go create mode 100644 main.go create mode 100644 scrape/scrape.go create mode 100644 server/server.go create mode 100644 types/bundesland.go create mode 100644 types/cache.go create mode 100644 types/mensa.go diff --git a/api/fetch/fetch.go b/api/fetch/fetch.go deleted file mode 100644 index 6638009..0000000 --- a/api/fetch/fetch.go +++ /dev/null @@ -1,14 +0,0 @@ -package fetch - -import ( - "net/http" -) - -func Fetch(url string) (*http.Response, error) { - u, err := url.ParseRequestURI(url) - if err != nil { - return nil, err - } - - return http.Get(url) -} diff --git a/api/server.go b/api/server.go deleted file mode 100644 index d42a1ae..0000000 --- a/api/server.go +++ /dev/null @@ -1,78 +0,0 @@ -package main - -import ( - "io" - "net/http" - "net/url" - "strings" - - "golang.org/x/net/html" - - "github.com/gin-gonic/gin" -) - -func main() { - r := gin.Default() - r.GET("/bl/:bundesland", bundesland) - r.Run("localhost:8080") -} - -func bundesland(c *gin.Context) { - bundesland := c.Param("bundesland") - if bundesland == "" { - c.JSON(http.StatusBadRequest, gin.H{ - "error": "bundesland is required", - }) - return - } - resp, err := Fetch("bl/" + bundesland) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error(), - }) - return - } - defer resp.Body.Close() - d, err := io.ReadAll(resp.Body) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{ - "error": err.Error(), - }) - return - } - html := string(d) - scraped := ScrapeBundesland(html) - c.JSON(http.StatusOK, scraped) -} - -func Fetch(path string) (*http.Response, error) { - baseurl := "https://www.imensa.de/" - queryurl := baseurl + "/" + path - u, err := url.ParseRequestURI(queryurl) - if err != nil { - return nil, err - } - return http.Get(u.String()) -} - -func ScrapeBundesland(h string) []string { - tkn := html.NewTokenizer(strings.NewReader(h)) - - var mensen []string - - for { - if tkn.Next() == html.ErrorToken { - return mensen - } - - t := tkn.Token() - attr := t.Attr - - for _, a := range attr { - if a.Key == "class" && a.Val == "elements" { - print(t.Data) - mensen = append(mensen, t.Data) - } - } - } -} diff --git a/cache/cache.go b/cache/cache.go new file mode 100644 index 0000000..8c0e4de --- /dev/null +++ b/cache/cache.go @@ -0,0 +1,18 @@ +package cache + +import ( + "mensa/types" +) + +func NewCache() *types.Cache { + cache := types.Cache{} + return &cache +} + +func setCacheData(cache *types.Cache, key string, data string, lifetime ...int64) { + cache.SetData(key, data, lifetime...) +} + +func getCacheData(cache *types.Cache) string { + return cache.GetData() +} diff --git a/fetch/fetch.go b/fetch/fetch.go new file mode 100644 index 0000000..2eebbe9 --- /dev/null +++ b/fetch/fetch.go @@ -0,0 +1,16 @@ +package fetch + +import ( + "net/http" + "net/url" +) + +func Fetch(path string) (*http.Response, error) { + baseurl := "https://www.imensa.de/" + queryurl := baseurl + "/" + path + u, err := url.ParseRequestURI(queryurl) + if err != nil { + return nil, err + } + return http.Get(u.String()) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..3c3a458 --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "mensa/server" +) + +func main() { + server.Run() +} diff --git a/scrape/scrape.go b/scrape/scrape.go new file mode 100644 index 0000000..fe6f503 --- /dev/null +++ b/scrape/scrape.go @@ -0,0 +1,29 @@ +package scrape + +import ( + "strings" + + "golang.org/x/net/html" +) + +func ScrapeBundesland(h string) []string { + tkn := html.NewTokenizer(strings.NewReader(h)) + + var mensen []string + + for { + if tkn.Next() == html.ErrorToken { + return mensen + } + + t := tkn.Token() + attr := t.Attr + + for _, a := range attr { + if a.Key == "class" && a.Val == "elements" { + print(t.Data) + mensen = append(mensen, t.Data) + } + } + } +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..3587c01 --- /dev/null +++ b/server/server.go @@ -0,0 +1,53 @@ +package server + +import ( + "io" + "mensa/cache" + "mensa/fetch" + "mensa/scrape" + "mensa/types" + "net/http" + + "github.com/gin-gonic/gin" +) + +var c = cache.NewCache() + +func Run() { + r := gin.Default() + r.GET("/bl/:bundesland", bundesland) + r.Run("localhost:8080") +} + +func bundesland(c *gin.Context) { + bundesland := c.Param("bundesland") + if bundesland == "" { + c.JSON(http.StatusBadRequest, gin.H{ + "error": "bundesland is required", + }) + return + } + + if c.GetCacheData(bundesland) != "" { + return c.GetCacheData(bundesland) + } + + resp, err := fetch.Fetch("bl/" + bundesland) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + }) + return + } + defer resp.Body.Close() + d, err := io.ReadAll(resp.Body) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": err.Error(), + }) + return + } + html := string(d) + scraped := scrape.ScrapeBundesland(html) + c.JSON(http.StatusOK, scraped) +} diff --git a/types/bundesland.go b/types/bundesland.go new file mode 100644 index 0000000..ab1254f --- /dev/null +++ b/types/bundesland.go @@ -0,0 +1 @@ +package types diff --git a/types/cache.go b/types/cache.go new file mode 100644 index 0000000..15dcff3 --- /dev/null +++ b/types/cache.go @@ -0,0 +1,33 @@ +package types + +import "time" + +type Cache struct { + data string + lastUpdated time.Time + lifetime int64 + key string +} + +func (c *Cache) SetData(key string, data string, lifetime ...int64) { + if len(lifetime) > 0 { + c.lifetime = lifetime[0] + } else { + c.lifetime = 60 + } + + c.data = data + c.lastUpdated = time.Now() +} + +func (c *Cache) GetData() string { + if time.Now().Unix()-c.lastUpdated.Unix() > c.lifetime { + return "" + } + + if c.data == "" { + return "" + } + + return c.data +} diff --git a/types/mensa.go b/types/mensa.go new file mode 100644 index 0000000..ab1254f --- /dev/null +++ b/types/mensa.go @@ -0,0 +1 @@ +package types