Skip to content

Commit ad1b59e

Browse files
authored
Add extra token (#188)
1 parent 0101336 commit ad1b59e

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/stats.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ function getContributionGraphs(string $user): array
6060
return $response;
6161
}
6262

63+
/**
64+
* Get all tokens from environment variables (TOKEN, TOKEN2, TOKEN3, etc.) if they are set
65+
*
66+
* @return array<string> List of tokens
67+
*/
68+
function getGitHubTokens() {
69+
// result is already calculated
70+
if (isset($GLOBALS["ALL_TOKENS"])) {
71+
return $GLOBALS["ALL_TOKENS"];
72+
}
73+
// find all tokens in environment variables
74+
$tokens = array($_SERVER["TOKEN"] ?? "");
75+
for ($i = 2; $i < 4; $i++) {
76+
if (isset($_SERVER["TOKEN$i"])) {
77+
// add token to list
78+
$tokens[] = $_SERVER["TOKEN$i"];
79+
}
80+
}
81+
// store for future use
82+
$GLOBALS["ALL_TOKENS"] = $tokens;
83+
return $tokens;
84+
}
85+
6386
/** Create a CurlHandle for a POST request to GitHub's GraphQL API
6487
*
6588
* @param string $query GraphQL query
@@ -68,7 +91,8 @@ function getContributionGraphs(string $user): array
6891
*/
6992
function getGraphQLCurlHandle(string $query)
7093
{
71-
$token = $_SERVER["TOKEN"];
94+
$all_tokens = getGitHubTokens();
95+
$token = $all_tokens[array_rand($all_tokens)];
7296
$headers = array(
7397
"Authorization: bearer $token",
7498
"Content-Type: application/json",
@@ -147,7 +171,7 @@ function getContributionYears(string $user): array
147171
// API Error
148172
if (!empty($response->errors)) {
149173
// Other errors that contain a message field
150-
throw new InvalidArgumentException($response->data->errors[0]->message);
174+
throw new InvalidArgumentException($response->errors[0]->message);
151175
}
152176
// API did not return data
153177
if (!isset($response->data) && isset($response->message)) {

0 commit comments

Comments
 (0)