summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cmd/cetus-bing/cetus-bing.go103
-rw-r--r--pkg/bing/bpod.go77
-rw-r--r--pkg/cetus/req.go43
3 files changed, 114 insertions, 109 deletions
diff --git a/cmd/cetus-bing/cetus-bing.go b/cmd/cetus-bing/cetus-bing.go
index 3cc3850..871c96a 100644
--- a/cmd/cetus-bing/cetus-bing.go
+++ b/cmd/cetus-bing/cetus-bing.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"encoding/json"
 	"flag"
 	"fmt"
 	"log"
@@ -12,19 +13,31 @@ import (
 	"framagit.org/andinus/cetus/pkg/cetus"
 )
 
+type photo 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 bpod struct {
+	photos []photo `json:"images"`
+}
+
 var (
+	t         time.Duration
+	api       string
+	dump      bool
 	quiet     bool
+	random    bool
 	version   bool
-	fetchOnly bool
 	pathOnly  bool
-
-	api     string
-	random  bool
-	timeout time.Duration
-
-	err       error
-	bpodRes   bing.BPOD
-	bpodPhoto bing.Photo
+	fetchOnly bool
 )
 
 func main() {
@@ -37,29 +50,30 @@ func main() {
 	rand.Seed(time.Now().Unix())
 
 	// Convert timeout to seconds
-	timeout = timeout * time.Second
+	t = t * time.Second
 
-	// get response from api
-	bpodRes, err = getBPODRes()
-	if err != nil {
-		log.Fatal(err)
+	body, err := bpodBody()
+	if dump {
+		fmt.Println(body)
+		return
 	}
 
+	bpod := bpod{}
+	err = json.Unmarshal([]byte(body), &bpod)
+	errChk("body unmarshal failed", err)
+
 	// if random was set then bpodRes holds list of multiple
 	// responses, choose a random response from the list
-	var i int = rand.Intn(len(bpodRes.Photos))
-	bpodPhoto = bpodRes.Photos[i]
+	var i int = rand.Intn(len(bpod.photos))
+	bpodPhoto := bpod.photos[i]
 
 	// correct image path
-	bpodPhoto.URL = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.URL)
+	bpodPhoto.url = fmt.Sprintf("%s%s", "https://www.bing.com", bpodPhoto.url)
 
 	// correct date format
-	var dt time.Time
-	dt, err = time.Parse("20060102", bpodPhoto.StartDate)
-	if err != nil {
-		log.Fatal(err)
-	}
-	bpodPhoto.StartDate = dt.Format("2006-01-02")
+	dt, err := time.Parse("20060102", bpodPhoto.startDate)
+	errChk("bpodPhoto.startDate parse failed", err)
+	bpodPhoto.startDate = dt.Format("2006-01-02")
 
 	printDetails(bpodPhoto)
 
@@ -68,51 +82,52 @@ func main() {
 		return
 	}
 
-	// if media type is an image then set background
-	err = background.Set(bpodPhoto.URL)
-	if err != nil {
-		log.Fatal(err)
-	}
+	err = background.Set(bpodPhoto.url)
+	errChk("setting background failed", err)
 }
 
 func parseFlags() {
 	flag.BoolVar(&quiet, "quiet", false, "No output")
 	flag.BoolVar(&version, "version", false, "Cetus version")
 	flag.BoolVar(&fetchOnly, "fetch-only", false, "Don't set background, only fetch info")
-
+	flag.BoolVar(&dump, "dump", false, "Only dump received response")
 	flag.BoolVar(&random, "random", false, "Choose a random image (from 7 images)")
 	flag.BoolVar(&pathOnly, "path-only", false, "Print only path of the image")
 
 	flag.StringVar(&api, "api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API URL")
 
-	flag.DurationVar(&timeout, "timeout", 32*time.Second, "Timeout for http client in seconds")
+	flag.DurationVar(&t, "timeout", 32*time.Second, "Timeout for http client in seconds")
 	flag.Parse()
 
 }
 
-func printDetails(bpodPhoto bing.Photo) {
+func printDetails(bpodPhoto photo) {
 	if quiet {
 		return
 	}
 	if pathOnly {
-		cetus.PrintPath(bpodPhoto.URL)
+		cetus.PrintPath(bpodPhoto.url)
 		return
 	}
-	fmt.Printf("Title: %s\n\n", bpodPhoto.Title)
-	fmt.Printf("Copyright: %s\n", bpodPhoto.Copyright)
-	fmt.Printf("Copyright Link: %s\n", bpodPhoto.CopyrightLink)
-	fmt.Printf("Date: %s\n\n", bpodPhoto.StartDate)
-	fmt.Printf("URL: %s\n", bpodPhoto.URL)
+	fmt.Printf("Title: %s\n\n", bpodPhoto.title)
+	fmt.Printf("Copyright: %s\n", bpodPhoto.copyright)
+	fmt.Printf("Copyright Link: %s\n", bpodPhoto.copyrightLink)
+	fmt.Printf("Date: %s\n\n", bpodPhoto.startDate)
+	fmt.Printf("URL: %s\n", bpodPhoto.url)
 }
 
-func getBPODRes() (bing.BPOD, error) {
-	var bpodInfo map[string]string
-	bpodInfo = make(map[string]string)
-	bpodInfo["api"] = api
+func bpodBody() (string, error) {
+	reqInfo := make(map[string]string)
+	reqInfo["api"] = api
 	if random {
-		bpodInfo["random"] = "true"
+		reqInfo["random"] = "true"
 	}
-	bpodRes, err = bing.BPODPath(bpodInfo, timeout)
 
-	return bpodRes, err
+	body, err := bing.GetBpodJson(reqInfo, t)
+	return body, err
+}
+
+func errChk(ctx string, err error) {
+	log.Println(ctx)
+	log.Fatal(err)
 }
diff --git a/pkg/bing/bpod.go b/pkg/bing/bpod.go
index 041eef7..c7ee79d 100644
--- a/pkg/bing/bpod.go
+++ b/pkg/bing/bpod.go
@@ -1,76 +1,23 @@
 package bing
 
 import (
-	"encoding/json"
-	"fmt"
-	"io/ioutil"
-	"net/http"
 	"time"
-)
-
-// Photo holds responses
-type Photo 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"`
-}
-
-// BPOD  holds list of response
-type BPOD struct {
-	Photos []Photo `json:"images"`
-}
 
-// BPODPath returns Bing Photo of the Day responses
-func BPODPath(bpodInfo map[string]string, timeout time.Duration) (BPOD, error) {
-	var err error
-	bpodRes := BPOD{}
-
-	client := http.Client{
-		Timeout: time.Second * timeout,
-	}
-
-	req, err := http.NewRequest(http.MethodGet, bpodInfo["api"], nil)
-	if err != nil {
-		return bpodRes, err
-	}
-	q := req.URL.Query()
-	q.Add("format", "js")
-
-	// if random flag is passed then fetch 7 photos
-	if bpodInfo["random"] == "true" {
-		q.Add("n", "7")
-	} else {
-		q.Add("n", "1")
-	}
-	req.URL.RawQuery = q.Encode()
-
-	res, err := client.Do(req)
-
-	if err != nil {
-		return bpodRes, err
-	}
-	defer res.Body.Close()
+	"framagit.org/andinus/cetus/pkg/cetus"
+)
 
-	resBody, err := ioutil.ReadAll(res.Body)
-	if err != nil {
-		return bpodRes, err
-	}
+// GetBpodJson returns json response received from the api
+func GetBpodJson(reqInfo map[string]string, t time.Duration) (string, error) {
+	params := make(map[string]string)
+	params["format"] = "js"
+	params["n"] = "1"
 
-	err = json.Unmarshal([]byte(resBody), &bpodRes)
-	if err != nil {
-		return bpodRes, err
-	}
+	// if random is true then fetch 7 photos
+	if reqInfo["random"] == "true" {
+		params["n"] = "7"
 
-	if res.StatusCode != 200 {
-		return bpodRes, fmt.Errorf("Unexpected response status code received: %d %s",
-			res.StatusCode, http.StatusText(res.StatusCode))
 	}
 
-	return bpodRes, err
+	body, err := cetus.GetRes(reqInfo["api"], params, t)
+	return string(body), err
 }
diff --git a/pkg/cetus/req.go b/pkg/cetus/req.go
new file mode 100644
index 0000000..84e52a5
--- /dev/null
+++ b/pkg/cetus/req.go
@@ -0,0 +1,43 @@
+package cetus
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"time"
+)
+
+// GetRes returns api response
+func GetRes(api string, params map[string]string, t time.Duration) (string, error) {
+	c := http.Client{
+		Timeout: time.Second * t,
+	}
+
+	req, err := http.NewRequest(http.MethodGet, api, nil)
+	if err != nil {
+		return "", err
+	}
+
+	q := req.URL.Query()
+	for k, v := range params {
+		q.Add(k, v)
+	}
+	req.URL.RawQuery = q.Encode()
+
+	res, err := c.Do(req)
+	if err != nil {
+		return "", err
+	}
+	defer res.Body.Close()
+
+	if res.StatusCode != 200 {
+		return "", fmt.Errorf("Unexpected response status code received: %d %s",
+			res.StatusCode, http.StatusText(res.StatusCode))
+	}
+
+	body, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return "", err
+	}
+	return string(body), err
+}
ik K. Agaram <vc@akkartik.com> 2015-01-21 02:06:53 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2015-01-21 02:06:53 -0800 596' href='/akkartik/mu/commit/chessboard-cursor.arc.t?h=main&id=79ca54b3d2e74ee7789437fbae9249fb88927f96'>79ca54b3 ^
df0485ae ^
79ca54b3 ^
df0485ae ^
79ca54b3 ^

4ca73eb6 ^
79ca54b3 ^
5b698455 ^
79ca54b3 ^



f3060715 ^
47588257 ^
0c145043 ^
47588257 ^









14a1649c ^
0c145043 ^

47588257 ^
188206bd ^
47588257 ^









188206bd ^

14a1649c ^


188206bd ^

47588257 ^
188206bd ^
47588257 ^









14a1649c ^


188206bd ^

47588257 ^
33d602db ^
47588257 ^

f7a26719 ^

47588257 ^






f7a26719 ^

47588257 ^



33d602db ^


8799371a ^
cc36784d ^
33d602db ^









8799371a ^
445a1494 ^

47588257 ^
445a1494 ^
47588257 ^






f7a26719 ^
47588257 ^






f7a26719 ^
47588257 ^















445a1494 ^


8799371a ^
cc36784d ^
445a1494 ^









8799371a ^
33d602db ^
47588257 ^
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241