summaryrefslogtreecommitdiff
path: root/bitcoin/btc_ticker.sh
blob: 189bd2666c6d9bcb9062c7c0ff13146b5f574f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh
get_key() {
    echo $1 | sed -ne 's/^.*"\([a-zA-Z0-9_]\+\)":.*$/\1/p'
}
get_value() {
    echo $1 | sed -ne 's/^.*"[^"]\+":"\?\([A-Za-z0-9$\.]*\)"\?$/\1/p'
    echo $1 | sed -ne 's/^.*"[^"]\+":{$/{/p'
}

parse_json() {
    local path=$1

    while read line; do
        if [ "$line" = "{" ]; then continue; fi
        if [ "$line" = "}" ]; then
            return;
        fi

        key=`get_key "$line"`
        value=`get_value "$line"`
        if [ -z "$key" ] || [ -z "$value" ]; then
            echo "skip $line" >&2
            continue
        fi
        if [ "$value" = "{" ]; then
            parse_json "${path}_${key}"
        else
            printf "%s_%s=\"%s\"\n" $path $key $value
        fi
    done
}

preprocess_json() {
    sed -e 's/}/\n}/g' | sed -e 's/\("[^"]\+":\)/\n\1/g' | tr -d ',' | sed -e 's/\\u/\\\\u/g'
}

eval $(curl -s --max-time 3 "http://data.mtgox.com/api/2/BTCEUR/money/ticker" | preprocess_json | parse_json "ticker")

if [ "$ticker_result" = "success" ]; then
    echo -n "฿"
    echo -n " ➘ $ticker_data_sell_value $ticker_data_sell_currency"
    echo -n " ➚ $ticker_data_buy_value $ticker_data_buy_currency"
    echo -n " ⋀ $ticker_data_high_value $ticker_data_high_currency"
    echo -n " ⋁ $ticker_data_low_value $ticker_data_low_currency"
    echo
fi