-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapodFetch.sh
237 lines (195 loc) · 6.28 KB
/
apodFetch.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
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
#!/bin/bash
#https://github.com/taurhine/apodFetch
##################
# static options #
######################################################
#default scale mode
defaultScaleMode="fill"
#effect to be applied to lock-screen background
# effectOpt=""
effectOpt="-paint 2 -noise 10"
#show description as notification on wallpaper change
descAsNotification="disabled"
#show apodFetch messages as notification
messagesAsNotification="enabled"
#show description in lock-screen
lockscreenDesc="enabled"
######################################################
hashNameExt=".apodcache"
wpPath="/home/""$(whoami)""/Documents/wallpapers/apod"
cachePath="/home/""$(whoami)"
linkPrefix="https://apod.nasa.gov/apod"
ShowMessage()
{
if test "$messagesAsNotification" = "enabled"; then
notify-send "$1" "$2"
else
echo "$1" "$2"
fi
}
CheckDependencies()
{
echo "notify-send
feh
wget
awk
sed
convert
rdjpgcom
html2text" | while read commandDep
do
if test "$(command -v $commandDep)" = ""; then
case "$commandDep" in
"notify-send")
messagesAsNotification="disabled"
ShowMessage "apodFetch: error" "package libnotify is missing"
;;
"convert")
ShowMessage "apodFetch: error" "package imagemagick is missing"
;;
"rdjpgcom")
ShowMessage "apodFetch: error" "package libjpeg-progs is missing"
;;
*)
ShowMessage "apodFetch: error" "package $commandDep is missing"
;;
esac
fi
done
exit 1
}
EnsurePathExists()
{
#create the destination path if it doesn't exist
if [ ! -d $1 ]; then
mkdir -p $1
fi
#abort execution if the path was not made
if [ ! -d $1 ]; then
ShowMessage "apodFetch: error" "Could not create ""\"$1\""
exit 1
fi
}
GetParameters()
{
#initialise the scaleMode with the default setting
scaleMode=$defaultScaleMode
#check the parameters to define the target date
case "$1" in
"")
#by default the target date will be set to today
targetDate=`date +"%Y%m%d"`
;;
"-d")
CheckDependencies
;;
"-r")
randomDay="$(( ( RANDOM % 365 ) + 1 ))"
targetDate=`date --date "-$randomDay day" +"%Y%m%d"`
;;
"-c")
#this option accepts numbers >= 0
if [[ $2 =~ ^[0-9]+$ ]]; then
targetDate=`date --date "-$2 day" +"%Y%m%d"`
elif test "$2" = "today"; then
targetDate=`date +"%Y%m%d"`
else
ShowMessage "apodFetch: error" "invalid parameter for option -c ""$2"
exit 1
fi
case "$3" in
"")
scaleMode=$defaultScaleMode
;;
"fill" | "fitall" | "center")
scaleMode=$3
;;
*)
ShowMessage "apodFetch: error" "unknown option ""$3"
exit 1
;;
esac
;;
*)
ShowMessage "apodFetch: error" "unknown option ""$1"
exit 1
;;
esac
}
GetDescription()
{
#read the description from the jpg file and escape the '
description=$(rdjpgcom $fileFullPath | sed "s/'/\\\'/g")
}
ShowDescriptionAsNotification()
{
if test "$descAsNotification" = "enabled"; then
notify-send -u critical "NASA Astronomy Picture for $targetDate" "$description"
fi
}
DownloadPicture()
{
fileName=$targetDate".jpg"
#check if we already have a picture from that date
fileFullPath=$wpPath/$fileName
#if the file does not exist try to download it
if [ ! -f $fileFullPath ]; then
#fetch the webpage
wget $linkPrefix/"ap"${targetDate:2:6}".html" -q -O $cachePath/cached.html
title="$(grep "<title>" $cachePath/cached.html | awk -F ' - ' '{print $2}')"
description="$(sed -n '/Explanation:/,/Tomorrow/p' $cachePath/cached.html | html2text | sed 's/_/ /g' | sed 's/Explanation: //g' | sed '$d')"
relativePicPath="$(awk "/${targetDate:0:4}/,/\"/" $cachePath/cached.html | grep "[\w]*g\"" | awk -F '"' '{print $2}')"
if [ ! $relativePicPath ]; then
ShowMessage "apodFetch: info" "No picture found for the date $targetDate"
exit 1
fi
#compile the full link string
fullLink=$linkPrefix/$relativePicPath
#download the picture and add description
wget -qO - $fullLink | wrjpgcom -replace -c "$title
$description" > $fileFullPath
fi
}
ScaleAndSetWallpaper()
{
#make sure that the file exists
if [ ! -f $fileFullPath ]; then
ShowMessage "apodFetch: error" "File not found: $fileFullPath"
exit 1;
fi
#get the current screen resolution
screenRes="$(xrandr --current | grep '\*' | awk -F ' ' '{print $1}')"
case $scaleMode in
"center")
resizeOpt=""
;;
"fill")
screenW="$(echo $screenRes | awk -F 'x' '{print $1}')"
resizeOpt="-resize $screenW"
;;
*)
resizeOpt="-resize $screenRes"
;;
esac
#scale and resize the picture for the current resolution
convert -background black $resizeOpt -extent $screenRes -gravity center -quiet $fileFullPath $wpPath"/apod.png"
GetDescription
if test "$effectOpt" = ""; then
#no effect selected, copy the same file
cp $wpPath"/apod.png" $wpPath"/apod_lock.png"
else
convert $effectOpt $wpPath"/apod.png" -quiet $wpPath"/apod_lock.png"
fi
#add description to the lock-screen
if test "$lockscreenDesc" = "enabled"; then
convert -quiet -pointsize 20 -fill white -undercolor black -draw "text 10,20 '$description' " $wpPath"/apod_lock.png" $wpPath"/apod_lock.png"
fi
#set the wallpaper
feh --no-fehbg --bg-tile $wpPath"/apod.png"
}
EnsurePathExists $cachePath
EnsurePathExists $wpPath
GetParameters "$1" "$2" "$3"
DownloadPicture
ScaleAndSetWallpaper
ShowDescriptionAsNotification