diff --git a/fetch_fio.sh b/fetch_fio.sh index f4f2477..4d7f2b6 100644 --- a/fetch_fio.sh +++ b/fetch_fio.sh @@ -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"