From 0e84f0460a830d2333793c59136048f16f59f2a0 Mon Sep 17 00:00:00 2001 From: Stefan Hoffmann Date: Sat, 6 Feb 2021 00:57:51 +0100 Subject: [PATCH] Fetch .json from URL --- go.mod | 2 ++ go.sum | 16 ++++++++++++++++ main.go | 24 ++++++++++++++---------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 13f5908..185622d 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index e69de29..8ec57a6 100644 --- a/go.sum +++ b/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= diff --git a/main.go b/main.go index 06917ed..d9ad6d6 100644 --- a/main.go +++ b/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 }