kristopolous / TickTick

JSON in your Bash scripts
http://9ol.es/TheEmperorsNewClothes.html
Other
579 stars 55 forks source link

Parsing feed from mtgox not working #27

Closed zQueal closed 11 years ago

zQueal commented 11 years ago

I was attempting to use TickTick to render the current price of BTC form MtGox via a terminal and Bash. It seems easy enough, however, I'm unable to get it to print the current price.

#!/bin/bash

. /root/.bin/ticktick.sh

DATA=`curl http://data.mtgox.com/api/2/BTCUSD/money/ticker`

tickParse "$DATA"

echo ``data["last"]``

Outputs:

root@ks4003010 .bin]$ sh btc
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
105  1367    0  1367    0     0   9669      0 --:--:-- --:--:-- --:--:-- 19253

root@ks4003010 .bin]$ 

The script itself seems to work just fine, it's simply not printing the requested information. Any advice?

kristopolous commented 11 years ago

it's the $. $'s are supported for variable injection. It's part of the syntax. You need to escape them.

Good luck with your investments. Looks like a fun ride.

zQueal commented 11 years ago

@kristopolous,

Thanks so far for the help! I would have never thought of that (I'm still new to bash).

However, I still don't have it working quite yet:

#!/bin/bash

. /root/.bin/ticktick.sh

DATA=`curl -silent http://data.mtgox.com/api/2/BTCUSD/money/ticker`

tickParse ${DATA}

echo ``data{last}{display_short}``

I silenced curl, and was able to figure out how to escape ${DATA} but I'm still not able to return the information I want, which is stored: data->last->display_short.

image

Any ideas?

kristopolous commented 11 years ago

I'll look more into it. But if you are new to bash then I sincerely and empathetically plead you to not use this library. The P languages (perl, python, php) along with node and ruby are great at this kind of thing. They are really truly fantastic.

If I was going to do what you were doing, even as the author of the library, I'd do it in php or node.

zQueal commented 11 years ago

I was almost positive I explained, but now that I look, I didn't. I work on many terminals throughout a day, some have PHP, Perl, Ruby, and Python, and some have none. The reason why I chose bash is because they all run linux. At heart I'm a pretty confident PHP developer, so I've already been able to accomplish this easily with PHP:

#!/usr/bin/env php
<?php

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($c, CURLOPT_URL, 'http://data.mtgox.com/api/2/BTCUSD/money/ticker');

$data = curl_exec($c);
curl_close($c);

$obj = json_decode($data);

echo print_r($obj->{'data'}->{'last'}->{'display_short'}."\n", true);

?>

But as I said, not all the terminals I work at carry PHP. But I do see what you mean about being new to bash and relying on a single library. Perhaps I'll simply set up a curl option instead.

Regardless, thanks for the help! :+1:

kristopolous commented 11 years ago

they don't all have perl? that's pretty ubiquitous. Well ok ... let me look into this more. here I'll even reopen it.

kristopolous commented 11 years ago

ok so i've looked further. There's really no parent object support like what you are using.

echo ``data["last"]``

What happens is that the code gets rewritten in bash.

if you ran this:

set | grep ^__tick_data

you'll get this after the tickparse

__tick_data_data_last_local_display_short=5.60
__tick_data_data_last_local_value=95.59992
__tick_data_data_last_local_value_int=9559992
...
__tick_data_data_now=1366041828601185
__tick_data_data_sell_currency=USD
__tick_data_data_sell_display=5.60

If you do this:

echo ``data["last"].items()`

You'll see all your vars

__tick_data_data_last_all_currency __tick_data_data_last_all_display 
__tick_data_data_last_all_display_short __tick_data_data_last_all_value 
__tick_data_data_last_all_value_int __tick_data_data_last_currency 
__tick_data_data_last_display __tick_data_data_last_display_short 
__tick_data_data_last_local_currency __tick_data_data_last_local_display 
__tick_data_data_last_local_display_short __tick_data_data_last_local_value 
__tick_data_data_last_local_value_int __tick_data_data_last_orig_currency 
__tick_data_data_last_orig_display __tick_data_data_last_orig_display_short 
__tick_data_data_last_orig_value __tick_data_data_last_orig_value_int 
__tick_data_data_last_value __tick_data_data_last_value_int

so you can do something like this:

echo ``data.last.orig.value``

And I get

95.30002

You can use any thing else in that list. Take out the "__tickdata" and then replace the _ with . and there you go.

zQueal commented 11 years ago

Well, I guess I shouldn't say that; all Perl scripts are restricted from running except for a set of white-listed scripts; for security purposes.

I've updated the script to reflect your changes:

root@term503 ~]$ btc
59.23001
root@term503 ~]$ 

And as you can see, it's working like a charm! Thanks very much for your continued support!