From 3da2da7d4b9e3d32258e868fce3b26beb90ad946 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Tue, 16 Mar 2021 14:46:28 +0000 Subject: [PATCH] set up the ip reverse lookup for the api host, and start figuring out the json parsing --- browser.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ stations.go | 6 ++++++ 2 files changed, 66 insertions(+) diff --git a/browser.go b/browser.go index 1110061..5bb66eb 100644 --- a/browser.go +++ b/browser.go @@ -1,3 +1,63 @@ package main +import ( + "encoding/json" + "fmt" + "io" + "log" + "math/rand" + "net" + "net/http" + "time" +) +//func BytesToString(b []byte) string { +// bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) +// sh := reflect.StringHeader{bh.Data, bh.Len} +// return *(*string)(unsafe.Pointer(&sh)) +//} + +func ResponseToJson(resp io.ReadCloser) interface{} { + var jbody interface{} + err := json.NewDecoder(resp).Decode(&jbody) + if err != nil { + panic(err) + } + return jbody +} + +func RandomIP(iplist []net.IP) net.IP { + rand.Seed(time.Now().Unix()) + randomIndex := rand.Intn(len(iplist)) + return iplist[randomIndex] +} + +func nslookup(hostname string) net.IP { + iprecords, _ := net.LookupIP(hostname) + randomIp := RandomIP(iprecords) + return randomIp +} + +func reverseLookup(ipAddr net.IP) string { + ptr, _ := net.LookupAddr(ipAddr.String()) + return ptr[0] +} + +func GetApiHost() string { + appHostIp := nslookup(api()) + apiHost := reverseLookup(appHostIp) + return apiHost +} + +func GetStations(qstring string) interface{} { + urlstr := fmt.Sprintf("https://%s/json/stations/search?%s&limit=100000",GetApiHost(),qstring) + resp, err := http.Get(urlstr) + if err != nil { + log.Printf(err.Error()) + } + defer resp.Body.Close() + bodyJson := ResponseToJson(resp.Body) + //body, err := io.ReadAll(resp.Body) + //bodyString := bytes.NewBuffer(body).String() + return bodyJson +} \ No newline at end of file diff --git a/stations.go b/stations.go index 4810192..7567dda 100644 --- a/stations.go +++ b/stations.go @@ -32,4 +32,10 @@ func main(){ flag.Usage() } + stations := GetStations("tag=chicago") + for station := range(len(stations)) { + + } + + } \ No newline at end of file