From 698021e8a84f907a75df875ae8c9b920624e4a51 Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 12 Mar 2020 03:42:01 +0530 Subject: Add support for Bing Photo of the Day --- main.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'main.go') 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 { -- cgit 1.4.1-2-gfad0