mirror of
https://github.com/LBRYFoundation/pool.git
synced 2025-09-24 20:30:34 +00:00
yiimp: add "coindb labels" command
to fill missing db coin's labels with http://api.cryptocoincharts.info/listCoins
This commit is contained in:
parent
de607b1bfc
commit
a101cb5e8e
1 changed files with 109 additions and 0 deletions
109
web/yaamp/commands/CoindbCommand.php
Normal file
109
web/yaamp/commands/CoindbCommand.php
Normal 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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue