-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuntimes
executable file
·63 lines (53 loc) · 1.55 KB
/
suntimes
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
#!/bin/bash
# Thanks Lubos Rendek - [email protected]!
url="https://weather.com/weather/today/l"
download() {
# find old suntime file if it exists
previous=$(find "$HOME"/.cache -maxdepth 1 -name "*_$1-sun")
# dl a new suntime
timeout 3 curl -s "$url/$1" > "$2"
while [ ! -s "$2" ] && [ ! -s "$previous" ]; do
curl -s "$url/$1" > "$2"
done
comp=$(cmp "$2" "$previous" 2> /dev/null)
# check if it's the same as the new file
if [ ! "$comp" ] || [ ! -f $previous ]; then
rm "$previous" 2> /dev/null
#echo "new"
sun_file=$2
else
rm "$2"
#echo "old"
sun_file=$previous
fi
echo "$sun_file"
}
get_times() {
sun_times=$( cat $1 | sed 's/,/\n/g' | grep -E "sunriseTimeLocal|sunsetTimeLocal" | head -n2 | cut -d'T' -f3 | cut -d'-' -f1 | tr '\n' '\t' )
sunrise=$( echo $sun_times | awk '{print $1}' | cut -c1-5 )
sunset=$( echo $sun_times | awk '{print $2}' | cut -c1-5 )
echo "🌄 $sunrise 🌇 $sunset"
}
main() {
#obtain a location code from: https://weather.codes/search/
LOCATION="USCA0830" # bay area
#LOCATION="USMA0046" # for boston
for i in "$@"; do
case $i in
-l|--location)
LOCATION=$2
shift
shift
;;
*)
;;
esac
done
new_sun_file="$HOME/.cache/$(date '+%Y-%m-%d-%H:%M:%S')_$LOCATION-sun"
#echo $LOCATION
#download "$LOCATION" "$new_sun_file"
sun_file=$(download "$LOCATION" "$new_sun_file" | tail -n1)
#echo $sun_file
get_times "$sun_file"
}
main "$@"