summary refs log tree commit diff stats
path: root/main.go
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-12 14:42:32 +0530
committerAndinus <andinus@inventati.org>2020-03-12 14:42:32 +0530
commit08c68e60ea7c4661bbf9083629842d516caff1ee (patch)
tree6e8ecab5f838f4007df36ada543e6a6abebb64ec /main.go
parent0e51e2944ac86cb3e60df92700bd3affda5c890a (diff)
downloadcetus-08c68e60ea7c4661bbf9083629842d516caff1ee.tar.gz
Support Unsplash Source & Astronomy Picture of the Day
Diffstat (limited to 'main.go')
-rw-r--r--main.go120
1 files changed, 77 insertions, 43 deletions
diff --git a/main.go b/main.go
index 95e91fa..c8ad829 100644
--- a/main.go
+++ b/main.go
@@ -35,6 +35,9 @@ var (
 	bpodAPI     string
 	bpodNum     int
 	unsplashAPI string
+
+	width  int
+	height int
 )
 
 func main() {
@@ -57,10 +60,14 @@ func main() {
 	flag.StringVar(&src, "src", "random", "Source for the image")
 	flag.StringVar(&mode, "mode", "random", "Daily, Weekly or Random wallpaper")
 
+	flag.IntVar(&width, "width", 1920, "Width of the image")
+	flag.IntVar(&height, "height", 1080, "Height of the image")
+
 	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.StringVar(&bpodAPI, "bpod-api", "https://www.bing.com/HPImageArchive.aspx", "BPOD API URL")
 	flag.IntVar(&bpodNum, "bpod-num", 16, "BPOD Number of images to fetch")
+	flag.StringVar(&unsplashAPI, "unsplash-api", "https://source.unsplash.com", "Unsplash Source API URL")
 	flag.DurationVar(&timeout, "timeout", 16, "Timeout for http client")
 	flag.Parse()
 
@@ -112,6 +119,53 @@ func parseSrcAndGetPath(src string, mode string) (string, error) {
 func getPathAPOD(mode string) (string, error) {
 	var err error
 	var imgPath string
+
+	switch mode {
+	case "daily", "random":
+		break
+	default:
+		return "", fmt.Errorf("Error: Unknown Mode")
+	}
+
+	type apodRes struct {
+		Copyright      string `json:"copyright"`
+		Date           string `json:"string"`
+		Explanation    string `json:"explanation"`
+		HDURL          string `json:"hdurl"`
+		MediaType      string `json:"media_type"`
+		ServiceVersion string `json:"service_version"`
+		Title          string `json:"title"`
+		URL            string `json:"url"`
+	}
+
+	apodNow := apodRes{}
+
+	req, err := http.NewRequest(http.MethodGet, apodAPI, nil)
+	if err != nil {
+		return "", err
+	}
+	q := req.URL.Query()
+	q.Add("api_key", apodAPIKey)
+	req.URL.RawQuery = q.Encode()
+
+	res, err := getRes(req)
+	if err != nil {
+		fmt.Printf("Error: GET %s\n", apodAPI)
+		return "", err
+	}
+	defer res.Body.Close()
+
+	apiBody, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		return "", err
+	}
+
+	err = json.Unmarshal([]byte(apiBody), &apodNow)
+	if err != nil {
+		return "", err
+	}
+
+	imgPath = apodNow.HDURL
 	return imgPath, err
 }
 
@@ -182,65 +236,45 @@ func getPathBPOD(mode string) (string, error) {
 func getPathUnsplash(mode string) (string, error) {
 	var err error
 	var imgPath string
-	return imgPath, err
-}
-
-// Calls feh to set the wallpaper
-func setWall(imgPath string) error {
-	feh, err := exec.LookPath("feh")
-	if err != nil {
-		fmt.Println("Error: feh is not in $PATH")
-		return err
-	}
-
-	fmt.Printf("Path to set as Wallpaper: %s\n", imgPath)
-
-	err = exec.Command(feh, "--bg-fill", imgPath).Run()
-	return err
-}
 
-// Get url of Astronomy Picture of the Day & pass it to setWall()
-func setWallFromAPOD(apodI map[string]string) error {
-	type apodRes struct {
-		Copyright      string `json:"copyright"`
-		Date           string `json:"string"`
-		Explanation    string `json:"explanation"`
-		HDURL          string `json:"hdurl"`
-		MediaType      string `json:"media_type"`
-		ServiceVersion string `json:"service_version"`
-		Title          string `json:"title"`
-		URL            string `json:"url"`
+	switch mode {
+	case "daily", "weekly":
+		unsplashAPI = fmt.Sprintf("%s/%s",
+			unsplashAPI, mode)
+	case "random":
+		unsplashAPI = fmt.Sprintf("%s/%sx%s",
+			unsplashAPI, strconv.Itoa(width), strconv.Itoa(height))
+	default:
+		return "", fmt.Errorf("Error: Unknown Mode")
 	}
 
-	apodNow := apodRes{}
-
-	req, err := http.NewRequest(http.MethodGet, apodI["api"], nil)
+	req, err := http.NewRequest(http.MethodGet, unsplashAPI, nil)
 	if err != nil {
-		return err
+		return "", err
 	}
-	q := req.URL.Query()
-	q.Add("api_key", apodI["apiKey"])
-	req.URL.RawQuery = q.Encode()
 
 	res, err := getRes(req)
 	if err != nil {
-		fmt.Printf("Error: GET %s\n", apodI["api"])
-		return err
+		return "", err
 	}
 	defer res.Body.Close()
 
-	apiBody, err := ioutil.ReadAll(res.Body)
-	if err != nil {
-		return err
-	}
+	// Unsplash Source API will redirect to the image
+	imgPath = res.Request.URL.String()
+	return imgPath, err
+}
 
-	err = json.Unmarshal([]byte(apiBody), &apodNow)
+// Calls feh to set the wallpaper
+func setWall(imgPath string) error {
+	feh, err := exec.LookPath("feh")
 	if err != nil {
+		fmt.Println("Error: feh is not in $PATH")
 		return err
 	}
 
-	// Set Astronomy Picture of the Day as wallpaper
-	err = setWall(apodNow.HDURL)
+	fmt.Printf("Path to set as Wallpaper: %s\n", imgPath)
+
+	err = exec.Command(feh, "--bg-fill", imgPath).Run()
 	return err
 }