Add a bash script for recording mscale #2

This commit is contained in:
gg 2023-08-19 05:23:14 -07:00
parent 857439d62e
commit c86316777c
1 changed files with 48 additions and 0 deletions

48
bin/mscale-record.sh Executable file
View File

@ -0,0 +1,48 @@
#!/bin/bash
# Print the current mscale value.
function mscale() {
curl --silent https://weathernews.jp/mscale/json/scale.json | jq -r .mscale
}
# Print the current time in Japan.
function jst() {
TZ=Asia/Tokyo date --iso-8601=minutes
}
# Print the current time in Japan with hourly resolution.
function jst_1h() {
jst | perl -pe 's/T(\d{2}):\d{2}/T$1:00/'
}
# Print an mscale row by jst if it exists.
function exists_sql {
echo "SELECT val, jst FROM mscale WHERE jst = \"$1\""
}
# Print an INSERT statement for adding data to mscale.
function insert_sql {
echo "INSERT INTO mscale (val, jst) VALUES ($1, \"$2\");"
}
# Print an UPDATE statement
function update_sql {
echo "UPDATE mscale SET val = $1 WHERE jst = \"$2\";"
}
function main() {
db=${1:-mscale.db}
if [[ $(exists_sql `jst_1h` | sqlite3 "$db" | wc -l) > 0 ]] ; then
# update
update_sql `mscale` `jst_1h` | sqlite3 "$db"
else
# insert
insert_sql `mscale` `jst_1h` | sqlite3 "$db"
fi
}
# What is the bash equivalent of Python's `if __name__ == '__main__'`?
# https://stackoverflow.com/a/29967433
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi