-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdynamicWallpaper.sh
executable file
·191 lines (160 loc) · 5.42 KB
/
dynamicWallpaper.sh
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
#!/bin/bash
[[ "$(uname -s)" = "Darwin" ]] && PATH="/usr/local/bin:${PATH}" && export PATH
command -v gdate >/dev/null 2>&1 && date() { command gdate "${@}"; }
load_prev_weather () {
w_type=$(sed '1q;d' /tmp/prev_weather.dat)
t_type=$(sed '2q;d' /tmp/prev_weather.dat)
rm /tmp/prev_weather.dat
}
get_weather () {
weatherdata=$(curl -s "wttr.in/?format=j1")
if jq -e . >/dev/null 2>&1 <<< "$weatherdata"; then
temp=$(echo "$weatherdata" | jq -r '.current_condition[0].temp_F' )
w_type=$(echo "$weatherdata" | jq -r '.current_condition[0].weatherDesc[0].value')
sunrise=$(echo "$weatherdata" | jq -r '.weather[0].astronomy[0].sunrise' | tr -d ":AM[:space:]" | sed 's/^0//')
sunset=$(echo "$weatherdata" | jq -r '.weather[0].astronomy[0].sunset'| tr -d ":PM[:space:]" | sed 's/^0//')
sunset=$((sunset + 1200))
else
echo "wttr.in failure, using previous weather."
wttr_fail=1
fi
}
get_time_chunk () {
time_day=$(date +%-k%M)
echo "$sunrise ~ $time_day ~ $sunset"
t_type="Day"
if (( time_day < sunrise )); then
t_type="Night"
elif (( time_day >= sunrise )) && (( time_day < sunrise + 130 )); then
t_type="Morning"
elif (( time_day < sunset - 130 )) && (( time_day > sunrise + 130 )); then
t_type="Day"
elif (( time_day > sunset - 100 )) && (( time_day < sunset )); then
t_type="Evening"
else
t_type="Night"
fi
}
get_simple_weather () {
echo "Temp: $temp"
if echo "$w_type" | grep -i -q -E "sun|sunny|clear" && (( temp >= 34 )); then
w_type="Sun"
elif echo "$w_type" | grep -q -i "snow" || ((temp < 34)); then
w_type="Snow"
elif echo "$w_type" | grep -i -q -E "rain|overcast|Light drizzle"; then
w_type="Rain"
else
w_type="Misc"
fi
}
set_pape () {
# if ! pape=$(find "$pape_prefix"/"$t_type"/"$w_type"/* "$pape_prefix"/"$t_type"/Misc/* | shuf -n 1); then
if ! pape=$(find "$pape_prefix"/"$t_type"/"$w_type"/* | shuf -n 1); then
pape=$(find "$pape_prefix"/"$t_type"/* | shuf -n 1)
fi
if [[ $embed -eq 1 ]]; then
if which osascript; then
res=$(/usr/sbin/system_profiler SPDisplaysDataType | grep Resolution | awk '{print $2, $4}' | tr " " "x")
else
pos_res=$(xrandr | awk '/ connected.*[0-9]+x[0-9]+/ {if ($3 == "primary") {print $4} else {print $3} }' | cut -f 1 -d '+')
res=$(echo "$pos_res" | head -n 1)
fi
echo "Res: $res"
convert "$pape" -resize "$res" resized_pape.png
pape="resized_pape.png"
rm embed_pape_*
convert <( curl -s "wttr.in/_tqp0.png" ) weather_report.png
convert "$pape" "weather_report.png" -gravity center -geometry +0+0 -composite "embed_pape.png"
stamped_pape="embed_pape_$(date +%T).png"
cp embed_pape.png "$stamped_pape"
convert <( curl -s "wttr.in/_tqp0.png" ) -resize 175% weather_report.png
convert "$pape" "weather_report.png" -gravity center -geometry +0+0 -composite "$stamped_pape"
pape="$stamped_pape"
fi
pape=$(realpath $pape)
if which osascript; then
rm ~/Pictures/Weather\ Wallpaper/*
cp "$pape" ~/Pictures/Weather\ Wallpaper/
else
case $XDG_CURRENT_DESKTOP in
*XFCE*)
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$UID/bus"
xfconf-query -c xfce4-desktop -l | \
grep --color=never last-image | \
while read -r path; do xfconf-query --channel xfce4-desktop --property "$path" -s "$pape"; done
;;
*GNOME*)
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$UID/bus"
gsettings set org.gnome.desktop.background picture-uri "file://$pape"
;;
*)
feh --randomize --bg-fill "$pape"
;;
esac
fi
if [[ $use_wal -eq 1 ]]; then
if ! which wal; then
PATH="$HOME/.local/bin":${PATH} && export PATH
fi
if which wal; then
wal -i "$pape" -n --saturate 1.0
else
echo "Trying to use wal but it's not installed"
fi
fi
rm weather_report.png
rm resized_pape.png
#rm "$stamped_pape"
rm /tmp/prev_weather.dat
echo "$w_type" >> /tmp/prev_weather.dat
echo "$t_type" >> /tmp/prev_weather.dat
}
exit_help () {
echo "ERROR: $1"
echo "USAGE: dynamicWallpaper -p [PAPER_PREFIX] -w [AIRPORT_CALLSIGN]"
printf "\t-p: The prefix of your wallpaper folder without a trailing /\n"
printf "\t-e: Embed a little weather preview png in the wallpaper\n"
printf "\t--wal: Use pywal if it's installed\n"
exit 1
}
pape_prefix="0"
use_wal=0
embed=0
wttr_fail=0
while [[ $# -gt 0 ]]; do
arg="$1"
case $arg in
-p)
pape_prefix="$2"
shift; shift
;;
--wal)
use_wal=1
shift;
;;
-e)
embed=1
shift;
;;
*)
exit_help "Unrecognized argument"
;;
esac
done
if [[ "$pape_prefix" == "0" ]]; then
exit_help "The paper prefix is unset."
fi
get_weather
echo "Real Weather: $w_type"
if [[ wttr_fail -eq 1 ]]; then
load_prev_weather
embed=0
else
get_time_chunk
get_simple_weather
fi
echo "Time Type: $t_type, Weather Type: $w_type"
if [[ "$t_type" == "Night" ]] && [[ "$w_type" == "Sun" ]]; then
w_type="Misc"
fi
set_pape