Created
May 31, 2022 21:15
-
-
Save nicoverbruggen/4bf805ffa7ed8e5994eccc6c3816f891 to your computer and use it in GitHub Desktop.
How to retrieve GitHub stargazer and downloads data via a Command
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
<?php declare(strict_types=1); | |
namespace App\Console\Commands; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Http; | |
use Illuminate\Support\Facades\Log; | |
use Illuminate\Console\Command; | |
/** @codeCoverageIgnore */ | |
final class RetrieveGitHubData extends Command | |
{ | |
protected $signature = 'retrieve:github-data'; | |
protected $description = 'Retrieves GitHub data for open source projects.'; | |
public function handle(): int | |
{ | |
$repositoryResponse = Http::get('https://api.github.com/repos/nicoverbruggen/phpmon'); | |
$releasesResponse = Http::get('https://api.github.com/repos/nicoverbruggen/phpmon/releases'); | |
if ($repositoryResponse->failed()) { | |
Log::error($repositoryResponse->body()); | |
return 1; | |
} | |
if ($releasesResponse->failed()) { | |
Log::error($releasesResponse->body()); | |
return 1; | |
} | |
$data = json_decode($repositoryResponse->body()); | |
$releases = json_decode($releasesResponse->body()); | |
$total = collect($releases)->where('prerelease', false)->sum(function ($release) { | |
return collect($release->assets)->sum('download_count'); | |
}); | |
DB::table('stargazer_log') | |
->updateOrInsert([ | |
'date' => now()->startOfDay(), | |
], [ | |
'app' => $data->full_name, | |
'count' => $data->stargazers_count, | |
'total_downloads' => $total | |
]); | |
$this->info("PHP Monitor currently has {$data->stargazers_count} stargazers and {$total} total downloads."); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment