yiimp: add "coindb labels" command

to fill missing db coin's labels with http://api.cryptocoincharts.info/listCoins
This commit is contained in:
Tanguy Pruvot 2015-09-08 19:28:44 +02:00
parent de607b1bfc
commit a101cb5e8e

View file

@ -0,0 +1,109 @@
<?php
/**
* CoindbCommand is a console command :
* - labels: complete Unknown Coins Labels from CryptoCoinCharts.info
*
* To use this command, enter the following on the command line:
* <pre>
* yiimp coindb labels
* </pre>
*
* @property string $help The command description.
*
*/
class CoindbCommand extends CConsoleCommand
{
protected $basePath;
/**
* Execute the action.
* @param array $args command line parameters specific for this command
* @return integer non zero application exit code after printing help
*/
public function run($args)
{
$runner=$this->getCommandRunner();
$commands=$runner->commands;
$root = realpath(Yii::app()->getBasePath().DIRECTORY_SEPARATOR.'..');
$this->basePath = str_replace(DIRECTORY_SEPARATOR, '/', $root);
if (!isset($args[0])) {
echo "Yiimp coindb command\n";
echo "Usage: yiimp coindb labels\n";
return 1;
} elseif ($args[0] == 'labels') {
self::updateCoinsLabels();
echo "ok\n";
return 0;
}
}
/**
* Provides the command description.
* @return string the command description.
*/
public function getHelp()
{
return parent::getHelp().'coindb labels';
}
public static function getCoinChartsData()
{
$json = file_get_contents('http://api.cryptocoincharts.info/listCoins');
$data = json_decode($json,true);
$array = array();
foreach ($data as $coin) {
$key = strtoupper($coin['id']);
if (empty($key)) continue;
$array[$key] = $coin;
}
return $array;
}
/**
* Link new coin pictures /images/coin-<SYMBOL>.png
*/
public function updateCoinsLabels()
{
$modelsPath = $this->basePath.'/yaamp/models';
if(!is_dir($modelsPath))
echo "Directory $modelsPath is not a directory\n";
require_once($modelsPath.'/db_coinsModel.php');
$obj = CActiveRecord::model('db_coins');
$table = $obj->getTableSchema()->name;
try{
$coins = new $obj;
} catch (Exception $e) {
echo "Error Model: $table \n";
echo $e->getMessage();
continue;
}
if ($coins instanceof CActiveRecord)
{
echo "$table: ".$coins->count()." records\n";
$nbUpdated = 0;
$dataset = $coins->findAll(array('condition'=>'name = :u', 'params'=>array(':u'=>'unknown')));
$json = self::getCoinChartsData();
foreach ($dataset as $coin) {
if ($coin->name == 'unknown' && isset($json[$coin->symbol])) {
$data = $json[$coin->symbol];
if ($data['name'] != $coin->symbol) {
echo "{$coin->symbol}: {$data['name']}\n";
$coin->name = $data['name'];
$nbUpdated += $coin->save();
}
}
}
echo "$nbUpdated coin labels updated\n";
}
}
}