Commit 42ee2cb37dcc910335d5389384c21fdaf8132e1a
Add getquote.sh
Charles Lehner committed on 9/19/2016, 3:28:27 AMParent: 32bcd58af6ac1e096b87f0cf6e490ae1f18c8f2f
Files changed
README.md | changed |
getquote | deleted |
getquote.pl | added |
README.md | ||
---|---|---|
@@ -4,9 +4,9 @@ | ||
4 | 4 | A collection of scripts for helping to organize one's finances using |
5 | 5 | [ledger](ledger-cli.org), |
6 | 6 | with a focus on crypto-currencies. |
7 | 7 | |
8 | -© 2013-2015 Charles Lehner, [MIT License](http://cel.mit-license.org/) | |
8 | +© 2013-2016 Charles Lehner, [MIT License](http://cel.mit-license.org/) | |
9 | 9 | |
10 | 10 | Data import scripts |
11 | 11 | ------------------- |
12 | 12 | |
@@ -97,17 +97,22 @@ | ||
97 | 97 | |
98 | 98 | More scripts |
99 | 99 | ------------ |
100 | 100 | |
101 | -#### `getquote` | |
101 | +#### `getquote.pl` | |
102 | 102 | |
103 | -Get quotes for various stocks, currencies, and commodities. | |
103 | +Get quotes for various stocks, currencies, and commodities. Written in Perl and depends on some perl modules. | |
104 | 104 | |
105 | 105 | Covers: |
106 | 106 | |
107 | 107 | BTC, altcoins, gold, silver, Havelock Investments, CryptoStocks, |
108 | 108 | NASDAQ, and sovereign currencies. |
109 | 109 | |
110 | +#### `getquote.sh` | |
111 | + | |
112 | +Get quotes for things. Written in POSIX shell. Depends on curl and jq. | |
113 | +Currently it does not support as many tickers as `getquote.pl` does. | |
114 | + | |
110 | 115 | #### `gethistoric` |
111 | 116 | |
112 | 117 | Get past quotes for BTC/USD, using BitcoinAverage's API. |
113 | 118 |
getquote | ||
---|---|---|
@@ -1,173 +1,0 @@ | ||
1 | -#!/usr/bin/perl | |
2 | -# | |
3 | -# getquote | |
4 | -# | |
5 | -# Get quotes for various markets with a focus on cryptocurrencies | |
6 | -# | |
7 | -# Markets: | |
8 | -# | |
9 | -# BitcoinAverage (BTC/USD) | |
10 | -# Coinabul (Gold, Silver) | |
11 | -# Havelock Investments | |
12 | -# Cryptostocks | |
13 | -# Google Finance | |
14 | -# Rate-Exchange | |
15 | -# | |
16 | - | |
17 | -use strict; | |
18 | - | |
19 | -use Finance::Quote; | |
20 | -use HTTP::Request; | |
21 | -use LWP::UserAgent; | |
22 | -use JSON; | |
23 | -use POSIX qw(strftime); | |
24 | - | |
25 | -my $timeout = 60; | |
26 | - | |
27 | -my $symbol = $ARGV[0]; | |
28 | -my $price; | |
29 | - | |
30 | -if (!$symbol) { | |
31 | - print STDERR "Usage: getquote TICKER\n"; | |
32 | - exit 1; | |
33 | -} | |
34 | - | |
35 | -sub request { | |
36 | - my $url = shift; | |
37 | - print STDERR "Looking up $symbol\n"; | |
38 | - my $req = HTTP::Request->new('GET', $url); | |
39 | - my $ua = LWP::UserAgent->new(agent => 'perl/' . $^V); | |
40 | - $ua->timeout($timeout); | |
41 | - $ua->ssl_opts(verify_hostname => 0); | |
42 | - my $res = $ua->request($req); | |
43 | - return $res->is_success ? $res->decoded_content : undef; | |
44 | -} | |
45 | - | |
46 | -sub json_req { | |
47 | - my $content = request(shift); | |
48 | - return $content ? decode_json($content) : undef; | |
49 | -} | |
50 | - | |
51 | -sub getquote { | |
52 | - if ($symbol eq '$' or $symbol eq 'USD') { | |
53 | - if (my $ticker = json_req('https://api.bitcoinaverage.com/ticker/USD')) { | |
54 | - return sprintf '%.8f BTC', 1/$ticker->{last}; | |
55 | - } | |
56 | - } | |
57 | - | |
58 | - if ($symbol eq 'BTC') { | |
59 | - if (my $ticker = json_req('https://api.bitcoinaverage.com/ticker/USD')) { | |
60 | - return '$' . $ticker->{last}; | |
61 | - } | |
62 | - } | |
63 | - | |
64 | - my %coinabul_markets = ( | |
65 | - AU => 'Gold', | |
66 | - AG => 'Silver', | |
67 | - ); | |
68 | - | |
69 | - my %poloniex_markets = ( | |
70 | - ETH => 'USDT', | |
71 | - PPC => 'BTC', | |
72 | - LTC => 'USDT', | |
73 | - XPM => 'BTC', | |
74 | - NMC => 'BTC', | |
75 | - ); | |
76 | - | |
77 | - my %cryptostocks_markets = ( | |
78 | - BTCINVEST => 1, | |
79 | - GREEN => 1, | |
80 | - ); | |
81 | - | |
82 | - my %havelock_markets = ( | |
83 | - KCIM => 1, | |
84 | - AM => 'AM1', | |
85 | - HIF => 1, | |
86 | - MS => 1, | |
87 | - RENT => 1, | |
88 | - SFI => 1, | |
89 | - VTX => 1 | |
90 | - ); | |
91 | - | |
92 | - my %rateexchange_markets = ( | |
93 | - '€' => 'EUR', | |
94 | - '£' => 'GBP', | |
95 | - '¥' => 'JPY', | |
96 | - EUR => 1, | |
97 | - GBP => 1, | |
98 | - JPY => 1, | |
99 | - CAD => 1, | |
100 | - HKD => 1, | |
101 | - MXN => 1, | |
102 | - INR => 1, | |
103 | - DKK => 1, | |
104 | - CNY => 1, | |
105 | - RUB => 1, | |
106 | - ); | |
107 | - | |
108 | - if ($symbol eq 'FAIR') { | |
109 | - if (my $market = json_req('https://getfaircoin.net/api/ticker')) { | |
110 | - my $amount = $market->{USD}->{last}; | |
111 | - return '$' . $amount; | |
112 | - } | |
113 | - } | |
114 | - | |
115 | - if (my $base = $poloniex_markets{$symbol}) { | |
116 | - my $ticker = $base . '_' . $symbol; | |
117 | - if (my $market = json_req('https://poloniex.com/public?command=returnTicker')) { | |
118 | - my $amount = $market->{$ticker}->{last}; | |
119 | - return $base eq 'USDT' ? '$' . $amount : $amount . ' ' . $base; | |
120 | - } | |
121 | - } | |
122 | - | |
123 | - if (my $marketid = $coinabul_markets{$symbol}) { | |
124 | - if (my $market = json_req('http://coinabul.com/api.php')) { | |
125 | - return '$' . $market->{$marketid}->{USD}; | |
126 | - } | |
127 | - } | |
128 | - | |
129 | - if ($cryptostocks_markets{$symbol}) { | |
130 | - if (my $info = json_req('https://cryptostocks.com/api/get_security_info.json?ticker=' . $symbol)) { | |
131 | - if ($info and $info->{return_code} == 0) { | |
132 | - return $info->{last_price} . ' ' . $info->{currency}; | |
133 | - } | |
134 | - } | |
135 | - } | |
136 | - | |
137 | - if ($havelock_markets{$symbol}) { | |
138 | - if ($havelock_markets{$symbol} != 1) { | |
139 | - $symbol = $havelock_markets{$symbol}; | |
140 | - } | |
141 | - if (my $ticker = json_req('https://www.havelockinvestments.com/r/ticker')) { | |
142 | - if (my $details = $ticker->{$symbol}) { | |
143 | - return $details->{last} . ' BTC'; | |
144 | - } | |
145 | - } | |
146 | - } | |
147 | - | |
148 | - if ($rateexchange_markets{$symbol}) { | |
149 | - if ($rateexchange_markets{$symbol} != 1) { | |
150 | - $symbol = $rateexchange_markets{$symbol}; | |
151 | - } | |
152 | - if (my $info = json_req('https://rate-exchange.herokuapp.com/fetchRate?to=USD&from=' . $symbol)) { | |
153 | - if ($info and $info->{Rate}) { | |
154 | - return '$' . $info->{Rate}; | |
155 | - } | |
156 | - } | |
157 | - } | |
158 | - | |
159 | - if ($_ = request('http://www.google.com/finance/info?infotype=infoquoteall&q=' . $symbol)) { | |
160 | - s/\/\/.*/[/; | |
161 | - my $info = decode_json($_); | |
162 | - if ($info and $info->[0]) { | |
163 | - return '$' . $info->[0]->{l}; | |
164 | - } | |
165 | - } | |
166 | -} | |
167 | - | |
168 | -if (my $price = getquote()) { | |
169 | - print strftime("%Y/%m/%d %H:%M:%S ", localtime(time())), | |
170 | - $symbol, ' ', $price, "\n"; | |
171 | -} else { | |
172 | - exit 1; | |
173 | -} |
getquote.pl | ||
---|---|---|
@@ -1,0 +1,173 @@ | ||
1 | +#!/usr/bin/perl | |
2 | +# | |
3 | +# getquote | |
4 | +# | |
5 | +# Get quotes for various markets with a focus on cryptocurrencies | |
6 | +# | |
7 | +# Markets: | |
8 | +# | |
9 | +# BitcoinAverage (BTC/USD) | |
10 | +# Coinabul (Gold, Silver) | |
11 | +# Havelock Investments | |
12 | +# Cryptostocks | |
13 | +# Google Finance | |
14 | +# Rate-Exchange | |
15 | +# | |
16 | + | |
17 | +use strict; | |
18 | + | |
19 | +use Finance::Quote; | |
20 | +use HTTP::Request; | |
21 | +use LWP::UserAgent; | |
22 | +use JSON; | |
23 | +use POSIX qw(strftime); | |
24 | + | |
25 | +my $timeout = 60; | |
26 | + | |
27 | +my $symbol = $ARGV[0]; | |
28 | +my $price; | |
29 | + | |
30 | +if (!$symbol) { | |
31 | + print STDERR "Usage: getquote TICKER\n"; | |
32 | + exit 1; | |
33 | +} | |
34 | + | |
35 | +sub request { | |
36 | + my $url = shift; | |
37 | + print STDERR "Looking up $symbol\n"; | |
38 | + my $req = HTTP::Request->new('GET', $url); | |
39 | + my $ua = LWP::UserAgent->new(agent => 'perl/' . $^V); | |
40 | + $ua->timeout($timeout); | |
41 | + $ua->ssl_opts(verify_hostname => 0); | |
42 | + my $res = $ua->request($req); | |
43 | + return $res->is_success ? $res->decoded_content : undef; | |
44 | +} | |
45 | + | |
46 | +sub json_req { | |
47 | + my $content = request(shift); | |
48 | + return $content ? decode_json($content) : undef; | |
49 | +} | |
50 | + | |
51 | +sub getquote { | |
52 | + if ($symbol eq '$' or $symbol eq 'USD') { | |
53 | + if (my $ticker = json_req('https://api.bitcoinaverage.com/ticker/USD')) { | |
54 | + return sprintf '%.8f BTC', 1/$ticker->{last}; | |
55 | + } | |
56 | + } | |
57 | + | |
58 | + if ($symbol eq 'BTC') { | |
59 | + if (my $ticker = json_req('https://api.bitcoinaverage.com/ticker/USD')) { | |
60 | + return '$' . $ticker->{last}; | |
61 | + } | |
62 | + } | |
63 | + | |
64 | + my %coinabul_markets = ( | |
65 | + AU => 'Gold', | |
66 | + AG => 'Silver', | |
67 | + ); | |
68 | + | |
69 | + my %poloniex_markets = ( | |
70 | + ETH => 'USDT', | |
71 | + PPC => 'BTC', | |
72 | + LTC => 'USDT', | |
73 | + XPM => 'BTC', | |
74 | + NMC => 'BTC', | |
75 | + ); | |
76 | + | |
77 | + my %cryptostocks_markets = ( | |
78 | + BTCINVEST => 1, | |
79 | + GREEN => 1, | |
80 | + ); | |
81 | + | |
82 | + my %havelock_markets = ( | |
83 | + KCIM => 1, | |
84 | + AM => 'AM1', | |
85 | + HIF => 1, | |
86 | + MS => 1, | |
87 | + RENT => 1, | |
88 | + SFI => 1, | |
89 | + VTX => 1 | |
90 | + ); | |
91 | + | |
92 | + my %rateexchange_markets = ( | |
93 | + '€' => 'EUR', | |
94 | + '£' => 'GBP', | |
95 | + '¥' => 'JPY', | |
96 | + EUR => 1, | |
97 | + GBP => 1, | |
98 | + JPY => 1, | |
99 | + CAD => 1, | |
100 | + HKD => 1, | |
101 | + MXN => 1, | |
102 | + INR => 1, | |
103 | + DKK => 1, | |
104 | + CNY => 1, | |
105 | + RUB => 1, | |
106 | + ); | |
107 | + | |
108 | + if ($symbol eq 'FAIR') { | |
109 | + if (my $market = json_req('https://getfaircoin.net/api/ticker')) { | |
110 | + my $amount = $market->{USD}->{last}; | |
111 | + return '$' . $amount; | |
112 | + } | |
113 | + } | |
114 | + | |
115 | + if (my $base = $poloniex_markets{$symbol}) { | |
116 | + my $ticker = $base . '_' . $symbol; | |
117 | + if (my $market = json_req('https://poloniex.com/public?command=returnTicker')) { | |
118 | + my $amount = $market->{$ticker}->{last}; | |
119 | + return $base eq 'USDT' ? '$' . $amount : $amount . ' ' . $base; | |
120 | + } | |
121 | + } | |
122 | + | |
123 | + if (my $marketid = $coinabul_markets{$symbol}) { | |
124 | + if (my $market = json_req('http://coinabul.com/api.php')) { | |
125 | + return '$' . $market->{$marketid}->{USD}; | |
126 | + } | |
127 | + } | |
128 | + | |
129 | + if ($cryptostocks_markets{$symbol}) { | |
130 | + if (my $info = json_req('https://cryptostocks.com/api/get_security_info.json?ticker=' . $symbol)) { | |
131 | + if ($info and $info->{return_code} == 0) { | |
132 | + return $info->{last_price} . ' ' . $info->{currency}; | |
133 | + } | |
134 | + } | |
135 | + } | |
136 | + | |
137 | + if ($havelock_markets{$symbol}) { | |
138 | + if ($havelock_markets{$symbol} != 1) { | |
139 | + $symbol = $havelock_markets{$symbol}; | |
140 | + } | |
141 | + if (my $ticker = json_req('https://www.havelockinvestments.com/r/ticker')) { | |
142 | + if (my $details = $ticker->{$symbol}) { | |
143 | + return $details->{last} . ' BTC'; | |
144 | + } | |
145 | + } | |
146 | + } | |
147 | + | |
148 | + if ($rateexchange_markets{$symbol}) { | |
149 | + if ($rateexchange_markets{$symbol} != 1) { | |
150 | + $symbol = $rateexchange_markets{$symbol}; | |
151 | + } | |
152 | + if (my $info = json_req('https://rate-exchange.herokuapp.com/fetchRate?to=USD&from=' . $symbol)) { | |
153 | + if ($info and $info->{Rate}) { | |
154 | + return '$' . $info->{Rate}; | |
155 | + } | |
156 | + } | |
157 | + } | |
158 | + | |
159 | + if ($_ = request('http://www.google.com/finance/info?infotype=infoquoteall&q=' . $symbol)) { | |
160 | + s/\/\/.*/[/; | |
161 | + my $info = decode_json($_); | |
162 | + if ($info and $info->[0]) { | |
163 | + return '$' . $info->[0]->{l}; | |
164 | + } | |
165 | + } | |
166 | +} | |
167 | + | |
168 | +if (my $price = getquote()) { | |
169 | + print strftime("%Y/%m/%d %H:%M:%S ", localtime(time())), | |
170 | + $symbol, ' ', $price, "\n"; | |
171 | +} else { | |
172 | + exit 1; | |
173 | +} |
Built with git-ssb-web