summary refs log tree commit diff stats
path: root/main.go
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-12 03:42:01 +0530
committerAndinus <andinus@inventati.org>2020-03-12 03:42:01 +0530
commit698021e8a84f907a75df875ae8c9b920624e4a51 (patch)
tree1fdf090d379555a0c6f18588e628c3180743d04b /main.go
parentdc639afe4e15372e1fd68baa72571484156d1dcd (diff)
downloadcetus-698021e8a84f907a75df875ae8c9b920624e4a51.tar.gz
Add support for Bing Photo of the Day
Diffstat (limited to 'main.go')
-rw-r--r--main.go68
1 files changed, 67 insertions, 1 deletions
diff --git a/main.go b/main.go
index 9f506f4..3f52fb8 100644
--- a/main.go
+++ b/main.go
@@ -37,6 +37,9 @@ func main() {
 		apod       bool
 		apodAPI    string
 		apodAPIKey string
+
+		bpod    bool
+		bpodAPI string
 	)
 
 	// Parse flags passed to program
@@ -46,6 +49,9 @@ func main() {
 	flag.StringVar(&apodAPI, "apod-api", "https://api.nasa.gov/planetary/apod", "APOD API URL")
 	flag.StringVar(&apodAPIKey, "apod-api-key", "DEMO_KEY", "APOD API Key")
 
+	flag.BoolVar(&bpod, "bpod", false, "Set Bing Photo of the Day as wallpaper")
+	flag.StringVar(&bpodAPI, "bpod-api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API URL")
+
 	flag.DurationVar(&timeout, "timeout", 16, "Timeout for http client")
 
 	flag.Parse()
@@ -56,7 +62,7 @@ func main() {
 		return
 	}
 
-	if apod != false {
+	if apod {
 		apodI := make(map[string]string)
 		apodI["api"] = apodAPI
 		apodI["apiKey"] = apodAPIKey
@@ -65,6 +71,15 @@ func main() {
 		errChk(err)
 		return
 	}
+
+	if bpod {
+		bpodI := make(map[string]string)
+		bpodI["api"] = bpodAPI
+
+		err = setWallFromBPOD(bpodI)
+		errChk(err)
+		return
+	}
 }
 
 // Calls feh to set the wallpaper
@@ -81,6 +96,57 @@ func setWall(imgPath string) error {
 	return err
 }
 
+// Get url of Bing Photo of the Day & pass it to setWall()
+func setWallFromBPOD(bpodI map[string]string) error {
+	type Images 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 bpodRes struct {
+		Image []Images `json:"images"`
+	}
+
+	bpodNow := bpodRes{}
+
+	req, err := http.NewRequest(http.MethodGet, bpodI["api"], nil)
+	if err != nil {
+		return err
+	}
+	q := req.URL.Query()
+	q.Add("format", "js")
+	q.Add("n", "1")
+	req.URL.RawQuery = q.Encode()
+
+	res, err := getRes(req)
+	if err != nil {
+		fmt.Printf("Error: GET %s\n", bpodI["api"])
+		return err
+	}
+	defer res.Body.Close()
+
+	apiBody, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return err
+	}
+
+	err = json.Unmarshal([]byte(apiBody), &bpodNow)
+	if err != nil {
+		return err
+	}
+
+	// Set Bing Photo of the Day as wallpaper
+	err = setWall(fmt.Sprintf("%s%s", "https://www.bing.com", bpodNow.Image[0].URL))
+	return err
+}
+
 // Get url of Astronomy Picture of the Day & pass it to setWall()
 func setWallFromAPOD(apodI map[string]string) error {
 	type apodRes struct {