#!/bin/sh # The MIT License (MIT) # # Copyright © 2024 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # bocler # Bank Of Canada Latest Exchange Rates # # Emit the latest exchange rates against the canadian dollar as 2 lines, the # first line being a header with the column names. # # The numbers in the second line are what you multiply each foreign currency # to get its canadian-dollar equivalent amount. # The full list of columns the webserver returns are shown below with their # 1-based column indices, in case you want to change which columns this # script picks. # # 1 date # 15 RUB # 2 AUD # 16 SAR # 3 BRL # 17 SGD # 4 CNY # 18 ZAR # 5 EUR # 19 KRW # 6 HKD # 20 SEK # 7 INR # 21 CHF # 8 IDR # 22 TWD # 9 JPY # 23 THB # 10 MYR # 24 TRY # 11 MXN # 25 GBP # 12 NZD # 26 USD # 13 NOK # 27 VND # 14 PEN # handle leading options case "$1" in -h|--h|-help|--help) awk '/^# +bocler/, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac # keep source-code lines narrow base='https://www.bankofcanada.ca/valet/observations/group/FX_RATES_DAILY' # load data from website: starting from 3 days back ensures this script # works even on weekends, while minimizing the data being transferred wget -q -O - "${base}/csv?start_date=$(date -d '3 days ago' +'%Y-%m-%d')" | # exclude prelude/intro lines, keeping only the header and the last line awk '/^"date"/; END { print }' | # ignore double quotes, and rename most column names sed 's-"--g; s-FX--g; s-CAD--g' | # pick a subset of the columns awk -F "," -v OFS="\t" '{ print $1, $2, $3, $4, $5, $7, $11, $25, $26 }' | # realign columns, turning tabs into spaces column -t