Adam Bardon
Adam Bardon

Reputation: 3889

GitHub API limit

I have website iOS Cookies where I divide libraries(currently 190) written in Swift into categories. When category is displayed, I'm showing list of it's libraries with number of GitHub stars and description.

Since that number of stars doesn't have to be accurate, I've decided to get it for every library at once with CRON job(once a day). Problem is I'm reaching API limit for some reason:

You have reached GitHub hour limit! Actual limit is: 5000

Which is quite odd, since there's less then 200 libraries, and for each one it's called only once, like this:

// GitHub API
$this->github = new GitHub($page);
// list of libraries stored in .yaml file
$libraries = $this->config->get('plugins.swift-version.libraries');
$arrlength = count($libraries);

$array = Array();   

for($x = 0; $x < $arrlength; $x++) {
    $link = $libraries[$x]['link'];
    $path = parse_url($link, PHP_URL_PATH);
    $segments = explode('/', $path);

    $author = $segments[1];
    $repo = $segments[2];

    // actual API calling to get number of stars for library
    $libraries[$x]['stargazers_count'] = $this->github->client->api('repo')->show($author, $repo)['stargazers_count'];     
}

Am I missing something?

Upvotes: 0

Views: 337

Answers (1)

Sammitch
Sammitch

Reputation: 32232

To save you the trouble of writing a full API client, and also because that client is on my to-do list so I need to try out the github API anyway, here's a quick and dirty example which uses precisely one API request:

$url = "https://api.github.com/repos/realm/realm-cocoa";

$token = /* https://github.com/settings/tokens */;

$headers = [
    'Authorization: token ' . $token,
];

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => $url,
    CURLOPT_HEADER => false,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0',
]);

$foo = json_decode(curl_exec($ch), true);

curl_close($ch);

var_dump($foo['description'], $foo['stargazers_count']);

Output:

string(64) "Realm is a mobile database: a replacement for Core Data & SQLite"
int(5888)

Upvotes: 1

Related Questions