Last active
November 20, 2016 23:31
-
-
Save chappy84/92a2fa2a9172c12462498c01ae571c4c to your computer and use it in GitHub Desktop.
Github Starred Dates - displays when you starred repos, which seems to have disappeared from GitHub's UI :(
This file contains hidden or 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 | |
function errorAndDie($message) { | |
echo $message . PHP_EOL; | |
die(1); | |
} | |
$url = 'https://api.github.com/user/starred'; | |
do { | |
$ch = curl_init($url); | |
curl_setopt_array($ch, [ | |
CURLOPT_HEADER => true, | |
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, | |
CURLOPT_HTTPHEADER => [ | |
'Accept: application/vnd.github.v3.star+json' | |
], | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_USERAGENT => 'GitHub-Starred-Repos-Request', | |
CURLOPT_USERPWD => '<username>:<personal access token>', | |
]); | |
$response = curl_exec($ch); | |
$headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if (200 !== $httpStatusCode || false === $response) { | |
errorAndDie('Failed to fetch URL: ' . $url); | |
} | |
$starredRepos = json_decode(substr($response, $headerLength)); | |
if (empty($starredRepos) || !is_array($starredRepos)) { | |
errorAndDie('No starred repos found on URL: ' . $url); | |
} | |
foreach ($starredRepos as $starredRepo) { | |
if (isset($starredRepo->repo->full_name, $starredRepo->starred_at)) { | |
echo $starredRepo->repo->full_name . ': ' . $starredRepo->starred_at . PHP_EOL; | |
} | |
} | |
$headerString = substr($response, 0, $headerLength); | |
$hasMorePages = false; | |
if (preg_match('/^\s*link:\s*(.+)\s*$/im', $headerString, $linksHeader) | |
&& !empty($linksHeader[1]) | |
&& preg_match('/\<([^<>]+)\>\;\s*rel\=\"next\"/', $linksHeader[1], $nextLink) | |
&& !empty($nextLink[1]) | |
) { | |
$url = $nextLink[1]; | |
$hasMorePages = true; | |
} | |
} while (true === $hasMorePages); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment