From 27cd5fa4e8cc7eb45f0d2d7661160b94156067ff Mon Sep 17 00:00:00 2001 From: Joshua Seigler Date: Fri, 1 Jul 2016 01:05:49 -0400 Subject: [PATCH] added currency support checking --- index.php | 17 +++++++---------- views/chart.php | 45 ++++++++++++++++++++++++++++++++++++++++++--- views/index.php | 6 +++--- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/index.php b/index.php index 86a2891..dd80341 100644 --- a/index.php +++ b/index.php @@ -1,10 +1,8 @@ map('GET', '/', function() { }); // map cryptocurrency stuff -$router->map( 'GET', '/charts/[dark|light:theme]/[a:curA]-[a:curB]/[7d|24h:duration]/[svg|png:format]', function($theme, $curA, $curB, $duration, $format) { +$router->map( 'GET', '/charts/[dark|light:theme]/[a:curA]-[btc:curB]/[a:duration]/[svg|png:format]', function($theme, $curA, $curB, $duration, $format) { require __DIR__ . '/views/chart.php'; - renderChart( + return renderChart( $theme, - strtoupper($curB.'_'.$curA), - 60 * 60 * 24 * ($duration == '7d' ? 7 : 1), - ($duration == '7d' ? 1800 : 300), + $curA, + $curB, + $duration, $format, 800, 200, 12 ); - return true; }); // match current request url diff --git a/views/chart.php b/views/chart.php index d0a58bb..bd8c070 100644 --- a/views/chart.php +++ b/views/chart.php @@ -22,15 +22,53 @@ function getJson($url) { function renderChart( $theme, - $pair, - $dataDuration = (7 * 24 * 60 * 60), - $dataResolution = 1800, + $currencyA, + $currencyB, + $duration, $format = 'svg', $width = 800, $height = 200, $fontSize = 12 ) { + $durations = [ +// '30d'=> [ +// 'duration' => 60 * 60 * 24 * 30, +// 'resolution' => 7200 // 2h +// ], + '7d'=> [ + 'duration' => 60 * 60 * 24 * 7, + 'resolution' => 1800 // 30m + ], + '24h' => [ + 'duration' => 60 * 60 * 24 * 1, + 'resolution' => 900 // 15m + ] + ]; + + if (array_key_exists($duration, $durations)) { + $dataDuration = $durations[$duration]['duration']; + $dataResolution = $durations[$duration]['resolution']; + } else { + return false; + } + + $supportedCurrencies = CacheManager::get('poloniex-supported-currencies'); + if (is_null($supportedCurrencies)) { + $supportedCurrenciesJson = getJson('https://poloniex.com/public?command=returnCurrencies'); + foreach ($supportedCurrenciesJson as $key => $value) { + if ($value->delisted == 0) { + $supportedCurrencies[] = strtolower($key); + } + } + CacheManager::set('poloniex-supported-currencies', $supportedCurrencies, 60 * 60 * 24 * 7); // asking once a week doesn't seem like too much + } + if ($currencyB != 'btc' || !in_array($currencyA, $supportedCurrencies)) { + return false; + } + + $pair = strtoupper($currencyB . '_' . $currencyA); // poloniex you strange + $chartCacheKey = 'poloniex-'.$theme.'-'.$pair.'-'.$dataDuration.'-'.$format; $result = CacheManager::get($chartCacheKey); @@ -88,4 +126,5 @@ function renderChart( header('Content-Disposition: inline; filename="Dash-chart-' . gmdate('Y-m-d\THis+0', $startTime) . '--' . gmdate('Y-m-d\THis+0') . '.png"'); } echo $result; + return true; } diff --git a/views/index.php b/views/index.php index 6f5886f..6adffe4 100644 --- a/views/index.php +++ b/views/index.php @@ -54,16 +54,16 @@

Poloniex Dash/BTC Price

- Poloniex Dash/BTC price + Poloniex Dash/BTC price
7 Day Dash price in BTC http://cryptohistory.org/charts/dark/dash-btc/7d/svg

Build your own chart:

The URL is flexible:
- http://cryptohistory.org/charts/{theme}/{currency}/{timespan}/{format}.

+ http://cryptohistory.org/charts/{theme}/{currency}-btc/{timespan}/{format}.

Theme: dark or light. (More planned)

-

Currency: anything active on Poloniex.

+

Currency: anything active on Poloniex. Prices are all in bitcoin.

Timespan: 30d, 7d, or 24h. (More planned)

Format: svg (best) or png.