Fetch .json from URL
This commit is contained in:
parent
c8569db91a
commit
0e84f0460a
2
go.mod
2
go.mod
@ -1,3 +1,5 @@
|
||||
module git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api
|
||||
|
||||
go 1.16
|
||||
|
||||
require git.nils.zone/nils/prettify v0.0.4
|
||||
|
16
go.sum
16
go.sum
@ -0,0 +1,16 @@
|
||||
git.nils.zone/nils/prettify v0.0.4 h1:y014Ejp0yhFAc9vu5U0nJRFxtXN6k6eWW6LiUkwn4NQ=
|
||||
git.nils.zone/nils/prettify v0.0.4/go.mod h1:q4ydEhSvXuywHe5U6uSEoEeS9f2Upz/n8z0MJylLz5c=
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
|
||||
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
|
||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
|
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
24
main.go
24
main.go
@ -10,10 +10,11 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "git.nils.zone/nils/prettify"
|
||||
)
|
||||
|
||||
// types
|
||||
@ -45,6 +46,7 @@ func main() {
|
||||
if *ucUser == "" {
|
||||
log.Fatalln("Please specify an API User via the flag '-ucUser'")
|
||||
}
|
||||
//getFile()
|
||||
// start API processing (runs in a loop)
|
||||
go processAPIs()
|
||||
|
||||
@ -280,22 +282,24 @@ func processAPIs() {
|
||||
log.Println("...done")
|
||||
}
|
||||
}
|
||||
|
||||
func getFile(url string) []byte {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Printf("error")
|
||||
}
|
||||
data := resp.Body
|
||||
byteValue, _ := ioutil.ReadAll(data)
|
||||
return byteValue
|
||||
}
|
||||
func getDevices(file string) (devices, error) {
|
||||
// get devices from JSON file
|
||||
jsonFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return devices{}, errors.New("can't open devices.json")
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
jsonFile := getFile("https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/" + file)
|
||||
|
||||
// read file to bytes
|
||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||
|
||||
// variable for d
|
||||
var d devices
|
||||
// unmarshal to struct
|
||||
json.Unmarshal(byteValue, &d)
|
||||
json.Unmarshal(jsonFile, &d)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user