#!/bin/bash # Execute `main.py` to get the data, output it to a temp file, then merge the # results into an existing `usage.tsv`, removing duplicate rows. # # If `usage.tsv` doesn't exist, it will be created. set -e NEW=new.tsv USAGE=usage.tsv TEMP=temp.tsv printf "\n=== $(date) ===\n" cd "$(dirname $0)" # Make sure local paths work. echo "Activating venv" source ./env/bin/activate echo "Fetching new records into ${NEW}" python3 main.py > ${NEW} echo "Retrieved $(wc -l ${NEW}) records" if [[ ! -f ${USAGE} ]]; then echo "'${USAGE}' does not exist. Renaming '$NEW' to '$USAGE'." mv ${NEW} ${USAGE} else echo "'${USAGE}' exists, with $(cat ${USAGE} | wc -l) lines." echo "Sorting & adding new records from '${USAGE}' and '${NEW}' to '${TEMP}'." header="$(head -n1 ${USAGE})" echo "${header}" > ${TEMP} cat <(tail -n +2 ${USAGE}) <(echo) <(tail -n +2 ${NEW}) \ | sort | uniq \ >> ${TEMP} echo "$(wc -l ${TEMP})" echo "Replacing '${USAGE}' with contents of '${TEMP}'" rm ${USAGE} mv ${TEMP} ${USAGE} fi rm ${NEW} 2> /dev/null