-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
227 lines (196 loc) · 4.86 KB
/
job.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"bytes"
"errors"
"image"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
"unicode"
"github.com/disintegration/imaging"
"github.com/kkyr/wallpaper"
)
const (
randomMax = 20
fetchLimit = 20
)
type savedImage struct {
url string
image image.Image
isDark bool
}
func start() {
savedSubreddit := mainApp.Preferences().String("subreddits")
savedSubreddit = trimWhiteSpace(savedSubreddit)
savedSorting := mainApp.Preferences().String("sorting")
savedPreferDarker := mainApp.Preferences().String("prefer_darker")
savedDeepscan := mainApp.Preferences().Bool("deepscan")
subreddit := randomElement(strings.Split(savedSubreddit, ","))
randomIndex, deathCounter := 0, 0
if mainApp.Preferences().String("first_or_random") == "random" {
rand.Seed(time.Now().UnixNano())
randomIndex = rand.Intn(randomMax-1) + 1
}
var finalImage, lastImage *savedImage = nil, nil
afterID := ""
for finalImage == nil {
newLogInfo("Getting a new page from: " + subreddit + " , afterID=" + afterID)
payload, newAfterID, err := getReddit(subreddit, savedSorting, afterID)
if err != nil {
newLogError("cannot get reddit", err)
return
}
afterID = newAfterID
for _, v := range payload.Data.Children {
var image *savedImage = nil
if v.Data.Preview.Images == nil {
if savedDeepscan {
image, err = getImageDeepscan(&v)
if err != nil {
newLogError("ImageDeepscan error", err)
continue
}
} else {
continue
}
} else {
image, err = getImage(&v)
if err != nil {
newLogError("get image error", err)
continue
}
}
if image != nil {
if savedPreferDarker == "dark_image" || savedPreferDarker == "dim_image" {
isDark, err := checkDarkImage(image.image)
if err != nil {
newLogError("check dark image error", err)
continue
}
image.isDark = isDark
if !isDark && savedPreferDarker != "dim_image" {
newLogInfo("Skip image because it's not dark: " + image.url)
continue
}
}
lastImage = image
randomIndex--
if randomIndex <= 0 {
finalImage = lastImage
break
}
}
}
deathCounter++
if deathCounter > fetchLimit {
break
}
}
if finalImage == nil {
if lastImage != nil {
finalImage = lastImage
} else {
newLogError("", errors.New("No image found"))
return
}
}
if savedPreferDarker == "dim_image" {
newLogInfo("Dimming brightness")
image := dimImage(finalImage.image)
finalImage.image = image
}
path, err := saveWallper(finalImage.image)
if err != nil {
newLogError("set wallpaper error", err)
} else {
newLogInfo("Successfully set wallpaper: " + finalImage.url)
newLogInfo("Saved to: " + path)
}
}
func randomElement(ls []string) string {
randomIndex := rand.Intn(len(ls))
return ls[randomIndex]
}
func trimWhiteSpace(text string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, text)
}
func saveWallper(img image.Image) (string, error) {
tmpfile, err := ioutil.TempFile("", "go-reddit-wallpaper-temp-")
if err != nil {
return "", err
}
byteArr, err := encodePNG(img)
if err != nil {
return "", err
}
_, err = tmpfile.Write(byteArr)
if err != nil {
return "", err
}
err = tmpfile.Close()
if err != nil {
return "", err
}
err = wallpaper.SetFromFile(tmpfile.Name())
return tmpfile.Name(), err
}
func getImage(v *PayloadDataChild) (*savedImage, error) {
minWidth := mainApp.Preferences().Int("min_width")
minHeight := mainApp.Preferences().Int("min_height")
width := v.Data.Preview.Images[0].Source.Width
height := v.Data.Preview.Images[0].Source.Height
if width >= minWidth && height >= minHeight {
url := fixPreviewURL(v.Data.Preview.Images[0].Source.URL)
image, err := downloadImage(url)
if err != nil {
return nil, err
}
return &savedImage{url: url, image: image}, nil
}
return nil, nil
}
func getImageDeepscan(v *PayloadDataChild) (*savedImage, error) {
minWidth := mainApp.Preferences().Int("min_width")
minHeight := mainApp.Preferences().Int("min_height")
url := v.Data.URL
if url == "" {
return nil, nil
}
if url[len(url)-4:] != ".png" && url[len(url)-4:] != ".jpg" && url[len(url)-5:] != ".jpeg" {
return nil, nil
}
image, err := downloadImage(url)
if err != nil {
return nil, err
}
width, height, err := getDimensions(image)
if err != nil {
return nil, err
}
if width >= minWidth && height >= minHeight {
return &savedImage{url: fixPreviewURL(url), image: image}, nil
}
return nil, nil
}
func downloadImage(url string) (image.Image, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
b := buf.Bytes()
decodedImage, err := imaging.Decode(bytes.NewReader(b))
return decodedImage, err
}
func fixPreviewURL(url string) string {
return strings.Replace(url, "amp;", "", -1)
}