forked from Drakulix/sway-gnome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnasa-apod.sh
executable file
·57 lines (48 loc) · 1.59 KB
/
nasa-apod.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
#!/bin/sh
source /etc/profile
# Sway Nasa Astronomy Picture of the Day background
set -ex
random=$(echo "random" | \
fold -w1 | shuf | \
tr -d '\n' \
)
img_regex='<IMG SRC="(.*)"'
script_path=$(dirname "$(readlink -f "$0")")
mkdir -p "$HOME/.cache/sway-nasa-apod/"
local_image_path="$HOME/.cache/sway-nasa-apod/nasa-apod-$(date -I).jpg"
latest_image_path="$HOME/.cache/sway-nasa-apod/latest.jpg"
get_img() {
remote_image_path=$(curl "$1" 2>/dev/null | \
pcregrep --only-matching=1 "$img_regex" \
)
image_url="https://apod.nasa.gov/apod/$remote_image_path"
curl --silent --output "$local_image_path" "$image_url"
cp "$local_image_path" "$latest_image_path"
}
if [[ "$1" = "-h" ]] || [[ "$1" = "--help" ]]; then
echo "Usage: ${0##*/} [-h] [-r]"
echo ""
echo "set Sway background to Nasa Astronomy Picture of the Day"
echo ""
echo "optional arguments:"
echo " -h, --help show this help message and exit"
echo " -r, --$random pick random day from the archive, url saved to $script_path/last_random_url"
exit 0
elif [[ "$1" = "-r" ]] || [[ "$1" = "$random" ]] ; then
archive_list_url="https://apod.nasa.gov/apod/archivepixFull.html"
archive_regex='<a href="(ap.*.html)'
archive_urls=$(curl --silent "$archive_list_url" | \
pcregrep --only-matching=1 "$archive_regex" \
)
url=$(echo "$archive_urls" | \
sort --random-sort | \
head --lines=1 \
)
url=https://apod.nasa.gov/apod/"$url"
get_img "$url"
echo "$url" > "$script_path/last_random_url"
else
url="https://apod.nasa.gov/apod/astropix.html"
get_img "$url"
fi
swww img $latest_image_path --transition-type outer