From 9d65986c57a8bb41e274518e435c61d24e6385b9 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Mon, 27 Nov 2017 15:01:07 +0100 Subject: [PATCH] tools: add coincap.io api for missing labels --- web/yaamp/commands/CoindbCommand.php | 54 +++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/web/yaamp/commands/CoindbCommand.php b/web/yaamp/commands/CoindbCommand.php index 37c8c85..41892a4 100644 --- a/web/yaamp/commands/CoindbCommand.php +++ b/web/yaamp/commands/CoindbCommand.php @@ -40,6 +40,7 @@ class CoindbCommand extends CConsoleCommand } elseif ($args[0] == 'labels') { $nbUpdated = $this->updateCryptopiaLabels(); + $nbUpdated += $this->updateCoinCapLabels(); $nbUpdated += $this->updateLiveCoinLabels(); $nbUpdated += $this->updateYiimpLabels("api.yiimp.eu"); $nbUpdated += $this->updateFromJson(); @@ -71,6 +72,52 @@ class CoindbCommand extends CConsoleCommand return parent::getHelp().'coindb labels'; } + /** + * coincap.io api + */ + public static function getCoinCapData() + { + $json = file_get_contents('http://coincap.io/front'); + $data = json_decode($json,true); + $array = array(); + foreach ($data as $coin) { + $key = strtoupper($coin['short']); + if (empty($key)) continue; + $array[$key] = $coin; + } + return $array; + } + + public function updateCoinCapLabels() + { + $coins = new db_coins; + $nbUpdated = 0; + + $dataset = $coins->findAll(array( + 'condition'=>"name='unknown' OR name=symbol" + )); + + if (!empty($dataset)) + { + $json = self::getCoinCapData(); + if (empty($json)) return 0; + + foreach ($dataset as $coin) { + if ($coin->name == 'unknown' && isset($json[$coin->symbol])) { + $data = $json[$coin->symbol]; + if ($data['long'] != $coin->name) { + echo "{$coin->symbol}: {$data['long']}\n"; + $coin->name = trim($data['long']); + $nbUpdated += $coin->save(); + } + } + } + if ($nbUpdated) + echo "$nbUpdated coin labels updated from coincap.io\n"; + } + return $nbUpdated; + } + /** * Special for cryptopia coins */ @@ -92,9 +139,6 @@ class CoindbCommand extends CConsoleCommand public function updateCryptopiaLabels() { - $modelsPath = $this->basePath.'/yaamp/models'; - require_once($modelsPath.'/db_coinsModel.php'); - $coins = new db_coins; $nbUpdated = 0; @@ -177,7 +221,9 @@ class CoindbCommand extends CConsoleCommand if (!empty($dataset)) { $url = "http://{$pool}/api/currencies"; - $json = json_decode(file_get_contents($url), true); + $data = @ file_get_contents($url); + if (empty($data)) return 0; + $json = json_decode($data, true); if (!empty($json)) foreach ($dataset as $coin) {