@@ -35,29 +35,28 @@ var conf Config
3535
3636var 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
6362func 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+
147152func getDefault (value any , defaults any ) any {
148153 if value == nil || value == "" || value == 0 || value == false {
149154 return defaults
0 commit comments