Improve update checks.

This commit is contained in:
Dominik Pantůček 2023-05-26 12:01:29 +02:00
parent 5287c17d19
commit abd9f4aa5b

View file

@ -119,7 +119,6 @@ get_file_date() {
# $1 - URI # $1 - URI
# $2 - output file # $2 - output file
download_file() { download_file() {
log "Downloading \"$1\" to \"$2\""
url="$1" url="$1"
fname="$2" fname="$2"
tmpfname="$fname.tmp" tmpfname="$fname.tmp"
@ -132,7 +131,7 @@ download_file() {
cp "$fname" "$oldfname" cp "$fname" "$oldfname"
fi fi
mv "$tmpfname" "$fname" mv "$tmpfname" "$fname"
log Finished log Rename OK
break break
else else
log Download successfull but empty or non-existing result. log Download successfull but empty or non-existing result.
@ -151,7 +150,11 @@ download_file() {
# $2 - year # $2 - year
# $3 - destination file name # $3 - destination file name
download_year() { download_year() {
download_file "${APIURI}/periods/$1/$2-01-01/$2-12-31/transactions.csv" "$3" log "Downloading ${APIURI}/periods/.../$year-01-01/$year-12-31/transactions.csv to $fname"
apikey="$1"
year="$2"
fname="$3"
download_file "${APIURI}/periods/$apikey/$year-01-01/$year-12-31/transactions.csv" "$fname"
} }
# Current year (last in seq) # Current year (last in seq)
@ -190,23 +193,23 @@ while read accnt ; do
for year in `seq $YEAR $CYEAR` ; do for year in `seq $YEAR $CYEAR` ; do
# Check whether it needs fetching # Check whether it needs fetching
CSVNAME="$BANK_DIR_PARTS/$ACCNO-$year.csv" CSVNAME="$BANK_DIR_PARTS/$ACCNO-$year.csv"
UPDATED=`get_file_date "$CSVNAME"` FILE_DATE=`get_file_date "$CSVNAME"`
NYEAR=`expr $year + 1` NEXT_YEAR=`expr $year + 1`
MINUPD="$NYEAR-01-02" MIN_DATE="$NEXT_YEAR-01-02"
UTS=`date -d $UPDATED +%s` FILE_TS=`date -d $FILE_DATE +%s`
MTS=`date -d $MINUPD +%s` MIN_TS=`date -d $MIN_DATE +%s`
if [ "$UTS" -lt "$MTS" ] ; then if [ "$FILE_TS" -lt "$MIN_TS" ] ; then
log "Update $ACCNO in $year - updated $UPDATED, needs $MINUPD" log "Update $ACCNO in $year - updated $FILE_DATE, needs $MIN_DATE"
download_year "$APIKEY" "$year" "$CSVNAME" download_year "$APIKEY" "$year" "$CSVNAME"
if [ "$year" -lt "$CYEAR" ] ; then if [ "$year" -lt "$CYEAR" ] ; then
log "Sleeping for 30s before next API usage" log "Sleeping for 30s before next API usage"
sleep 30 sleep 30
fi fi
else else
log "Skipping $ACCNO in $year - already latest" log "Skipping $ACCNO in $year - already latest: $FILE_DATE"
fi fi
done done
done < "$APIKEYS_FILE" done < "$APIKEYS_FILE"
# Mark # Mark
log "$0" finished log "$0" ======== finished ========