Move wget retry logic to separate function.
This commit is contained in:
parent
22fc0c0c5a
commit
d9ebe5265c
1 changed files with 33 additions and 18 deletions
51
fetch_fio.sh
51
fetch_fio.sh
|
@ -111,13 +111,44 @@ get_file_date() {
|
||||||
echo ${STAT%% *}
|
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
|
# Mark
|
||||||
log "$0" ======== started ========
|
log "$0" ======== started ========
|
||||||
|
|
||||||
# Each line should contain account number and Fio API token as first
|
# Each line should contain account number and Fio API token as first
|
||||||
# two non-whitespace strings. The rest of each line is ignored. There
|
# two non-whitespace strings. The rest of each line is ignored. There
|
||||||
# must be no leading whitespace.
|
# must be no leading whitespace.
|
||||||
while read accnt; do
|
while read accnt ; do
|
||||||
# Extract account number, Fio token and starting year
|
# Extract account number, Fio token and starting year
|
||||||
ACCNO=${accnt%% *}
|
ACCNO=${accnt%% *}
|
||||||
accrest=${accnt#* }
|
accrest=${accnt#* }
|
||||||
|
@ -134,23 +165,7 @@ while read accnt; do
|
||||||
|
|
||||||
# Fetch CSV from bank
|
# Fetch CSV from bank
|
||||||
CSVNAME="$BANK_DIR/$ACCNO.csv"
|
CSVNAME="$BANK_DIR/$ACCNO.csv"
|
||||||
for i in `seq 1 3` ; do
|
download_file "${APIURI}/periods/${APIKEY}/2015-01-01/2025-01-31/transactions.csv" "$CSVNAME"
|
||||||
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
|
|
||||||
fi
|
fi
|
||||||
done < "$APIKEYS_FILE"
|
done < "$APIKEYS_FILE"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue