summary refs log tree commit diff stats
path: root/pkg
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-15 17:13:28 +0530
committerAndinus <andinus@inventati.org>2020-03-15 17:13:28 +0530
commit16bcb7f173399c17b73469ac26d20e0d29c59e0d (patch)
treee27a1b0b3c648bd26d8aa0079f4cce060d5fda58 /pkg
parent88f7ad2ae17b9be5b55b03dd014901ed3b1bea85 (diff)
downloadcetus-16bcb7f173399c17b73469ac26d20e0d29c59e0d.tar.gz
Support Bing Photo of the Day
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bing/bpod.go90
-rw-r--r--pkg/cetus/cetus.go5
2 files changed, 95 insertions, 0 deletions
diff --git a/pkg/bing/bpod.go b/pkg/bing/bpod.go
new file mode 100644
index 0000000..5f425b0
--- /dev/null
+++ b/pkg/bing/bpod.go
@@ -0,0 +1,90 @@
+// Copyright (c) 2020, Andinus <andinus@inventati.org>
+
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+
+// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+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()
+
+	resBody, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return bpodRes, err
+	}
+
+	err = json.Unmarshal([]byte(resBody), &bpodRes)
+	if err != nil {
+		return bpodRes, err
+	}
+
+	if res.StatusCode != 200 {
+		return bpodRes, fmt.Errorf("Unexpected response status code received: %d %s",
+			res.StatusCode, http.StatusText(res.StatusCode))
+	}
+
+	return bpodRes, err
+}
diff --git a/pkg/cetus/cetus.go b/pkg/cetus/cetus.go
index cca3a2e..8859ee8 100644
--- a/pkg/cetus/cetus.go
+++ b/pkg/cetus/cetus.go
@@ -22,3 +22,8 @@ var version string = "v0.4.3"
 func Version() {
 	fmt.Printf("Cetus %s\n", version)
 }
+
+// PrintPath prints the path passed
+func PrintPath(path string) {
+	fmt.Println(path)
+}