summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--README.org12
-rw-r--r--cmd/cetus/cetus.go8
-rw-r--r--pkg/unsplash/unsplash.go43
3 files changed, 62 insertions, 1 deletions
diff --git a/README.org b/README.org
index 894c5e9..03891cc 100644
--- a/README.org
+++ b/README.org
@@ -6,7 +6,8 @@ wallpapers.
 *Note*: Cetus is a work in-progress & many features are yet to be implemented.
 
 * Features
-- set a specific photo as wallpaper
+- set a specific photo as background
+- set a random photo as background
 - customize wallpaper size
 * Demo
 I just run some cetus commands on my computer, nothing fancy. I'll make better
@@ -24,3 +25,12 @@ photo-id flag can be used to set a specific photo as background.
 cetus -width 1920 -height 1080 \
       -photo-id=FzrjgIId6NU
 #+END_SRC
+** random
+random flag can be used to set a random photo as background, this is the default
+behaviour.
+#+BEGIN_SRC sh
+cetus -width 1920 -height 1080 \
+      -random
+
+cetus -width 1920 -height 1080 # same as above
+#+END_SRC
diff --git a/cmd/cetus/cetus.go b/cmd/cetus/cetus.go
index 35afd01..e6799c8 100644
--- a/cmd/cetus/cetus.go
+++ b/cmd/cetus/cetus.go
@@ -24,6 +24,7 @@ import (
 func main() {
 	var (
 		photoID string
+		random  bool
 
 		width  int
 		height int
@@ -32,6 +33,7 @@ func main() {
 	)
 
 	flag.StringVar(&photoID, "photo-id", "", "Unsplash Photo ID to set as background")
+	flag.BoolVar(&random, "random", true, "Set a random photo as background")
 
 	flag.IntVar(&width, "width", 1920, "Width of the image")
 	flag.IntVar(&height, "height", 1080, "Height of the image")
@@ -43,6 +45,12 @@ func main() {
 		errChk(err)
 		return
 	}
+
+	if random {
+		err = unsplash.SetRandom(width, height)
+		errChk(err)
+		return
+	}
 }
 
 func errChk(err error) {
diff --git a/pkg/unsplash/unsplash.go b/pkg/unsplash/unsplash.go
index a284ebc..7cd408a 100644
--- a/pkg/unsplash/unsplash.go
+++ b/pkg/unsplash/unsplash.go
@@ -16,6 +16,8 @@ package unsplash
 
 import (
 	"fmt"
+	"net/http"
+	"time"
 
 	"framagit.org/andinus/cetus/pkg"
 )
@@ -31,12 +33,53 @@ func SetFromID(photoID string, width int, height int) error {
 	return err
 }
 
+// SetRandom sets a random photo as background
+func SetRandom(width int, height int) error {
+	var path string
+	var err error
+
+	path, err = getPathRandom(width, height)
+	if err != nil {
+		return err
+	}
+	err = background.Set(path)
+	return err
+}
+
 func getPathFromID(photoID string) string {
 	var path string
 	path = fmt.Sprintf("%s/%s", "https://source.unsplash.com", photoID)
 	return path
 }
 
+func getPathRandom(width int, height int) (string, error) {
+	var err error
+	var path string
+	var reqPath string
+
+	client := http.Client{
+		Timeout: time.Second * 64,
+	}
+
+	reqPath = "https://source.unsplash.com"
+	reqPath = appendSizeToPath(reqPath, width, height)
+
+	req, err := http.NewRequest(http.MethodGet, reqPath, nil)
+	if err != nil {
+		return "", err
+	}
+
+	res, err := client.Do(req)
+	if err != nil {
+		return "", err
+	}
+	defer res.Body.Close()
+
+	// Unsplash Source redirects to the photo
+	path = res.Request.URL.String()
+	return path, nil
+}
+
 func appendSizeToPath(path string, width int, height int) string {
 	var size string