Commit b1a0d1b7679233736bc6756dcb9bdc9ca162a7ed
Refactor getquote
Charles Lehner committed on 8/19/2015, 12:49:05 AMParent: 9760d24c587e4452b36c974574c132cdd862a71e
Files changed
getquote | changed |
getquote | ||
---|---|---|
@@ -47,121 +47,121 @@ | ||
47 | 47 | my $content = request(shift); |
48 | 48 | return $content ? decode_json($content) : undef; |
49 | 49 | } |
50 | 50 | |
51 | -if ($symbol eq '$' or $symbol eq 'USD') { | |
52 | - if (my $ticker = json_req('https://api.bitcoinaverage.com/ticker/USD')) { | |
53 | - $price = sprintf '%.8f BTC', 1/$ticker->{last} | |
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 | + } | |
54 | 56 | } |
55 | -} | |
56 | 57 | |
57 | -if (!$price and $symbol eq 'BTC') { | |
58 | - if (my $ticker = json_req('https://api.bitcoinaverage.com/ticker/USD')) { | |
59 | - $price = '$' . $ticker->{last}; | |
58 | + if ($symbol eq 'BTC') { | |
59 | + if (my $ticker = json_req('https://api.bitcoinaverage.com/ticker/USD')) { | |
60 | + return '$' . $ticker->{last}; | |
61 | + } | |
60 | 62 | } |
61 | -} | |
62 | 63 | |
63 | -if (!$price and $symbol eq 'ETH') { | |
64 | - if (my $ticker = json_req('https://poloniex.com/public?command=returnTicker')) { | |
65 | - $price = '$' . $ticker->{USDT_ETH}->{last}; | |
64 | + if ($symbol eq 'ETH') { | |
65 | + if (my $ticker = json_req('https://poloniex.com/public?command=returnTicker')) { | |
66 | + return '$' . $ticker->{USDT_ETH}->{last}; | |
67 | + } | |
66 | 68 | } |
67 | -} | |
68 | 69 | |
69 | -my %coinabul_markets = ( | |
70 | - AU => 'Gold', | |
71 | - AG => 'Silver', | |
72 | -); | |
70 | + my %coinabul_markets = ( | |
71 | + AU => 'Gold', | |
72 | + AG => 'Silver', | |
73 | + ); | |
73 | 74 | |
74 | -my %cryptotrade_markets = ( | |
75 | - CTB => 'BTC', | |
76 | - CTL => 'LTC', | |
77 | - ESB => 'BTC', | |
78 | - ESL => 'LTC', | |
79 | - GGB => 'BTC', | |
80 | - PPC => 'BTC', | |
81 | - LTC => 'BTC', | |
82 | - NMC => 'BTC', | |
83 | - XPM => 'BTC', | |
84 | - FTC => 'BTC', | |
85 | -); | |
75 | + my %cryptotrade_markets = ( | |
76 | + CTB => 'BTC', | |
77 | + CTL => 'LTC', | |
78 | + ESB => 'BTC', | |
79 | + ESL => 'LTC', | |
80 | + GGB => 'BTC', | |
81 | + PPC => 'BTC', | |
82 | + LTC => 'BTC', | |
83 | + NMC => 'BTC', | |
84 | + XPM => 'BTC', | |
85 | + FTC => 'BTC', | |
86 | + ); | |
86 | 87 | |
87 | -my %cryptostocks_markets = ( | |
88 | - BTCINVEST => 1, | |
89 | - GREEN => 1, | |
90 | -); | |
88 | + my %cryptostocks_markets = ( | |
89 | + BTCINVEST => 1, | |
90 | + GREEN => 1, | |
91 | + ); | |
91 | 92 | |
92 | -my %havelock_markets = ( | |
93 | - KCIM => 1, | |
94 | - AM => 'AM1', | |
95 | - HIF => 1, | |
96 | - MS => 1, | |
97 | - RENT => 1, | |
98 | - SFI => 1, | |
99 | - VTX => 1 | |
100 | -); | |
93 | + my %havelock_markets = ( | |
94 | + KCIM => 1, | |
95 | + AM => 'AM1', | |
96 | + HIF => 1, | |
97 | + MS => 1, | |
98 | + RENT => 1, | |
99 | + SFI => 1, | |
100 | + VTX => 1 | |
101 | + ); | |
101 | 102 | |
102 | -my %rateexchange_markets = ( | |
103 | - '€' => 'EUR', | |
104 | - EUR => 1 | |
105 | -); | |
103 | + my %rateexchange_markets = ( | |
104 | + '€' => 'EUR', | |
105 | + EUR => 1 | |
106 | + ); | |
106 | 107 | |
107 | -if (!$price and my $marketid = $coinabul_markets{$symbol}) { | |
108 | - if (my $market = json_req('http://coinabul.com/api.php')) { | |
109 | - $price = '$' . $market->{$marketid}->{USD}; | |
108 | + if (my $marketid = $coinabul_markets{$symbol}) { | |
109 | + if (my $market = json_req('http://coinabul.com/api.php')) { | |
110 | + return '$' . $market->{$marketid}->{USD}; | |
111 | + } | |
110 | 112 | } |
111 | -} | |
112 | 113 | |
113 | -if (!$price and my $currency = $cryptotrade_markets{$symbol}) { | |
114 | - my $ticker = lc($symbol . '_' . $currency); | |
115 | - my $resp = json_req('https://crypto-trade.com/api/1/ticker/' . $ticker); | |
116 | - if ($resp and $resp->{status} eq 'success') { | |
117 | - my $data = $resp->{data}; | |
118 | - $price = $data->{last} || ($data->{min_ask} + $data->{max_bid}) / 2; | |
119 | - $price .= ' ' . $currency; | |
114 | + if (my $currency = $cryptotrade_markets{$symbol}) { | |
115 | + my $ticker = lc($symbol . '_' . $currency); | |
116 | + my $resp = json_req('https://crypto-trade.com/api/1/ticker/' . $ticker); | |
117 | + if ($resp and $resp->{status} eq 'success') { | |
118 | + my $data = $resp->{data}; | |
119 | + return $data->{last} || ($data->{min_ask} + $data->{max_bid}) / 2; | |
120 | + $price .= ' ' . $currency; | |
121 | + } | |
120 | 122 | } |
121 | -} | |
122 | 123 | |
123 | -if (!$price and $cryptostocks_markets{$symbol}) { | |
124 | - if (my $info = json_req('https://cryptostocks.com/api/get_security_info.json?ticker=' . $symbol)) { | |
125 | - if ($info and $info->{return_code} == 0) { | |
126 | - $price = $info->{last_price} . ' ' . $info->{currency}; | |
124 | + if ($cryptostocks_markets{$symbol}) { | |
125 | + if (my $info = json_req('https://cryptostocks.com/api/get_security_info.json?ticker=' . $symbol)) { | |
126 | + if ($info and $info->{return_code} == 0) { | |
127 | + return $info->{last_price} . ' ' . $info->{currency}; | |
128 | + } | |
127 | 129 | } |
128 | 130 | } |
129 | -} | |
130 | 131 | |
131 | -if (!$price and $havelock_markets{$symbol}) { | |
132 | - if ($havelock_markets{$symbol} != 1) { | |
133 | - $symbol =$havelock_markets{$symbol}; | |
134 | - } | |
135 | - if (my $ticker = json_req('https://www.havelockinvestments.com/r/ticker')) { | |
136 | - if (my $details = $ticker->{$symbol}) { | |
137 | - $price = $details->{last} . ' BTC'; | |
132 | + if ($havelock_markets{$symbol}) { | |
133 | + if ($havelock_markets{$symbol} != 1) { | |
134 | + $symbol = $havelock_markets{$symbol}; | |
138 | 135 | } |
136 | + if (my $ticker = json_req('https://www.havelockinvestments.com/r/ticker')) { | |
137 | + if (my $details = $ticker->{$symbol}) { | |
138 | + return $details->{last} . ' BTC'; | |
139 | + } | |
140 | + } | |
139 | 141 | } |
140 | -} | |
141 | 142 | |
142 | -if (!$price and $rateexchange_markets{$symbol}) { | |
143 | - if ($rateexchange_markets{$symbol} != 1) { | |
144 | - $symbol = $rateexchange_markets{$symbol}; | |
145 | - } | |
146 | - if (my $info = json_req('http://rate-exchange.appspot.com/currency?to=USD&from=' . $symbol)) { | |
147 | - if ($info and $info->{rate}) { | |
148 | - $price = '$' . $info->{rate}; | |
143 | + if ($rateexchange_markets{$symbol}) { | |
144 | + if ($rateexchange_markets{$symbol} != 1) { | |
145 | + $symbol = $rateexchange_markets{$symbol}; | |
149 | 146 | } |
147 | + if (my $info = json_req('http://rate-exchange.appspot.com/currency?to=USD&from=' . $symbol)) { | |
148 | + if ($info and $info->{rate}) { | |
149 | + return '$' . $info->{rate}; | |
150 | + } | |
151 | + } | |
150 | 152 | } |
151 | -} | |
152 | 153 | |
153 | -if (!$price) { | |
154 | 154 | if ($_ = request('http://www.google.com/finance/info?infotype=infoquoteall&q=' . $symbol)) { |
155 | 155 | s/\/\/.*/[/; |
156 | 156 | my $info = decode_json($_); |
157 | 157 | if ($info and $info->[0]) { |
158 | - $price = '$' . $info->[0]->{l}; | |
158 | + return '$' . $info->[0]->{l}; | |
159 | 159 | } |
160 | 160 | } |
161 | 161 | } |
162 | 162 | |
163 | -if ($price) { | |
163 | +if (my $price = getquote()) { | |
164 | 164 | print strftime("%Y/%m/%d %H:%M:%S ", localtime(time())), |
165 | 165 | $symbol, ' ', $price, "\n"; |
166 | 166 | } else { |
167 | 167 | exit 1; |
Built with git-ssb-web