Move wget retry logic to separate function.

This commit is contained in:
Dominik Pantůček 2023-05-25 22:53:25 +02:00
parent 22fc0c0c5a
commit d9ebe5265c

View file

@ -111,13 +111,44 @@ get_file_date() {
echo ${STAT%% *}
}
#
# $1 - URI
# $2 - output file
download_file() {
log "Downloading \"$1\" to \"$2\""
url="$1"
fname="$2"
tmpfname="$fname.tmp"
oldfname="$fname.old"
for i in `seq 1 3` ; do
if wget -q "$url" -O "$tmpfname" ; then
if [ -s "$tmpfname" ] ; then
log Download OK
cp "$fname" "$oldfname"
mv "$tmpfname" "$fname"
log Finished
break
else
log Download successfull but empty or non-existing result.
log Retrying in 5 s.
fi
else
log Failed download, retrying in 5 s.
sleep 5
fi
done
}
# Current year (last in seq)
CYEAR=`date +%Y`
# Mark
log "$0" ======== started ========
# Each line should contain account number and Fio API token as first
# two non-whitespace strings. The rest of each line is ignored. There
# must be no leading whitespace.
while read accnt; do
while read accnt ; do
# Extract account number, Fio token and starting year
ACCNO=${accnt%% *}
accrest=${accnt#* }
@ -134,23 +165,7 @@ while read accnt; do
# Fetch CSV from bank
CSVNAME="$BANK_DIR/$ACCNO.csv"
for i in `seq 1 3` ; do
if wget -q "${APIURI}/periods/${APIKEY}/2015-01-01/2025-01-31/transactions.csv" -O "$CSVNAME.tmp" ; then
if [ -s "$CSVNAME.tmp" ] ; then
log Download OK
cp "$CSVNAME" "$CSVNAME.old"
mv "$CSVNAME.tmp" "$CSVNAME"
log Finished
break
else
log Download successfull but empty or non-existing result.
log Retrying in 5 s.
fi
else
log Failed download, retrying in 5 s.
sleep 5
fi
done
download_file "${APIURI}/periods/${APIKEY}/2015-01-01/2025-01-31/transactions.csv" "$CSVNAME"
fi
done < "$APIKEYS_FILE"