Просмотр исходного кода

Add czkrates, a shellfu script to get CZK rates

Alois Mahdal 7 лет назад
Родитель
Сommit
350dec8086
2 измененных файлов: 143 добавлений и 0 удалений
  1. 11
    0
      README.md
  2. 132
    0
      bin/czkrates

+ 11
- 0
README.md Просмотреть файл

@@ -34,6 +34,17 @@ Example:
34 34
     00000008  01110010 01101100 01100100 00001010  |rld.|
35 35
     $
36 36
 
37
+### czkrates ###
38
+
39
+    czkrates [-d] [-v] [-D DATE] [AMOUNT] [CURRENCY]
40
+    czkrates [-d] [-v] [-D DATE] [CURRENCY] [AMOUNT]
41
+
42
+Show CZK rate for currency CURRENCY. If AMOUNT is given, multiply by
43
+that number.  Use `-D` to get rate for past day, the DATE format is
44
+the same as for *date* utility from coreutils.  `-d` and `-v` turn on
45
+debugging and verbosity, respectively.
46
+
47
+
37 48
 ### dissect\_url ###
38 49
 
39 50
 Split URLs into components and arguments.  Output is useful to see more easily

+ 132
- 0
bin/czkrates Просмотреть файл

@@ -0,0 +1,132 @@
1
+#!/bin/bash
2
+#shellcheck disable=SC1090
3
+
4
+. "$(shellfu-get path)" || exit 3
5
+
6
+shellfu import pretty
7
+
8
+
9
+#
10
+# Currency to use when not specified
11
+#
12
+CZKRATES_DEFAULT_CURRENCY=${CZKRATES_DEFAULT_CURRENCY:-USD}
13
+
14
+
15
+#
16
+# URL to daily table from
17
+#
18
+# The real URL is formed by appending current date.
19
+#
20
+CZKRATES_URL=${CZKRATES_URL:-http://www.cnb.cz/en/financial_markets/foreign_exchange_market/exchange_rate_fixing/daily.txt}
21
+
22
+usage() {
23
+    mkusage "[options] [CURRENCY] [MULTIPLIER]" \
24
+            "[options] [MULTIPLIER] [CURRENCY]" \
25
+            "" \
26
+        -o \
27
+            "-D DATE, --date DATE   Use currency rate from DATE instead"    \
28
+            "       of today.  DATEs like 'last sun' work, see section DATE"\
29
+            "       STRING of date(1) for more info."                       \
30
+            "-d, --debug    Turn on debbugging output"                      \
31
+            "-v, --verbose  Be more verbose"
32
+}
33
+
34
+type_of() {
35
+    #
36
+    # Print 'int' if $1 is an integer, 'nil' if empty; 'str' otherwise
37
+    #
38
+    local es_p es_n
39
+    test -z "$1" && echo nil && return
40
+    test "$1" -ge 0 2>/dev/null; es_p=$?    # assuming positive
41
+    test "$1" -lt 0 2>/dev/null; es_n=$?    # assuming negative
42
+    test $es_p -eq 2 && echo str && return
43
+    test $es_n -eq 2 && echo str && return
44
+    echo int
45
+}
46
+
47
+parse_rate1() {
48
+    #
49
+    # Parse field $2 from currency $1 data
50
+    #
51
+    local wntcur=$1
52
+    local wntfld=${2:-Rate}
53
+    case "$wntfld" in
54
+        Country|Currency|Amount|Code|Rate) true;;
55
+        *) mkusage "code Country|Currency|Amount|Code|Rate" ;;
56
+    esac
57
+    local date head
58
+    read -r date
59
+    debug "got: date=$date"
60
+    read -r head
61
+    debug "got: head=$head"
62
+    while IFS='|' read -r Country Currency Amount Code Rate;
63
+    do
64
+        debug "got: Country=$Country Currency=$Currency Amount=$Amount Code=$Code Rate=$Rate"
65
+        test "$wntcur" = "$Code" || continue
66
+        case $Amount in
67
+            1) echo "${!wntfld}" ;;
68
+            *) python -c "print(float($Rate)/$Amount)" ;;
69
+        esac
70
+    done
71
+}
72
+
73
+get_unit_price() {
74
+    #
75
+    # Get price of 1 unit of $currrency
76
+    #
77
+    local table     # table with rate data
78
+    think "getting currrency for: $Curr"
79
+    table=$(curl -f --insecure --silent "$Uri") \
80
+     || die "failed to download rates: $Uri"
81
+    parse_rate1 "$Curr" Rate <<<"$table" | grep .
82
+}
83
+
84
+maybe_multiply() {
85
+    #
86
+    # Multiply if needed
87
+    #
88
+    local price
89
+    read -r price
90
+    if test "$Mult" = 1;
91
+    then
92
+        echo "$price"
93
+    else
94
+        think "multiplying by: $Mult"
95
+        python -c "print(float($price) * $Mult)"
96
+    fi
97
+}
98
+
99
+
100
+main() {
101
+    local Uri
102
+    local Curr
103
+    local Mult
104
+    local Date=now
105
+    Uri=$CZKRATES_URL
106
+    Uri+="?date=$(date +%d.%m.%Y)"
107
+    #shellcheck disable=SC2034
108
+    while true; do case "$1" in
109
+        -D|--date)    Date=$2;  shift 2 || usage ;;
110
+        -d|--debug)   PRETTY_DEBUG=true;   shift ;;
111
+        -v|--verbose) PRETTY_VERBOSE=true; shift ;;
112
+        -*)           usage ;;
113
+        *)            break ;;
114
+    esac done
115
+    Uri+="?date=$(date -d "$Date" +%d.%m.%Y)"
116
+    case "$(type_of "$1"):$(type_of "$2")" in
117
+        int:int|str:str)    usage ;;
118
+        int:nil|nil:int)    Mult="$1$2"          ;;
119
+        str:nil|nil:str)    Curr="$1$2"          ;;
120
+        str:int)            Curr="$1"; Mult="$2" ;;
121
+        int:str)            Curr="$2"; Mult="$1" ;;
122
+    esac
123
+    Curr="${Curr:-$CZKRATES_DEFAULT_CURRENCY}"
124
+    Curr="${Curr^^}"
125
+    Mult="${Mult:-1}"
126
+    debug -v Uri Curr Mult
127
+    get_unit_price | maybe_multiply
128
+}
129
+
130
+debug -v CZKRATES_DEFAULT_CURRENCY
131
+
132
+main "$@"