From 4113e05a100a0863f4178367c793cfdc763b2668 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Sun, 23 Jun 2013 20:41:43 +0200 Subject: [PATCH] Adding multi-API support This will allow users to change the API url, added coinchose as an example as pointed out by @vias79 . * tools class detects the API type * getPrice returns the price based on API URL parsed Fixes #236 --- cronjobs/tickerupdate.php | 10 +++-- public/include/classes/tools.class.php | 55 +++++++++++++++++++---- public/include/config/global.inc.dist.php | 12 ++++- 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/cronjobs/tickerupdate.php b/cronjobs/tickerupdate.php index d3778575..48e3678d 100755 --- a/cronjobs/tickerupdate.php +++ b/cronjobs/tickerupdate.php @@ -26,11 +26,13 @@ require_once('shared.inc.php'); require_once(CLASS_DIR . '/tools.class.php'); verbose("Running updates\n"); -if ($aData = $tools->getApi($config['price']['url'], $config['price']['target'])) { - if (!$setting->setValue('price', $aData['ticker']['last'])) - verbose("ERR Table update failed"); +verbose(" Price API Call ... "); +if ($price = $tools->getPrice()) { + verbose("found $price as price\n"); + if (!$setting->setValue('price', $price)) + verbose("unable to update value in settings table\n"); } else { - verbose("ERR Failed download JSON data from " . $config['price']['url'].$config['price']['target'] . "\n"); + verbose("failed to fetch API data: " . $tools->getError() . "\n"); } ?> diff --git a/public/include/classes/tools.class.php b/public/include/classes/tools.class.php index e97b3746..b160711d 100644 --- a/public/include/classes/tools.class.php +++ b/public/include/classes/tools.class.php @@ -9,11 +9,7 @@ if (!defined('SECURITY')) * Implements some common cron tasks outside * the scope of our web application **/ -class Tools { - public function __construct($debug) { - $this->debug = $debug; - } - +class Tools extends Base { /** * Fetch JSON data from an API * @param url string API URL @@ -21,13 +17,13 @@ class Tools { * @param auth array Optional authentication data to be sent with * @return dec array JSON decoded PHP array **/ - public function getApi($url, $target, $auth=NULL) { + private function getApi($url, $target, $auth=NULL) { static $ch = null; static $ch = null; if (is_null($ch)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); } curl_setopt($ch, CURLOPT_URL, $url . $target); // curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); @@ -41,6 +37,49 @@ class Tools { if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists'); return $dec; } + + /** + * Detect the API to properly extract information + * @param url string API URL + * @return data string API type + **/ + private function getApiType($url) { + if (preg_match('/coinchoose.com/', $url)) { + return 'coinchose'; + } else if (preg_match('/btc-e.com/', $url)) { + return 'btce'; + } + $this->setErrorMessage("API URL unknown"); + return false; + } + + /** + * Extract price information from API data + **/ + public function getPrice() { + $aData = $this->getApi($this->config['price']['url'], $this->config['price']['target']); + $strCurrency = $this->config['currency']; + // Check the API type for configured URL + if (!$strApiType = $this->getApiType($this->config['price']['url'])) + return false; + // Extract price depending on API type + switch ($strApiType) { + case 'coinchose': + foreach ($aData as $aItem) { + if($strCurrency == $aItem[0]) + return $aItem['price']; + } + break; + case 'btce': + return $aData['ticker']['last']; + break; + } + // Catchall, we have no data extractor for this API url + $this->setErrorMessage("Undefined API to getPrice() on URL " . $this->config['price']['url']); + return false; + } } -$tools = new Tools($debug); +$tools = new Tools(); +$tools->setDebug($debug); +$tools->setConfig($config); diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 29a46658..3d677814 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -1,7 +1,6 @@ '/ltc_usd/ticker', 'currency' => 'USD' // Used in ministats template ), +/** + * Another Example for API + 'price' => array( + 'url' => 'http://www.coinchoose.com', + 'target' => '/api.php', + 'currency' => 'BTC' + ), + **/ 'ap_threshold' => array( 'min' => 1, 'max' => 250