-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallpaper
executable file
·427 lines (343 loc) · 12.2 KB
/
wallpaper
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env bash
# (The MIT License)
#
# Copyright (c) 2018 - 2019 Mamadou Babaei
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
set +e
readonly FMT_OFF='\e[0m'
readonly FMT_INFO='\e[1;32m'
readonly FMT_WARN='\e[1;33m'
readonly FMT_ERR='\e[1;91m'
readonly FMT_FATAL='\e[1;31m'
readonly LOG_INFO="INFO"
readonly LOG_WARN="WARNING"
readonly LOG_ERR="ERROR"
readonly LOG_FATAL="FATAL"
readonly E_TRUE="true"
readonly E_FALSE="false"
readonly BASENAME="basename"
readonly CALLER="caller"
readonly CUT=$(which cut 2>/dev/null)
readonly CURL=$(which curl 2>/dev/null)
readonly DATE=$(which date 2>/dev/null)
readonly ECHO="echo"
readonly ECHO_FMT="echo -e"
readonly FIREFOX=$(which firefox 2>/dev/null)
readonly JQ=$(which jq 2>/dev/null)
readonly LOGGER="logger"
readonly PERL=$(which perl 2>/dev/null)
readonly PRINT="print"
readonly REV=$(which rev 2>/dev/null)
readonly TR=$(which tr 2>/dev/null)
readonly SCRIPT="${BASH_SOURCE[0]}"
readonly SCRIPT_NAME="$(${BASENAME} -- "${SCRIPT}")"
readonly SYSLOG_TAG="$(${BASENAME} -- "${SCRIPT}" | ${TR} '[:lower:]' '[:upper:]' | ${REV} | ${CUT} -d "." -f2- | ${REV})"
readonly SUBREDDIT_CATEGORY_ANIMALS="AnimalsBeingBros+AnimalsBeingDerps+AnimalsBeingJerks+aww+Eyebleach+likeus+rarepuppers"
readonly SUBREDDIT_CATEGORY_ART="Art+ArtPorn+Cinemagraphs+ExposurePorn+Graffiti+ImaginaryLandscapes+itookapicture"
readonly SUBREDDIT_CATEGORY_FOOD="Breadit+eatsandwiches+food+FoodPorn+grilledcheese+Pizza+slowcooking"
readonly SUBREDDIT_CATEGORY_IMAGINARY="ImaginaryBehemoths+ImaginaryCharacters+ImaginaryLandscapes+ImaginaryLeviathans+ImaginaryMindscapes+ImaginaryMonsters+ImaginaryTechnology"
readonly SUBREDDIT_CATEGORY_MAN_MADE="AbandonedPorn+carporn+CityPorn+CozyPlaces+DesignPorn+powerwashingporn+RoomPorn"
readonly SUBREDDIT_CATEGORY_NATURE="chemicalreactiongifs+EarthPorn+MacroPorn+physicsgifs+spaceporn+waterporn+WeatherGifs"
readonly DEFAULT_SUBREDDIT="r/AutumnPorn+EarthPorn+JunglePorn+desertporn+spaceporn" # https://scrolller.com/
readonly DEFAULT_SORT_BY="hot"
readonly DEFAULT_IS_NSFW_OK="${E_FALSE}"
readonly DEFAULT_BACKGROUND_COLOR="181614"
readonly DEFAULT_PICTURE_OPTION="zoom"
readonly DEFAULT_FIREFOX_VERSION_STRING="Mozilla Firefox 66.0"
readonly LOCAL_WALLPAPER_DIR="${HOME}/.cache/$( basename "$( readlink -f "$BASH_SOURCE" )" .sh )/"
readonly LOCAL_WALLPAPER_NAME="$(${DATE} +%Y-%m-%d-%H-%M-%S)"
function usage() {
readonly local message="${1}"
${ECHO}
if [[ -n "${message}" ]] ;
then
err "${message}${FMT_OFF}"
${ECHO}
fi
${ECHO_FMT} "${FMT_INFO}Correct usage:${FMT_OFF}"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}${SCRIPT_NAME} -h | [-r {r/subreddit}] [-s {sorty by}] [-n] [-b {background color}] [-o {picture option}] [-f {title regex}] [-z]${FMT_OFF}"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-h: shows this usage note"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-r: subreddit name or names prefixed with r/ and combined by a + sign (e.g. r/Art or r/Art+ArtPorn; default: ${DEFAULT_SUBREDDIT})${FMT_OFF}"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-s: reddit sort algorithm (e.g. hot, new, controversial, top, rising; default: ${DEFAULT_SORT_BY})${FMT_OFF}"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-n: allow nsfw wallpapers (no nsfw wallpaper is allowed by default, unless this flag is passed)${FMT_OFF}"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-b: hex rgb color in 'ffffff' format (default: ${DEFAULT_BACKGROUND_COLOR})${FMT_OFF}"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-o: picture option (e.g. scaled, zoom, centered, wallpaper, ...) (default: $DEFAULT_PICTURE_OPTION)"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-f: filter by title regex"
${ECHO}
${ECHO_FMT} " ${FMT_INFO}-z: randomize"
${ECHO}
if [[ -n "${message}" ]] ;
then
exit 1
else
exit 0
fi
}
function log()
{
local log_type=$1; shift
local line=$1; shift
local fmt=$1; shift
if [[ -n "$1" && -n "$@" ]] ;
then
${ECHO_FMT} "${fmt}[${log_type}] ${line} $@${FMT_OFF}"
${LOGGER} -t "${SYSLOG_TAG}" "${log_type} ${line} $@"
else
${ECHO_FMT} "${FMT_WARN}[${LOG_WARN}] ${line} A null log detected!${FMT_OFF}"
${LOGGER} -t "${SYSLOG_TAG}" "${LOG_WARN} ${line} A null log detected!"
fi
}
function info()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${LOG_INFO}" "${line}" "$FMT_INFO" "$@"
}
function warn()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${LOG_WARN}" "${line}" "${FMT_WARN}" "$@"
}
function err()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${LOG_ERR}" "${line}" "${FMT_ERR}" "$@"
}
function fatal()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${LOG_FATAL}" "${line}" "${FMT_FATAL}" "$@"
exit 1
}
if [[ -z "${CUT}" ]] ;
then
fatal "Could not find command cut!"
fi
if [[ -z "${CURL}" ]] ;
then
fatal "Could not find command curl!"
fi
if [[ -z "${DATE}" ]] ;
then
fatal "Could not find command date!"
fi
if [[ -z "${JQ}" ]] ;
then
fatal "Could not find command jq!"
fi
if [[ -z "${PERL}" ]] ;
then
fatal "Could not find command perl!"
fi
if [[ -z "${REV}" ]] ;
then
fatal "Could not find command rev!"
fi
if [[ -z "${TR}" ]] ;
then
fatal "Could not find command tr!"
fi
while getopts ":h :r: :s: :n :b: :o: :f: :z" ARG;
do
case ${ARG} in
h)
usage
;;
r)
SUBREDDIT=${OPTARG}
;;
s)
SORT_BY=${OPTARG}
;;
n)
IS_NSFW_OK="${E_TRUE}"
;;
b)
BACKGROUND_COLOR="#${OPTARG}"
;;
o)
PICTURE_OPTION="${OPTARG}"
;;
f)
TITLE_REGEX="${OPTARG}"
;;
z)
IS_RANDOM="${E_TRUE}"
;;
\?)
usage "Invalid option: '-${OPTARG}'!"
;;
esac
done
if [[ -z ${SUBREDDIT} ]] ;
then
SUBREDDIT=${DEFAULT_SUBREDDIT}
fi
readonly SUBREDDIT_REGEX="^r\/([a-zA-Z0-9_+]+)$"
if [[ ! "${SUBREDDIT}" =~ ${SUBREDDIT_REGEX} ]] ;
then
fatal "Invalid subreddit name! use r/subreddit or r/subreddit1+subreddit2 format!"
fi
if [[ -z ${SORT_BY} ]] ;
then
SORT_BY=${DEFAULT_SORT_BY}
fi
if [[ "${SORT_BY}" != "hot"
&& "${SORT_BY}" != "new"
&& "${SORT_BY}" != "controversial"
&& "${SORT_BY}" != "top"
&& "${SORT_BY}" != "rising" ]] ;
then
fatal "Invalid reddit sort algorithm!"
fi
if [[ -z ${IS_NSFW_OK} ]] ;
then
NSFW_OK=${DEFAULT_IS_NSFW_OK}
fi
if [[ -z ${BACKGROUND_COLOR} ]] ;
then
BACKGROUND_COLOR="#${DEFAULT_BACKGROUND_COLOR}"
fi
readonly BACKGROUND_COLOR_REGEX="^#([0-9a-f]{6})$"
if [[ ! "${BACKGROUND_COLOR}" =~ ${BACKGROUND_COLOR_REGEX} ]] ;
then
fatal "Invalid background color format! the only accepted format is 'ffffff'!"
fi
if [[ -z ${PICTURE_OPTION} ]] ;
then
PICTURE_OPTION=${DEFAULT_PICTURE_OPTION}
fi
case "${PICTURE_OPTION}" in
centered|scaled|stretched|spanned|wallpaper|zoom) ;;
*) fatal "Invalid picture options!"
esac
if [[ -z "${FIREFOX}" ]] ;
then
warn "Firefox executable not found!"
readonly FIREFOX_VERSION_STRING="${DEFAULT_FIREFOX_VERSION_STRING}"
warn "Setting Firefox version string to: ${FIREFOX_VERSION_STRING}"
else
readonly FIREFOX_VERSION_STRING=$(${FIREFOX} -version)
fi
readonly FIREFOX_VERSION_NUMBER=$(${ECHO} "${FIREFOX_VERSION_STRING}" | ${PERL} -nle "m/[-+]?([0-9]*\.[0-9]+|[0-9]+)/; ${PRINT} \$1")
readonly FIREFOX_USER_AGENET="Mozilla/5.0 (X11; Linux x86_64; rv:${FIREFOX_VERSION_NUMBER}) Gecko/20100101 Firefox/${FIREFOX_VERSION_NUMBER}"
info "Run '${SCRIPT_NAME} -h' for more information on available options."
info "Setting user agent to '${FIREFOX_USER_AGENET}'..."
readonly JSON_URL="https://www.reddit.com/${SUBREDDIT}/${SORT_BY}.json"
info "Downloading meta file '${JSON_URL}'..."
readonly JSON_CONTENT=$(${CURL} -A "${FIREFOX_USER_AGENET}" -sSL ${JSON_URL})
RC=$?
if [[ $RC -ne 0 ]] ;
then
fatal "Subreddit meta file download has failed!"
fi
readarray JSON_POSTS <<< "$( ${JQ} --compact-output '.data.children[]'"${TITLE_REGEX+|select(.data.title|test(\"$TITLE_REGEX\";\"i\"))}" <<<"$JSON_CONTENT" )"
RCS=(${PIPESTATUS[*]})
if [[ ${RCS[0]} -ne 0 || ${RCS[1]} -ne 0 ]] ;
then
fatal "Failed to parse the subreddit's meta file!"
fi
if [[ ${IS_RANDOM} == "${E_TRUE}" ]] ; then
# FROM http://mywiki.wooledge.org/BashFAQ/026
for ((i=${#JSON_POSTS[*]}-1; i>0; i--)); do
max=$(( 32768 / (i+1) * (i+1) ))
while (( (rand=$RANDOM) >= max )); do : ; done
rand=$(( rand % (i+1) ))
tmp=${JSON_POSTS[i]} JSON_POSTS[i]=${JSON_POSTS[rand]} JSON_POSTS[rand]=$tmp
done
fi
readonly URL_REGEX="^(https?://)?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)\\.(png|apng|jpg|jpeg|jpe|jif|jfif|jfi|gif|tiff|tif)$"
FOUND_A_SUITABLE_WALLPAPER="${E_FALSE}"
for post in "${JSON_POSTS[@]}" ;
do
if [[ "${IS_NSFW_OK}" != "${E_TRUE}" ]] ;
then
IS_OVER_18_POST=$(${ECHO} "${post}" | ${JQ} --compact-output '.data.over_18')
RCS=(${PIPESTATUS[*]})
if [[ ${RCS[0]} -ne 0 || ${RCS[1]} -ne 0 ]] ;
then
fatal "Failed to parse the subreddit's meta file!"
fi
if [[ "${IS_OVER_18_POST}" == "${E_TRUE}" ]] ;
then
continue
fi
fi
WALLPAPER_URL=$(${ECHO} "${post}" | ${JQ} --raw-output '.data.url')
RCS=(${PIPESTATUS[*]})
if [[ ${RCS[0]} -ne 0 || ${RCS[1]} -ne 0 ]] ;
then
fatal "Failed to parse the subreddit's meta file!"
fi
if [[ ! "${WALLPAPER_URL}" =~ ${URL_REGEX} ]] ;
then
continue
fi
WALLPAPER_EXTENSION="${WALLPAPER_URL##*.}"
if [[ -z "${WALLPAPER_EXTENSION}" ]] ;
then
continue
fi
FOUND_A_SUITABLE_WALLPAPER="${E_TRUE}"
break;
done
if [[ "${FOUND_A_SUITABLE_WALLPAPER}" != "${E_TRUE}" ]] ;
then
fatal "Could not find a suitable wallpaper on '${SUBREDDIT}'!"
fi
readonly LOCAL_WALLPAPER_PATH="${LOCAL_WALLPAPER_DIR}/${LOCAL_WALLPAPER_NAME}.${WALLPAPER_EXTENSION}"
info "Found a wallpaper on '${SUBREDDIT}' at '${WALLPAPER_URL}'!"
info "Fetching '${WALLPAPER_URL}'..."
${CURL} -fLo "${LOCAL_WALLPAPER_PATH}" --create-dirs ${WALLPAPER_URL} > /dev/null 2>&1
RC=$?
if [[ $RC -ne 0 ]] ;
then
fatal "Failed to fetch the wallpaper file from '${WALLPAPER_URL}'!"
fi
info "Setting desktop background color to '${BACKGROUND_COLOR}'..."
gsettings set org.gnome.desktop.background primary-color "${BACKGROUND_COLOR}"
RC=$?
if [[ $RC -ne 0 ]] ;
then
fatal "Failed to set the background color to '${BACKGROUND_COLOR}'!"
fi
info "Setting desktop picture options to '${PICTURE_OPTION}'..."
gsettings set org.gnome.desktop.background picture-options "${PICTURE_OPTION}"
RC=$?
if [[ $RC -ne 0 ]] ;
then
fatal "Failed to set the background options to '${PICTURE_OPTION}'!"
fi
info "Using '${WALLPAPER_URL}' as the desktop wallpaper..."
gsettings set org.gnome.desktop.background picture-uri "file://${LOCAL_WALLPAPER_PATH}"
RC=$?
if [[ $RC -ne 0 ]] ;
then
fatal "Failed to apply '${LOCAL_WALLPAPER_PATH}' as the desktop background image!"
fi
info "Done!"
info "Hope you enjoy it :)"