Skip to content

Instantly share code, notes, and snippets.

@nicoverbruggen
Created May 31, 2022 21:15
Show Gist options
  • Save nicoverbruggen/4bf805ffa7ed8e5994eccc6c3816f891 to your computer and use it in GitHub Desktop.
Save nicoverbruggen/4bf805ffa7ed8e5994eccc6c3816f891 to your computer and use it in GitHub Desktop.
How to retrieve GitHub stargazer and downloads data via a Command
<?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