Skip to content

Instantly share code, notes, and snippets.

@radixm46
Created April 6, 2024 08:27
Show Gist options
  • Save radixm46/46993c618b96f103a0b1daf3d3f2ccaf to your computer and use it in GitHub Desktop.
Save radixm46/46993c618b96f103a0b1daf3d3f2ccaf to your computer and use it in GitHub Desktop.
強震モニタをweztermに表示するやつ(macOSだと喋る)
#!/usr/bin/env bash
set -euo pipefail
# requirements: bc, curl, wezterm, jq, imagemagick
base='http://www.kmoni.bosai.go.jp'
wdir="$(mktemp --directory -t kmoni)"
rdate="$(date -r $(($(date +%s) - 4)) +%Y%m%d%H%M%S)"
baseimg="base_map_w.gif"
jmasimg="${rdate}.jma_s.gif"
waveimg="${rdate}.eew.gif"
json_data="${rdate}.json"
cd "${wdir}"
composition_files=(
"${baseimg}" "${jmasimg}"
)
curl --silent --show-error --fail --parallel \
-O "${base}/data/map_img/CommonImg/${baseimg}" \
-O "${base}/data/map_img/RealTimeImg/jma_s/${rdate:0:8}/${jmasimg}" \
-O "${base}/data/map_img/PSWaveImg/eew/${rdate:0:8}/${waveimg}" \
-O "${base}/webservice/hypo/eew/${json_data}" 2> /dev/null && {
# check essential files
for f in "${jmasimg}" "${baseimg}" "${json_data}"; do
[ ! -e "./${f}" ] && {
printf 'file acquisition error!: %s\nexit\n' "${f}"
exit 1
}
done
[ "$(head -c 6 ${waveimg})" != '<html>' ] && composition_files+=("${waveimg}")
}
# overlay images and make bg transparent
convert "${composition_files[@]}" -layers flatten \
-fuzz '10%' -transparent '#fefdfd' \
-fuzz '30%' -fill white -opaque black \
-resize '200%' -filter Lanczos 'output.gif' 2> /dev/null \
&& wezterm imgcat "${wdir}/output.gif" --width '800px' --tmux-passthru detect
eqmag=$(jq -r '
if .magnitude == "" then "0" else "\(.magnitude)" end
' "${json_data}")
# On macOS, use TTS to announce earthquakes with magnitude over 4.0
[[ "${OSTYPE}" == darwin* ]] && {
if [ 1 = $(bc -e "4.0 < ${eqmag} && ${eqmag} < 5.5") ]; then
(say "地震検知" &)
elif [ 1 = $(bc -e "5.5 <= ${eqmag}") ]; then
(say "マグニチュード${eqmag}検知" &)
fi
}
eqmsg=$(jq -r '
if .result.message == "" then
"最大震度: \(.calcintensity) (第\(.report_num)報\(.is_final | if . == "true" then " - 最終" else "" end))
震源: \(.region_name) (\(.latitude), \(.longitude))
マグニチュード: \(.magunitude) 深さ: \(.depth)"
else
"地震情報なし"
end
' "./${json_data}") 2> /dev/null
echo "${eqmsg}"
rm -rf "${wdir}"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment