Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit a328c6d

Browse files
committed
add intelligent token rate limit detection
1 parent 6549a74 commit a328c6d

4 files changed

Lines changed: 45 additions & 41 deletions

File tree

api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import (
77
"sort"
88
)
99

10+
func getRateLimit(token string) (data map[string]interface{}, err error) {
11+
err = NativeGet("rate_limit", token, &data)
12+
return data, err
13+
}
14+
1015
func GetUser(username string) (data map[string]interface{}, err error) {
1116
err = Get(fmt.Sprintf("users/%s", username), &data)
1217
if err == nil && data["message"] == "Not Found" {

main.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ func main() {
5454
logger.Fatalf("Failed to unmarshal config file: %v", err)
5555
}
5656

57-
tokenList = GetTokenFromEnv()
58-
ValidateToken()
57+
tokenList = DetectToken()
5958
SetupCache()
6059

6160
app := iris.Default()
@@ -66,3 +65,21 @@ func main() {
6665
}
6766
app.Listen(fmt.Sprintf(":%d", conf.Server.Port))
6867
}
68+
69+
func UserAPI(ctx iris.Context) {
70+
username := ctx.Params().Get("username")
71+
data := AnalysisUser(username)
72+
EndBodyWithCache(ctx, data)
73+
}
74+
75+
func RepoAPI(ctx iris.Context) {
76+
username, repo := ctx.Params().Get("username"), ctx.Params().Get("repo")
77+
data := AnalysisRepo(username, repo)
78+
EndBodyWithCache(ctx, data)
79+
}
80+
81+
func ContributorAPI(ctx iris.Context) {
82+
username, repo := ctx.Params().Get("username"), ctx.Params().Get("repo")
83+
data := AnalysisContributor(username, repo)
84+
EndBodyWithCache(ctx, data)
85+
}

server.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

utils.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,28 @@ var conf Config
3535

3636
var tokenList []string
3737

38-
func GetToken() string {
39-
source := rand.NewSource(time.Now().UnixNano())
40-
idx := rand.New(source).Intn(len(tokenList))
41-
return tokenList[idx]
42-
}
43-
44-
func GetTokenFromEnv() []string {
38+
func DetectToken() []string {
4539
data := strings.TrimSpace(os.Getenv("TOKEN"))
46-
tokenList = strings.Split(data, "|")
40+
arr := strings.Split(data, "|")
4741
result := make([]string, 0)
48-
for _, token := range tokenList {
42+
for _, token := range arr {
4943
if strings.HasPrefix(token, "ghp_") {
50-
result = append(result, token)
44+
data, err := getRateLimit(token)
45+
if err == nil {
46+
remaining := data["rate"].(map[string]interface{})["remaining"].(float64)
47+
if remaining > 0 {
48+
result = append(result, token)
49+
fmt.Printf("> Detected available token \u001B[33m%s\u001B[0m has \u001B[32m%d\u001B[0m remaining requests.\n", token[:10], int(remaining))
50+
}
51+
}
5152
}
5253
}
53-
return result
54-
}
5554

56-
func ValidateToken() {
57-
if len(tokenList) == 0 {
55+
if len(result) == 0 {
5856
logger.Fatal("No token found! Please set TOKEN environment variable.")
5957
}
6058
logger.Debug(fmt.Sprintf("Found %d available token(s)", len(tokenList)))
59+
return result
6160
}
6261

6362
func GetColor(lang any) string {
@@ -117,14 +116,14 @@ func ThrowError(ctx iris.Context, message string, code int) {
117116
})
118117
}
119118

120-
func Get(uri string, ptr interface{}) (err error) {
119+
func NativeGet(uri string, token string, ptr interface{}) (err error) {
121120
req, err := http.NewRequest("GET", "https://api.github.com/"+uri, nil)
122121
if err != nil {
123122
return err
124123
}
125124

126125
req.Header.Set("Accept", "application/json")
127-
req.Header.Set("Authorization", "Bearer "+GetToken())
126+
req.Header.Set("Authorization", "Bearer "+token)
128127

129128
client := &http.Client{}
130129
resp, err := client.Do(req)
@@ -144,6 +143,12 @@ func Get(uri string, ptr interface{}) (err error) {
144143
return nil
145144
}
146145

146+
func Get(uri string, ptr interface{}) (err error) {
147+
source := rand.NewSource(time.Now().UnixNano())
148+
idx := rand.New(source).Intn(len(tokenList))
149+
return NativeGet(uri, tokenList[idx], ptr)
150+
}
151+
147152
func getDefault(value any, defaults any) any {
148153
if value == nil || value == "" || value == 0 || value == false {
149154
return defaults

0 commit comments

Comments
 (0)