Backend coins and update
This commit is contained in:
@ -1,16 +1,18 @@
|
||||
<?php
|
||||
namespace ProVM\Crypto\Common\Service;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Carbon\Carbon;
|
||||
use ProVM\Common\Factory\Model as Factory;
|
||||
use ProVM\Crypto\Coin;
|
||||
use ProVM\Crypto\Value;
|
||||
|
||||
class Update {
|
||||
protected $factory;
|
||||
protected $execs;
|
||||
public function __construct(Factory $factory, array $executables) {
|
||||
protected $client;
|
||||
public function __construct(Factory $factory, Client $client) {
|
||||
$this->factory = $factory;
|
||||
$this->execs = $executables;
|
||||
$this->client = $client;
|
||||
$this->load();
|
||||
}
|
||||
public function load() {
|
||||
@ -22,39 +24,154 @@ class Update {
|
||||
}
|
||||
protected $coins;
|
||||
public function register(int $coin_id, int $type = 0) {
|
||||
/*if (array_search($coin_id, $this->coins[$type]) !== false) {
|
||||
return;
|
||||
}*/
|
||||
if (array_search($coin_id, $this->coins[$type]) !== false) {
|
||||
return false;
|
||||
}
|
||||
$this->coins[$type] []= $coin_id;
|
||||
$this->getHistorical($coin_id, $type);
|
||||
$check = \ORM::for_table('coin_registers')->where('coin_id', $coin_id)->find_one();
|
||||
if (!$check) {
|
||||
\ORM::raw_execute("INSERT INTO coin_registers (coin_id, type) VALUES (?, ?)", [$coin_id, $type]);
|
||||
return \ORM::raw_execute("INSERT INTO coin_registers (coin_id, type, date_Time) VALUES (?, ?, NOW())", [$coin_id, $type]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
protected function getHistorical(int $coin_id, int $type) {
|
||||
$coin = $this->factory->find(Coin::class)->one($coin_id);
|
||||
$f = Carbon::now();
|
||||
$exe = [$this->execs[$type]];
|
||||
$url = [];
|
||||
switch ($type) {
|
||||
case 0:
|
||||
$exe []= '-i ' . $coin->identifier;
|
||||
$exe []= '-c usd,clp';
|
||||
$exe []= 'hist -hi';
|
||||
$exe []= '-f ' . $f->copy()->subYears(10)->timestamp;
|
||||
$exe []= '-t ' . $f->timestamp();
|
||||
break;
|
||||
return $this->getHistoricalCrypto($coin);
|
||||
case 1:
|
||||
$exe []= '-i ' . $coin->identifier;
|
||||
$exe []= 'hist -hi';
|
||||
$exe []= '-s ' . $f->copy()->subYears(10)->year;
|
||||
break;
|
||||
return $this->getHistoricalIndicador($coin);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
!d(implode(' ', $exe));
|
||||
$output = shell_exec(implode(' ', $exe));
|
||||
!d($output);
|
||||
}
|
||||
protected function getHistoricalCrypto(Coin $coin) {
|
||||
$f = Carbon::now();
|
||||
$url = [];
|
||||
$url []= 'crypto';
|
||||
$url []= 'historical';
|
||||
$url []= $coin->identifier;
|
||||
$url []= $f->copy()->subYears(10)->timestamp;
|
||||
$url []= $f->timestamp;
|
||||
$url = implode('/', $url);
|
||||
$to = $this->factory->find(Coin::class)->where([['code', 'USD']])->one();
|
||||
$response = $this->client->get($url);
|
||||
$results = json_decode($response->getBody()->getContents());
|
||||
$created = [];
|
||||
foreach ($results->prices as $result) {
|
||||
$d = Carbon::createFromTimestamp(substr($result[0], 0, -3));
|
||||
$value = $result[1];
|
||||
$data = [
|
||||
'date_time' => $d->format('Y-m-d H:i:s'),
|
||||
'coin_id' => $coin->id,
|
||||
'value' => $value,
|
||||
'unit_id' => $to->id
|
||||
];
|
||||
$value = Value::add($this->factory, $data);
|
||||
$status = $value->save();
|
||||
$created []= [
|
||||
'value' => $value->toArray(),
|
||||
'created' => $status
|
||||
];
|
||||
}
|
||||
return $created;
|
||||
}
|
||||
protected function getHistoricalIndicador(Coin $coin) {
|
||||
$f = Carbon::now();
|
||||
$urls = [];
|
||||
for ($i = 10; $i >= 0; $i --) {
|
||||
$url = [];
|
||||
$url []= 'indicador';
|
||||
$url []= 'historical';
|
||||
$url []= $coin->identifier;
|
||||
$url []= $f->copy()->subYears($i)->year;
|
||||
$urls []= implode('/', $url);
|
||||
}
|
||||
$to = $this->factory->find(Coin::class)->where([['code', 'CLP']])->one();
|
||||
$created = [];
|
||||
foreach ($urls as $url) {
|
||||
$response = $this->client->get($url);
|
||||
$results = json_decode($response->getBody()->getContents());
|
||||
foreach ($results->serie as $result) {
|
||||
$d = Carbon::parse($result->fecha);
|
||||
$value = $result->valor;
|
||||
$data = [
|
||||
'date_time' => $d->format('Y-m-d H:i:s'),
|
||||
'coin_id' => $coin->id,
|
||||
'value' => $value,
|
||||
'unit_id' => $to->id
|
||||
];
|
||||
$value = Value::add($this->factory, $data);
|
||||
$status = $value->save();
|
||||
$created []= [
|
||||
'value' => $value->toArray(),
|
||||
'created' => $status
|
||||
];
|
||||
}
|
||||
}
|
||||
return $created;
|
||||
}
|
||||
public function run() {
|
||||
$created = [];
|
||||
$created = array_merge($created, $this->getCryptos($this->coins[0]));
|
||||
$created = array_merge($created, $this->getIndicadores($this->coins[1]));
|
||||
return $created;
|
||||
}
|
||||
protected function getCryptos(array $coins) {
|
||||
$created = [];
|
||||
foreach ($coins as $coin_id) {
|
||||
$coin = $this->factory->find(Coin::class)->one($coin_id);
|
||||
$url = [];
|
||||
$url []= 'crypto';
|
||||
$url []= $coin->identifier;
|
||||
$url = implode('/', $url);
|
||||
$to = $this->factory->find(Coin::class)->where([['code', 'USD']])->one();
|
||||
$response = $this->client->get($url);
|
||||
$results = json_decode($response->getBody()->getContents());
|
||||
$data = [
|
||||
'date_time' => $results->n->last_updated_at,
|
||||
'coin_id' => $coin->id,
|
||||
'value' => 1 / $results->n->usd,
|
||||
'unit_id' => $to->id
|
||||
];
|
||||
$value = Value::add($this->factory, $data);
|
||||
$status = $value->save();
|
||||
$created []= [
|
||||
'value' => $value->toArray(),
|
||||
'created' => $status
|
||||
];
|
||||
}
|
||||
return $created;
|
||||
}
|
||||
protected function getIndicadores(array $coins) {
|
||||
$created = [];
|
||||
foreach ($coins as $coin_id) {
|
||||
$coin = $this->factory->find(Coin::class)->one($coin_id);
|
||||
$f = Carbon::now();
|
||||
$url = [];
|
||||
$url []= 'indicador';
|
||||
$url []= $coin->identifier;
|
||||
$url []= $f->format('d-m-Y');
|
||||
$url = implode('/', $url);
|
||||
$to = $this->factory->find(Coin::class)->where([['code', 'CLP']])->one();
|
||||
$response = $this->client->get($url);
|
||||
$results = json_decode($response->getBody()->getContents());
|
||||
$data = [
|
||||
'date_time' => $results->serie[0]->fecha,
|
||||
'coin_id' => $coin->id,
|
||||
'value' => $results->serie[0]->valor,
|
||||
'unit_id' => $to->id
|
||||
];
|
||||
$value = Value::add($this->factory, $data);
|
||||
$status = $value->save();
|
||||
$created []= [
|
||||
'value' => $value->toArray(),
|
||||
'created' => $status
|
||||
];
|
||||
}
|
||||
return $created;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user