Skip to content

Commit

Permalink
daily btc price model, command
Browse files Browse the repository at this point in the history
  • Loading branch information
Baligabor committed Nov 16, 2023
1 parent 0a72427 commit 1e228a2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
57 changes: 57 additions & 0 deletions app/Console/Commands/StoreDailyBtcPriceCommand.php
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;
}
}
9 changes: 6 additions & 3 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Scheduling\Schedule;
use App\Console\Commands\DownloadData;
use App\Console\Commands\StoreDailyBtcPriceCommand;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
Expand All @@ -14,7 +15,8 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
DownloadData::class
DownloadData::class,
StoreDailyBtcPriceCommand::class
];

/**
Expand All @@ -23,12 +25,13 @@ class Kernel extends ConsoleKernel
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
/*protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
//$schedule->command('download:wasabidata')->cron('* * * * * *');
//$schedule->command('store:bitcoinprice')->cron('* * * * * *');
//$schedule->call(DownloadData::class)->cron('* * * * * *');
}
}*/

/**
* Register the commands for the application.
Expand Down
16 changes: 16 additions & 0 deletions app/Models/DailyBtcPrice.php
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'
];
}

0 comments on commit 1e228a2

Please sign in to comment.