From 052140fb3ccd4b386d8d98ba7355d676c1e0693d Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 18 Mar 2020 23:04:21 +0530 Subject: Initial commit for Cetus v0.5.0 --- pkg/bpod/json.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 pkg/bpod/json.go (limited to 'pkg/bpod/json.go') diff --git a/pkg/bpod/json.go b/pkg/bpod/json.go new file mode 100644 index 0000000..4d74668 --- /dev/null +++ b/pkg/bpod/json.go @@ -0,0 +1,55 @@ +package bpod + +import ( + "encoding/json" + "fmt" + "math/rand" + + "framagit.org/andinus/cetus/pkg/cetus" +) + +type Res struct { + StartDate string `json:"startdate"` + FullStartDate string `json:"fullstartdate"` + EndDate string `json:"enddate"` + Url string `json:"url"` + UrlBase string `json:"urlbase"` + Copyright string `json:"copyright"` + CopyrightLink string `json:"copyrightlink"` + Title string `json:"title"` + Hsh string `json:"hsh"` +} + +type List struct { + Photos []Res `json:"images"` +} + +// UnmarshalJson will take body as input & unmarshal it to res +func UnmarshalJson(body string) (Res, error) { + list := List{} + res := Res{} + + err := json.Unmarshal([]byte(body), &list) + if err != nil { + return res, fmt.Errorf("UnmarshalJson failed\n%s", err.Error()) + } + + res = list.Photos[rand.Intn(len(list.Photos))] + return res, nil +} + +// GetJson returns json response received from the api +func GetJson(reqInfo map[string]string) (string, error) { + params := make(map[string]string) + params["format"] = "js" + params["n"] = "1" + + // if random is true then fetch 7 photos + if reqInfo["random"] == "true" { + params["n"] = "7" + + } + + body, err := cetus.GetRes(reqInfo["api"], params) + return string(body), err +} -- cgit 1.4.1-2-gfad0