Skip to content

Instantly share code, notes, and snippets.

@gmarokov
Last active October 22, 2019 06:53
Show Gist options
  • Save gmarokov/87999185bab5cbcc637902ac9bcb133c to your computer and use it in GitHub Desktop.
Save gmarokov/87999185bab5cbcc637902ac9bcb133c to your computer and use it in GitHub Desktop.
Search repos on GitHub by given search string
<?php
$searchTerm = 'mail';
$language = 'php';
$reposPerPage = 100;
$repoUrls = array();
$page = 1;
// Start paging
do {
$request = 'https://api.github.com/search/repositories?q='.$searchTerm.'+language:'.$language.'+in:name&sort=stars&order=desc&per_page='.$reposPerPage.'&page='.$page;
$ch = curl_init($request);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === FALSE) {
echo "cURL Error: " . curl_error($ch) . "with this request:" . $request . "\n";
die();
}
curl_close($ch);
// Decode and save data
$repos = json_decode($response, true);
$reposOnPage = count($repos['items']);
foreach($repos['items'] as $repo) {
$repoUrls[] = $repo['html_url'];
}
echo "Page " .$page. " processed.. \n";
// Rate limiting each 10 requests sleep per minute #github allows 10 requests per minute#
if($page % 11 == 0) {
echo 'Rate limit hited, waiting 60 seconds..';
sleep(61);
}
$page++;
// Exit loop when repos are less than 100
} while ($page <= 10 && $reposOnPage <= $reposPerPage);
echo "Total processed pages: " . ($page-1) . "\n";
echo "Total repo urls: " . count($repoUrls) . "\n";
echo "Writing to file.. \n";
// Write to csv file
$fp = fopen('repos.csv', 'w');
fputcsv($fp, $repoUrls);
fclose($fp);
echo "Done! Exiting.. \n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment