add script to watch changes of summary web page

This commit is contained in:
Aki Kareha 2025-02-25 06:52:45 +09:00
parent 04229e0527
commit d2435335d4
No known key found for this signature in database
GPG Key ID: 53C60D74F1899BF0
4 changed files with 93 additions and 5 deletions

4
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.swp
*.csv
cache
log
*.csv
*.html

2
clean
View File

@ -1,3 +1,5 @@
#!/bin/sh
rm -rf cache
rm -rf log
rm -f *.csv
rm -f *.html

12
update
View File

@ -5,7 +5,7 @@ CSV_URL="https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv"
MAIL_TO="$USER"
MAIL_FROM="info@holiday.kareha.org"
MAIL_SUBJECT="Holiday CSV Updated"
MAIL_BODY="Holiday CSV file has been Updated."
MAIL_BODY="Holiday CSV file has been updated."
# Constants
EXIT_SUCCESS=0
@ -25,10 +25,10 @@ verify_command "awk"
verify_command "mailx"
# Logging
CURRENT_MONTH=$(date "+%Yw%U%z")
CURRENT_MONTH=$(date "+%Y-%m%z")
SCRIPT_NAME=$(basename $0)
LOG_DIR="log"
LOG_FILE="$LOG_DIR/$SCRIPT_NAME_$CURRENT_MONTH"
LOG_FILE="$LOG_DIR/$SCRIPT_NAME-$CURRENT_MONTH"
get_current_time() {
date "+%Y-%m-%d %H:%M:%S%z"
@ -48,7 +48,7 @@ send_mail() {
}
# Acquire the lock or exit
LOCK_DIR="lock"
LOCK_DIR="$SCRIPT_NAME-lock"
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
echo "Another process is holding the lock. Exiting."
exit $EXIT_FAILURE
@ -65,6 +65,10 @@ CSV_FILE="holidays-jp.csv"
# Run
curl -sS -L -o $TMP_CSV_FILE $CSV_URL
if ! diff -q $RAW_CSV_FILE $TMP_CSV_FILE >/dev/null 2>&1; then
CACHE_DIR="cache"
mkdir -p "$CACHE_DIR"
CURRENT_DATETIME=$(date "+%Y-%m-%d_%H:%M:%S%z")
cp $TMP_CSV_FILE "$CACHE_DIR/holidays-jp-$CURRENT_DATETIME.csv"
mv $TMP_CSV_FILE $RAW_CSV_FILE
nkf -w $RAW_CSV_FILE >$ALL_CSV_FILE
current_year=$(date +'%Y')

80
watch Executable file
View File

@ -0,0 +1,80 @@
#!/bin/sh
# Settings
SITE_URL="https://www8.cao.go.jp/chosei/shukujitsu/gaiyou.html"
MAIL_TO="$USER"
MAIL_FROM="info@holiday.kareha.org"
MAIL_SUBJECT="Holiday Site Updated"
MAIL_BODY="Holiday web site has been updated."
# Constants
EXIT_SUCCESS=0
EXIT_FAILURE=1
# Verify the required runtime environment
verify_command() {
if ! command -v $1 >/dev/null; then
echo "This script requires \"$1\" to be installed. Please install it and try again."
exit $EXIT_FAILURE
fi
}
verify_command "curl"
verify_command "mailx"
# Logging
CURRENT_MONTH=$(date "+%Y-%m%z")
SCRIPT_NAME=$(basename $0)
LOG_DIR="log"
LOG_FILE="$LOG_DIR/$SCRIPT_NAME-$CURRENT_MONTH"
get_current_time() {
date "+%Y-%m-%d %H:%M:%S%z"
}
log() {
local timestamp=$(get_current_time)
local message=$1
echo "$timestamp $message" >>$LOG_FILE
}
send_mail() {
echo "$MAIL_BODY" | mailx -r "$MAIL_FROM" -s "$MAIL_SUBJECT" "$MAIL_TO"
if [ $? -ne 0 ]; then
log "Failed to send email"
fi
}
# Acquire the lock or exit
LOCK_DIR="$SCRIPT_NAME-lock"
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
echo "Another process is holding the lock. Exiting."
exit $EXIT_FAILURE
fi
mkdir -p $LOG_DIR
# Internal configuration settings
TMP_HTML_FILE="summary-jp-tmp.html"
HTML_FILE="summary-jp.html"
# Run
curl -sS -L -o $TMP_HTML_FILE $SITE_URL
if ! diff -q $HTML_FILE $TMP_HTML_FILE >/dev/null 2>&1; then
CACHE_DIR="cache"
mkdir -p "$CACHE_DIR"
CURRENT_DATETIME=`date "+%Y-%m-%d_%H:%M:%S%z"`
cp $TMP_HTML_FILE "$CACHE_DIR/summary-jp-$CURRENT_DATETIME.html"
mv $TMP_HTML_FILE $HTML_FILE
log "Changed"
send_mail
else
log "No Change"
rm $TMP_HTML_FILE
fi
# Release the lock
rmdir "$LOCK_DIR"
# Exit
exit $EXIT_SUCCESS