summary refs log tree commit diff stats
path: root/pkg/unsplash/unsplash.go
diff options
context:
space:
mode:
authorAndinus <andinus@inventati.org>2020-03-14 14:18:30 +0530
committerAndinus <andinus@inventati.org>2020-03-14 14:18:30 +0530
commitdde2d726d5b65e648b99e45538221014562f1a18 (patch)
tree1db38a43964b205a6dcbb0c160c66213fdffd94e /pkg/unsplash/unsplash.go
parent82a53c3c5b5ffd8af9c69c6184134f100f80b89a (diff)
downloadcetus-dde2d726d5b65e648b99e45538221014562f1a18.tar.gz
Add support for random photo as background v0.3.1
Diffstat (limited to 'pkg/unsplash/unsplash.go')
-rw-r--r--pkg/unsplash/unsplash.go43
1 files changed, 43 insertions, 0 deletions
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
 
er/commit/ranger/gui/widgets/filelistcontainer.py?id=705c14a48645f6e7e4b318a5a3f4f7a6c7b96c80'>705c14a4 ^
16246965 ^
705c14a4 ^


49ae0dd1 ^




8db3c4b1 ^
a6791aee ^
49ae0dd1 ^

8db3c4b1 ^
49ae0dd1 ^
8db3c4b1 ^
49ae0dd1 ^
8db3c4b1 ^
77f59786 ^
06aefcf5 ^
c1033553 ^
a6791aee ^
08f21ae5 ^























49ae0dd1 ^

8db3c4b1 ^
49ae0dd1 ^
d3eff0a9 ^
d621586e ^
705c14a4 ^

d621586e ^

705c14a4 ^
d621586e ^


06aefcf5 ^

d621586e ^
49ae0dd1 ^
705c14a4 ^
06aefcf5 ^
705c14a4 ^

06aefcf5 ^
d3eff0a9 ^
06aefcf5 ^
49ae0dd1 ^
d3eff0a9 ^
49ae0dd1 ^

705c14a4 ^
49ae0dd1 ^
d621586e ^
b4cb1f7d ^








06aefcf5 ^
a6791aee ^

06aefcf5 ^

a6791aee ^

06aefcf5 ^



a6791aee ^

06aefcf5 ^

a6791aee ^

06aefcf5 ^


d621586e ^

ef81960b ^
06aefcf5 ^
d621586e ^

d3eff0a9 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137