Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
从 GitHub API 获取仓库附件下载量。受 API 限制,下载量统计不包括 Source ​code。
<?php
/**
* GitHub Release 下载监控
*
* @author AlanDecode | 熊猫小A
* @link https://www.imalan.cn
* @version 1.0
*/
$UserName = 'AlanDecode';
$RepoName = 'Typecho-Theme-VOID';
function curl_file_get_contents($_url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_REFERER, 'https://github.com/');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36');
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$API = "https://api.github.com/repos/{$UserName}/{$RepoName}/releases";
$data = json_decode(curl_file_get_contents($API), true);;
$data = array_reverse($data);
$max = -1;
foreach ($data as $release) {
foreach ($release['assets'] as $asset) {
$max = $asset['download_count'] > $max ? $asset['download_count'] : $max;
}
}
foreach ($data as $release) {
if (!array_key_exists('assets', $release)) {
continue;
}
echo $release['name'].' Published at '.date('Y-m-d', strtotime($release['published_at'])).PHP_EOL;
$BarWidth = 40;
$CountWidth = floor(log10($max)) + 1;
foreach ($release['assets'] as $asset) {
printf("|- %{$CountWidth}d ", $asset['download_count']);
$n = ceil(($asset['download_count'] / $max) * $BarWidth);
for ($i = 0; $i < $BarWidth; $i++) {
if ($i < $n) {
echo '█';
} else {
echo '·';
}
}
echo ' '.$asset['name'].PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.