Files: f1521af2d73e96acaa4e96dfc60dbfc50373845a / getquote.pl
4266 bytesRaw
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 | } |
174 |
Built with git-ssb-web