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         # show help message, extracting the info-comment at the start
  59         # of this file, and quit
  60         awk '/^# +bocler/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  61         exit 0
  62     ;;
  63 esac
  64 
  65 # keep source-code lines narrow
  66 b='https://www.bankofcanada.ca/valet/observations/group/FX_RATES_DAILY/'
  67 # load data from website: starting from 3 days back ensures this script
  68 # works even on weekends, while minimizing the data being transferred
  69 curl -s "${b}csv?start_date=$(date -d '3 days ago' +'%Y-%m-%d')" |
  70 # exclude prelude/intro lines, keeping only the header and the last line
  71 awk '/^"date"/; END { print }' |
  72 # turn CSV into TSV, also changing/simplifying most column names
  73 tr -d '"' | tr ',' '\t' | sed 's-FX--g; s-CAD--g' |
  74 # pick a subset of the columns
  75 awk -F "\t" -v OFS="\t" '{ print $1, $2, $3, $4, $5, $7, $11, $25, $26 }' |
  76 # realign columns, turning tabs into spaces
  77 column -t