mirror of
https://github.com/LeRoid-hub/Mensa-API.git
synced 2025-12-16 15:59:35 +00:00
some
This commit is contained in:
parent
c3cd0efb11
commit
e26aa4fb31
@ -2,7 +2,6 @@ package fetch
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Fetch(url string) (*http.Response, error) {
|
func Fetch(url string) (*http.Response, error) {
|
||||||
@ -13,4 +12,3 @@ func Fetch(url string) (*http.Response, error) {
|
|||||||
|
|
||||||
return http.Get(url)
|
return http.Get(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,28 +1,31 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"io"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.GET("/fetch/:url", fetchRoute)
|
r.GET("/bl/:bundesland", bundesland)
|
||||||
r.Run("localhost:8080")
|
r.Run("localhost:8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchRoute(c *gin.Context) {
|
func bundesland(c *gin.Context) {
|
||||||
url := c.Param("url")
|
bundesland := c.Param("bundesland")
|
||||||
if url == "" {
|
if bundesland == "" {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
"error": "url is required",
|
"error": "bundesland is required",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp, err := Fetch(url)
|
resp, err := Fetch("bl/" + bundesland)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
@ -37,19 +40,39 @@ func fetchRoute(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d = string(d)
|
html := string(d)
|
||||||
c.JSON(http.StatusOK, d)
|
scraped := ScrapeBundesland(html)
|
||||||
|
c.JSON(http.StatusOK, scraped)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fetch(path string) (*http.Response, error) {
|
func Fetch(path string) (*http.Response, error) {
|
||||||
baseurl := "https://www.imensa.de/"
|
baseurl := "https://www.imensa.de/"
|
||||||
queryurl := baseurl + "/" + path
|
queryurl := baseurl + "/" + path
|
||||||
u, err := url.ParseRequestURI(queryurl)
|
u, err := url.ParseRequestURI(queryurl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.Get(u.String())
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user