Skip to content

Commit

Permalink
Cleanup earthquake segment (#411)
Browse files Browse the repository at this point in the history
* comply with shellcheck
  • Loading branch information
xx4h authored Apr 13, 2024
1 parent 28c47b7 commit 78dae2c
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions segments/earthquake.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# shellcheck shell=bash
# Prints the most recent earthquake (currently only supports japan)
# It prints the location, time, and magnitude if the quake happened within
# a timelimit and magnitude threshold
Expand All @@ -13,8 +14,9 @@ TMUX_POWERLINE_SEG_EARTHQUAKE_ALERT_TIME_WINDOW_DEFAULT="60"
TMUX_POWERLINE_SEG_EARTHQUAKE_TIME_FORMAT_DEFAULT='(%H:%M)'
TMUX_POWERLINE_SEG_EARTHQUAKE_MIN_MAGNITUDE_DEFAULT='3'


generate_segmentrc() {
read -d '' rccontents << EORC
read -r -d '' rccontents << EORC
# The data provider to use. Currently only "goo" is supported.
export TMUX_POWERLINE_SEG_EARTHQUAKE_DATA_PROVIDER="${TMUX_POWERLINE_SEG_EARTHQUAKE_DATA_PROVIDER_DEFAULT}"
# How often to update the earthquake data in seconds.
Expand All @@ -40,7 +42,7 @@ run_segment() {
case "$TMUX_POWERLINE_SEG_EARTHQUAKE_DATA_PROVIDER" in
"goo") earthquake=$(__goo_earthquake) ;;
*)
echo "Unknown earthquake-information provider [${$TMUX_POWERLINE_SEG_EARTHQUAKE_DATA_PROVIDER}]";
echo "Unknown earthquake-information provider [$TMUX_POWERLINE_SEG_EARTHQUAKE_DATA_PROVIDER]";
return 1
esac
if [ -n "$earthquake" ]; then
Expand Down Expand Up @@ -73,9 +75,9 @@ __goo_earthquake() {
timestamp=""
if [[ -f "$tmp_file" ]]; then
if shell_is_osx || shell_is_bsd; then
last_update=$(stat -f "%m" ${tmp_file})
last_update=$(stat -f "%m" "${tmp_file}")
elif shell_is_linux; then
last_update=$(stat -c "%Y" ${tmp_file})
last_update=$(stat -c "%Y" "${tmp_file}")
fi
time_now=$(date +%s)

Expand All @@ -87,35 +89,34 @@ __goo_earthquake() {

if [ -z "$magnitude" ]; then
# get the rss file, convert encoding to UTF-8, then delete windows carriage-returns
earthquake_data=$(curl --max-time 4 -s "http://weather.goo.ne.jp/earthquake/index.rdf" | iconv -f EUC-JP -t UTF-8 | tr -d "\r")
if [ "$?" -eq "0" ]; then
# This rss feed is not very clean or easy to use, but we will use it because
# this is all that can be found for now
if earthquake_data=$(curl --max-time 4 -s "http://weather.goo.ne.jp/earthquake/index.rdf" | iconv -f EUC-JP -t UTF-8 | tr -d "\r"); then
# This rss feed is not very clean or easy to use, but we will use it because
# this is all that can be found for now

# we grab the data from the title of the first item (most recent earthquake)
earthquake_data=${earthquake_data#*item\><title>}
# end our data at the end of the approx. time
earthquake_data=${earthquake_data%%*}

# pluck our data
location=$(echo $earthquake_data | awk '{print $2}')
magnitude=$(echo $earthquake_data | awk '{print $4}')
location=$(echo "$earthquake_data" | awk '{print $2}')
magnitude=$(echo "$earthquake_data" | awk '{print $4}')
timestamp=${earthquake_data#*\(}

__convert_jp_magnitude
__convert_jp_timestamp

echo $location > $tmp_file
echo $magnitude >> $tmp_file
echo $timestamp >> $tmp_file
echo "$location" > "$tmp_file"
echo "$magnitude" >> "$tmp_file"
echo "$timestamp" >> "$tmp_file"
elif [ -f "$tmp_file" ]; then
__read_tmp_file
fi
fi
__convert_timestamp_to_fmt

# extract the numerical portion of magnitude
magnitude_number=$(echo $magnitude | sed -e 's/+//' -e 's/-//')
magnitude_number=$(echo "$magnitude" | sed -e 's/+//' -e 's/-//')

if [ -n "$magnitude" ]; then
if __check_alert_time_window && __check_min_magnitude ; then
Expand All @@ -135,7 +136,7 @@ __convert_jp_magnitude() {
}

__check_alert_time_window() {
[[ $(( ( $(date +%s) - $timestamp ) / 60 )) -lt $TMUX_POWERLINE_SEG_EARTHQUAKE_ALERT_TIME_WINDOW ]]
[[ $(( ( $(date +%s) - timestamp ) / 60 )) -lt $TMUX_POWERLINE_SEG_EARTHQUAKE_ALERT_TIME_WINDOW ]]
}

__check_min_magnitude() {
Expand All @@ -146,7 +147,7 @@ __convert_jp_timestamp() {
if shell_is_osx ; then
timestamp=$(date -j -f "%Y年%m月%d日 %H時%M分" "$timestamp" +"%s")
else
timestamp=$(echo $timestamp | $sed -e 's/年/-/' -e 's/月/-/' -e 's/日//' -e 's/時/:/' -e 's/分//')
timestamp=$(echo "$timestamp" | sed -e 's/年/-/' -e 's/月/-/' -e 's/日//' -e 's/時/:/' -e 's/分//')
timestamp=$(date -d "$timestamp" +"%s")
fi
}
Expand All @@ -165,7 +166,7 @@ __read_tmp_file() {
fi
IFS_bak="$IFS"
IFS=$'\n'
lines=($(cat ${tmp_file}))
read -r -a lines <<< "${tmp_file}"
IFS="$IFS_bak"
location="${lines[0]}"
magnitude="${lines[1]}"
Expand Down

0 comments on commit 78dae2c

Please sign in to comment.