Implement downloading particular month.

This commit is contained in:
Dominik Pantůček 2023-07-29 15:43:53 +02:00
parent 41b218613a
commit 9ca16fe6c5

View file

@ -1,8 +1,8 @@
#!/bin/sh
#
# fetch_fio.sh
# fetch_fio_fine.sh
#
# Fio API account statements fetcher.
# Fio API fine-grained account statements fetcher.
#
# ISC License
#
@ -148,16 +148,85 @@ download_file() {
}
#
# Downloads single year
# Leap year?
leap_year() {
YEAR="$1"
ym4=`expr "$YEAR" % 4`
ym100=`expr "$YEAR" % 100`
ym400=`expr "$YEAR" % 400`
if [ $ym4 = 0 ] ; then
# Maybe
if ! [ $ym100 = 0 ] ; then
# Leap
return 0
else
# Maybe
if [ $ym400 = 0 ] ; then
# Leap
return 0
else
# Not leap
return 1
fi
fi
else
# Not leap
return 1
fi
}
#
# Returns the last day of given month in Fio format
# $1 - year
# $2 - month
# $3 - if non-empty, return ISO
end_of_month() {
YEAR="$1"
MONTH="$2"
case $MONTH in
01) DAY=31 ;;
02)
DAY=$(
if leap_year $YEAR ; then
echo 29
else
echo 28
fi
)
;;
03) DAY=31 ;;
04) DAY=30 ;;
05) DAY=31 ;;
06) DAY=30 ;;
07) DAY=31 ;;
08) DAY=31 ;;
09) DAY=30 ;;
10) DAY=31 ;;
11) DAY=30 ;;
12) DAY=31 ;;
esac
if [ -z "$3" ] ; then
echo "$DAY.$MONTH.$YEAR"
else
echo "$YEAR-$MONTH-$DAY"
fi
}
#
# Downloads single year month
# $1 - apikey
# $2 - year
# $3 - destination file name
download_year() {
# $3 - month - must be in MM format already
# $4 - destination file name
download_year_month() {
apikey="$1"
year="$2"
fname="$3"
log "Downloading ${APIURI}/periods/.../$year-01-01/$year-12-31/transactions.csv to $fname"
download_file "${APIURI}/periods/$apikey/$year-01-01/$year-12-31/transactions.csv" "$fname"
month="$3"
fname="$4"
STARTDATE="$year-$month-01"
ENDDATE=`end_of_month $year $month 1`
log "Downloading ${APIURI}/periods/.../$STARTDATE/$ENDDATE/transactions.csv to $fname"
download_file "${APIURI}/periods/$apikey/$STARTDATE/$ENDDATE/transactions.csv" "$fname"
}
#