summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-03-25 00:22:20 +0530
committerAndinus <andinus@nand.sh>2020-03-25 00:22:20 +0530
commita0dd5dfdb701270637c4aab420b9fe7cec8eccf9 (patch)
treeff61d594d56f6cce4fbcb8275a2670dfdef0f896
parent3ee3715ae7777dd6c1804974e2e293406fd1fe54 (diff)
downloadcetus-a0dd5dfdb701270637c4aab420b9fe7cec8eccf9.tar.gz
Add bpod package
-rw-r--r--bpod/json.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/bpod/json.go b/bpod/json.go
new file mode 100644
index 0000000..98b43eb
--- /dev/null
+++ b/bpod/json.go
@@ -0,0 +1,62 @@
+package bpod
+
+import (
+	"encoding/json"
+	"fmt"
+	"math/rand"
+
+	"tildegit.org/andinus/cetus/request"
+)
+
+// BPOD holds the response from the api.
+type BPOD 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"`
+}
+
+// List holds the list of BPOD.
+type List struct {
+	Photos []BPOD `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 takes reqInfo as input and returns the body and an error.
+func GetJson(reqInfo map[string]string) (string, error) {
+	// reqInfo is map[string]string and params is built from it,
+	// currently it takes apiKey and the date from reqInfo to
+	// build param. If any new key/value is added to reqInfo then
+	// it must be addded here too, it won't be sent as param
+	// directly.
+	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 := request.GetRes(reqInfo["api"], params)
+	return string(body), err
+}