-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-wallhaven
executable file
·78 lines (68 loc) · 2.73 KB
/
auto-wallhaven
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
#!/bin/bash
# default path is /toplist
curl_path="toplist"
autoset=""
autodelete=""
autoresize=""
# handle args
for arg in $*; do
case $arg in
"-l"|"--latest")
curl_path="latest"
;;
"-r"|"--random")
curl_path="random"
;;
"-t"|"--toplist")
curl_path="toplist"
;;
"--tag="*)
curl_path="search?q=id%3A${arg:6}"
;;
# automatically resize image to the size of the primary monitor according to xrandr
"--resize")
autoresize="true"
;;
"-a"|"--autoset")
autoset="true"
;;
# only functions with --autoset, pass this flag to automatically delete old wallhaven downloads in home before moving and setting the new one
"-d"|"--autodelete")
autodelete="true"
;;
"-fa"|"--fully-automatic")
autoresize="true"
autoset="true"
autodelete="true"
;;
esac
done
# download page for requested url path
site_contents="$(curl https://wallhaven.cc/${curl_path})"
# find all images listed on the page
IFS=$'\n' # must change IFS for this loop to function
ids=()
for link in $(echo -e "${site_contents//>/>\\n}" | grep '<img.*lazyload\|lazyload.*<img'); do
# turn the full html element into a single id
id="$(echo "$link" | cut -d "\"" -f 6 | cut -c9- | cut -d "/" -f4 | cut -d "." -f1)"
# add the id to an array
ids+=( $id )
done
# select a random id
random_choice="${ids[$(( $RANDOM % ${#ids[*]} ))]}"
# download the image from the random id, fail on error 404
# note that for some reason the url must include the first two
# characters of the image's id before a slash and the actual image
# id. I have no idea what the advantage of structuring the storage
# this way is on their end, but it's important to keep in mind.
curl "https://w.wallhaven.cc/full/${random_choice:0:2}/wallhaven-${random_choice}.jpg" -o /tmp/wallhaven -f || \
curl "https://w.wallhaven.cc/full/${random_choice:0:2}/wallhaven-${random_choice}.png" -o /tmp/wallhaven -f || \
(echo "could not download image id \"$random_choice\"" && exit 1)
# autodelete if proper flags passed
[ ! -z "$autoset" ] && [ ! -z "$autodelete" ] && rm ~/wallhaven*
# autoresize if proper flag passed (requires imagemagick)
[ ! -z "$autoresize" ] && convert /tmp/wallhaven -resize "$(xrandr | grep 'primary' | cut -d ' ' -f4 | cut -d '+' -f1)" /tmp/wallhaven
# autoset if proper flag passed, otherwise open with feh
[ -z "$autoset" ] && feh /tmp/wallhaven && echo "set as wallpaper?" && read yn && [[ $yn =~ y|yes|YES|Y ]] && mv /tmp/wallhaven ~/wallhaven-${random_choice} && echo -e "#!/bin/sh\nfeh --no-fehbg --bg-scale '$HOME/wallhaven-${random_choice}'" > ~/.fehbg && bash ~/.fehbg
[ ! -z "$autoset" ] && mv /tmp/wallhaven ~/wallhaven-${random_choice} && echo -e "#!/bin/sh\nfeh --no-fehbg --bg-scale '$HOME/wallhaven-${random_choice}'" > ~/.fehbg && bash ~/.fehbg
# vim: syntax=sh