historical_trainmap/run.sh
2024-01-16 02:12:03 +01:00

36 lines
945 B
Bash
Executable file

#!/bin/sh
# Writes files to the current working directory
PYTHON="$(dirname "$0")"/venv/bin/python
if ! test -x "$PYTHON"; then
echo "Create a virtual environment in $(dirname "$0")/venv first"
exit 1
fi
previous_file=""
while true; do
# Get one shot of data
data="$("$PYTHON" "$(dirname "$0")"/trainmap_backup.py)"
timestamp="$(printf '%s\n' "$data" | jq ".timestamp / 1000 | round")"
file="$(date +%Y-%m-%d --date "@$timestamp").json"
# Write the data to file
printf '%s\n' "$data" >> "$file"
# At the start of a new day, archive the previous day's file
if test "$previous_file" == ""; then
previous_file="$file"
elif test "$file" != "$previous_file" && test "$file" != ""; then
gzip --best "$previous_file"
previous_file="$file"
fi
# Sleep to start the next request at the next full minute
seconds="$(date +%S)"
sleep_for="$("$PYTHON" -c "print(60 - int('$seconds', 10) - 0.5)")"
sleep "$sleep_for" || true
done