summary refs log tree commit diff stats
path: root/pkg/bpod
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/bpod')
-rw-r--r--pkg/bpod/json.go55
-rw-r--r--pkg/bpod/print.go14
2 files changed, 69 insertions, 0 deletions
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
+}
diff --git a/pkg/bpod/print.go b/pkg/bpod/print.go
new file mode 100644
index 0000000..75bf948
--- /dev/null
+++ b/pkg/bpod/print.go
@@ -0,0 +1,14 @@
+package bpod
+
+import (
+	"fmt"
+)
+
+// Print will print the json output
+func Print(res Res) {
+	fmt.Printf("Title: %s\n\n", res.Title)
+	fmt.Printf("Copyright: %s\n", res.Copyright)
+	fmt.Printf("Copyright Link: %s\n", res.CopyrightLink)
+	fmt.Printf("Date: %s\n\n", res.StartDate)
+	fmt.Printf("URL: %s\n", res.Url)
+}
c2'>^
22bb7e35 ^
3dbefa71 ^
22bb7e35 ^
3dbefa71 ^
22bb7e35 ^


3dbefa71 ^
22bb7e35 ^








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59