X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=home%2Fbin%2Ftoday;fp=home%2Fbin%2Ftoday;h=f9c2d289193680f400ce7b78061d5ded6c888a70;hb=a68d5744f8758fc77c34c612edac06dbc0bea129;hp=0000000000000000000000000000000000000000;hpb=e184ff59e5cc01f9f62d0be272b77e59e149a6f1;p=khome.git diff --git a/home/bin/today b/home/bin/today new file mode 100755 index 0000000..f9c2d28 --- /dev/null +++ b/home/bin/today @@ -0,0 +1,86 @@ +#! /bin/bash + +set -e + +DIR_DATA="$HOME/var/run/today" +FILE_WEATHER="$DIR_DATA/weather" + +mkdir -p "$DIR_DATA" + +repeat() { + x="$1" + n="$2" + awk -v x="$x" -v n="$n" 'BEGIN {for (i=1; i<=n; i++) {printf x}}' +} + +bar=$(repeat '-' '80') +ts_fmt='+%A, %Y %B %d, %H:%M:%S' +ts_lang='el_GR.UTF-8' +timestamp_local=$(LANG="$ts_lang" date "$ts_fmt") +timestamp_west=$( LANG="$ts_lang" TZ='US/Pacific' date "$ts_fmt") +timestamp_pl=$( LANG="$ts_lang" TZ='Poland' date "$ts_fmt") +timestamp_nz=$( LANG="$ts_lang" TZ='NZ' date "$ts_fmt") + +ip_addresses=$( + ip addr \ + | awk ' + /^[0-9]+:/ { + interface = $2 + sub(":$", "", interface) + interfaces[++interface_count] = interface + } + + /^ +inet +/ { + addr = $2 + sub("/[0-9]+$", "", addr) + addrs[interface] = addr + } + + END { + for (i=1; i<=interface_count; i++) { + interface = interfaces[i] + print interface, addrs[interface] + } + }' \ + | column -t +) + +weather_fetch() { + curl "http://wttr.in/${ZIP_CODE}?1" \ + 1> "$FILE_WEATHER" \ + 2> /dev/null +} + +weather_file_age() { + echo $(( $(date +%s) - $(stat -c '%Y' "$FILE_WEATHER") )) +} + +weather_get_or_fetch() { + weather_file_age_limit=$(( 3 * 60 * 60 )) + if [[ ! -e $FILE_WEATHER || weather_file_age -ge $weather_file_age_limit ]] + then + weather_fetch + fi + cat "$FILE_WEATHER" +} + +weather="$(weather_get_or_fetch)" + +clear +#LANG="$ts_lang" ncal -M $(date +%Y) +LANG="$ts_lang" ncal -M +echo "$bar" +( + echo 'LOCAL' ',' "$timestamp_local" + echo 'Pacific' ',' "$timestamp_west" + echo 'Poland' ',' "$timestamp_pl" + echo 'New Zealand' ',' "$timestamp_nz" +) \ +| column -ts, +echo "$bar" +echo "$ip_addresses" +echo "$bar" +echo '' +echo "Fetched on: $(stat -c %y $FILE_WEATHER)" +echo "$weather" +echo ''