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