-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\DailyBtcPrice; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Console\Command; | ||
|
||
class StoreDailyBtcPriceCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'store:btcprice'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Daily store btc prices'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$startDate = strtotime(Carbon::create(2018, 1, 1)); | ||
$endDate = strtotime(Carbon::now()); | ||
$response = Http::get('https://api.coingecko.com/api/v3/coins/bitcoin/market_chart/range', [ | ||
'vs_currency' => 'usd', | ||
'from' => $startDate, | ||
'to' => $endDate, | ||
'precision' => '02', | ||
]); | ||
$data = $response->json(); | ||
$prices = $data['prices']; | ||
|
||
foreach ($prices as $key => $price) { | ||
$timestampInMilliseconds = $price[0]; | ||
$timestampInSeconds = $timestampInMilliseconds / 1000; | ||
$date = Carbon::createFromTimestamp($timestampInSeconds)->format('Y-m-d'); | ||
if (!DailyBtcPrice::where('year_month_day', $date)->exists()) { | ||
DailyBtcPrice::create([ | ||
'year_month_day' => $date, | ||
'price' => $price[1] | ||
]); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class DailyBtcPrice extends Model | ||
{ | ||
protected $table = 'dailybtcprice'; | ||
use HasFactory; | ||
protected $fillable = [ | ||
'year_month_day', | ||
'price' | ||
]; | ||
} |