-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.sh
executable file
·103 lines (86 loc) · 2.46 KB
/
index.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
#!/usr/bin/env bash
set -e
# Config
WALLPAPER_DIR="$HOME/.wallpaper"
ZOOM_BACKGROUND_PATH="$HOME/.zoom/data/VirtualBkgnd_Custom/{a9e6f18c-18d7-4d29-a4b9-758f3f87256b}"
TODAY_WP_PATH="${WALLPAPER_DIR}/today_wallpaper"
APOD_API_KEY="DEMO_KEY" # A key can be generated at https://api.nasa.gov
# But the DEMO_KEY allows for 50 calls/day and 30 calls/hour from the same IP
# Which is good enough for a simple daily/hourly wallpaper change
# Arguments
nozoom=false
nowallpaper=false
while getopts 'zw' name
do
case $name in
'z') nozoom=true;;
'w') nowallpaper=true;;
?) echo "Usage: [-z] (no zoom) [-w] (no wallpaper)"
exit 2;;
esac
done
echom() {
echo "nwotd [$(date +"%Y/%m/%d %H:%M:%S")] $*"
}
fileIsImage() {
if file "$1" |grep -qE 'image|bitmap'; then
return 0;
fi
return 1;
}
APOD_API_URL="https://api.nasa.gov/planetary/apod?api_key=$APOD_API_KEY"
downloadImage() {
todayDetails=$(curl -s "$APOD_API_URL")
imageURL=$(jq -r '.url' <<< "$todayDetails" )
imageName=${imageURL//*\//} # Replace everything until last slash by nothing
imagePath="${WALLPAPER_DIR}/${imageName}"
if [ -f "$imagePath" ]; then
echom "Image already exists $imagePath"
return 0
fi
echom "Download new image to $imagePath"
curl -s "$imageURL" > "$imagePath"
if fileIsImage "$imagePath" ; then
cp "$imagePath" "$TODAY_WP_PATH"
return 0
fi
echo "$imagePath is not an image"
return 1;
}
setWallpaper() {
if ( $nowallpaper ); then
echom '--nowallpaper provided dont set the wallpaper'
return 0;
fi
echom 'Update wallpaper'
feh --bg-scale "$TODAY_WP_PATH"
return 0;
}
setZoomBackground() {
if ( $nozoom ); then
return 0;
fi
echom 'Update zoom'
cp "$TODAY_WP_PATH" "$ZOOM_BACKGROUND_PATH"
return 0;
}
deleteOldWallpapers() {
nbDeletions=$(find "$WALLPAPER_DIR" -mtime +1 | wc -l)
echom "Delete $nbDeletions files"
find "$WALLPAPER_DIR" -mtime +3 -delete
}
checkWallpaperDirectory() {
if [ ! -d "${WALLPAPER_DIR}" ]; then
echom "Wallpaper directory doesn't exist $WALLPAPER_DIR"
echom "Please create it yourself or check the value of the \$WALLPAPER_DIR variable in the script"
return 1
fi
return 0
}
echom "New execution"
checkWallpaperDirectory
downloadImage
setWallpaper
setZoomBackground
deleteOldWallpapers
echom 'Done'