Skip to content

Commit

Permalink
update to use apiv2.bitcoinaverage.com, since api.bitcoinaverage.com …
Browse files Browse the repository at this point in the history
…just stopped working
  • Loading branch information
dan-da committed Jun 20, 2017
1 parent 7c362fa commit 092d77b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bitprices-v1.0.9
bitprices-v1.0.10
27 changes: 12 additions & 15 deletions bitprices.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,6 @@ protected function get_historic_price( $currency, $timestamp ) {

$map = self::get_historic_prices( $currency );
$price = @$map[$date];

// if date is today, then get 24 hour average.
if( !$price && time() - $timestamp < 86400 ) {
return $this->get_24_hour_avg_price_cached( $currency );
}

return $price;
}

Expand Down Expand Up @@ -677,14 +671,15 @@ protected function get_24_hour_avg_price_cached( $currency ) {
* TODO: abstract for multiple providers.
*/
protected function get_24_hour_avg_price( $currency ) {

$url_mask = 'https://api.bitcoinaverage.com/ticker/global/%s/';
$url = sprintf( $url_mask, strtoupper( $currency ) );

mylogger()->log( "Retrieving $currency 24 hour average price from bitcoinaverage.com", mylogger::info );
$market = 'BTC' . strtoupper($currency);
$url_mask = 'https://apiv2.bitcoinaverage.com/indices/global/ticker/%s';
$url = sprintf( $url_mask, $market );
mylogger()->log( "Retrieving $currency price history from apiv2.bitcoinaverage.com", mylogger::info );
$buf = file_get_contents( $url );
$data = json_decode( $buf, true );
return (int)($data['24h_avg'] * 100);

return $data['averages']['day'] * 100;
}

/**
Expand All @@ -708,7 +703,9 @@ protected static function get_historic_prices($currency) {
return null;
}

$fname = dirname(__FILE__) . sprintf( '/price_history/per_day_all_time_history.%s.csv', $currency );
$market = 'BTC' . strtoupper($currency);

$fname = dirname(__FILE__) . sprintf( '/price_history/per_day_all_time_history.%s.csv', $market );
$exists = file_exists( $fname );
if( $exists ) {
$file_age = time() - filemtime( $fname );
Expand All @@ -718,9 +715,9 @@ protected static function get_historic_prices($currency) {
$dir = dirname( $fname );
file_exists($dir) || mkdir( $dir );

$url_mask = 'https://api.bitcoinaverage.com/history/%s/per_day_all_time_history.csv';
$url = sprintf( $url_mask, strtoupper( $currency ) );
mylogger()->log( "Retrieving $currency price history from bitcoinaverage.com", mylogger::info );
$url_mask = 'https://apiv2.bitcoinaverage.com/indices/global/history/%s?period=alltime&format=csv';
$url = sprintf( $url_mask, $market );
mylogger()->log( "Retrieving $currency price history from apiv2.bitcoinaverage.com", mylogger::info );
$buf = file_get_contents( $url );
file_put_contents( $fname, $buf );

Expand Down

0 comments on commit 092d77b

Please sign in to comment.