ãã¯ããããããŸã.ã¯ã©ã·ãã¯APIã䜿çšããŠGitHub APIããèããŒã¿ãååŸããã³ãŒããAPI䜿çšãã€ã€æçŽããŠã¢ãã®æ°åã§å®æããŸãã.ããããã®ãæããšå°äººæ°ã®éçºã§ãåžæãæãŠããªã£ãŠæããŸã.ã¡ãªã¿ã«èªåã¯æªã ã«ç¡ææ ã§ã³ãŒããçæããŠããã£ãŠããŸã.
æ£çŽãªãšãããç¡ææ ã§äºãè¶³ãããšããå°è±¡ã§ãã.ã³ãŒãã®æ¹åãåœåã¯çæAIã«ãã£ãŠããã£ãæ¹ãåªç§ã§ããèªåã®ç¡èœãããããããŸã.
èããŒã¿ã®äœ¿ç𿹿³ã¯ã¿ã€ãã«éããªã®ã§ç¹ã«åé¡ãªãããªã£ãŠæããŸã.ã³ã³ãã£ã°ãã¡ã€ã«ã¯æžããããŠã倿°ã«ä»£å
¥ããŠãããã°è¯ãããã§ãããã.äžå¿ãgithubäžã«ãœãŒã¹ã³ãŒããæ²èŒããŠããŸã.
è¯ãã£ãããããðïžïŒâïžïŒå®ãããé¡ãèŽããŸã.
<?php
class GitHubGrass
{
private string $token;
private string $username;
private array $weeks = [];
private int $cellSize = 12;
private int $padding = 2;
private array $colors = [];
public function __construct(string $username, string $token)
{
$this->username = $username;
$this->token = $token;
// è²ã®èšå®ïŒGitHub颚ïŒ
$this->colors = [
'level0' => [235, 237, 240],
'level1' => [155, 233, 168],
'level2' => [64, 196, 99],
'level3' => [48, 161, 78],
'level4' => [33, 110, 57]
];
}
/**
* GitHub APIããèããŒã¿ãååŸ
*/
public function fetchContributions(): bool
{
$url = 'https://api.github.com/graphql';
$query = <<<'JSON'
{
user(login: "USERNAME") {
contributionsCollection {
contributionCalendar {
weeks {
contributionDays {
contributionCount
}
}
}
}
}
}
JSON;
// ãŠãŒã¶ãŒåãåã蟌ã
$query = str_replace("USERNAME", $this->username, $query);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->token,
'User-Agent: ContributionsApp'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['query' => $query]));
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if (!isset($data['data']['user']['contributionsCollection']['contributionCalendar']['weeks'])) {
$this->weeks = []; // 空ã®é
åãèšå®
return false;
}
$this->weeks = $data['data']['user']['contributionsCollection']['contributionCalendar']['weeks'];
return true;
}
/**
* èç»åãçæ
*/
public function generateImage(): string
{
if (count($this->weeks)) {
die('No data available.');
}
// ç»åãµã€ãºèšå®
$width = (count($this->weeks) * ($this->cellSize + $this->padding)) + $this->padding;
$height = (7 * ($this->cellSize + $this->padding)) + $this->padding;
// ç»åäœæ
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);
// è²ã®äœæ
$colorPalette = [];
foreach ($this->colors as $key => $rgb) {
$colorPalette[$key] = imagecolorallocate($image, ...$rgb);
}
// ã»ã«æç»
foreach ($this->weeks as $x => $week) {
foreach ($week['contributionDays'] as $y => $day) {
$count = $day['contributionCount'];
$color = $this->getColor($count, $colorPalette);
// åè§åœ¢ãæç»
imagefilledrectangle(
$image,
$x * ($this->cellSize + $this->padding) + $this->padding,
$y * ($this->cellSize + $this->padding) + $this->padding,
($x + 1) * ($this->cellSize + $this->padding),
($y + 1) * ($this->cellSize + $this->padding),
$color
);
}
}
// åºå
ob_start();
imagepng($image);
$imageData = ob_get_contents();
ob_end_clean();
imagedestroy($image);
$base64 = base64_encode($imageData);
return $base64;
}
/**
* ã³ã³ããªãã¥ãŒã·ã§ã³æ°ã«å¿ããè²ãååŸ
*/
private function getColor(int $count, array $colorPalette)
{
if ($count == 0) {
return $colorPalette['level0'];
} elseif ($count < 5) {
return $colorPalette['level1'];
} elseif ($count < 10) {
return $colorPalette['level2'];
} elseif ($count < 20) {
return $colorPalette['level3'];
} else {
return $colorPalette['level4'];
}
}
}