1010TIMEZONE = 'Europe/Moscow'
1111
1212
13- def login (source , token , base_url ):
14- client = RepositoryFactory .create_api (source , token , base_url )
15- return client
13+ def login (token , base_url ):
14+ try :
15+ client = RepositoryFactory .create_api (token , base_url )
16+ return client
17+ except Exception :
18+ return None
1619
1720
1821def get_tokens_from_file (tokens_path : str ) -> list [str ]:
@@ -30,50 +33,36 @@ def get_repos_from_file(repos_path: str) -> list[str]:
3033
3134
3235class Clients :
33- def __init__ (self , source : str , tokens : list [str ], base_url : str | None = None ):
34- self .clients = self . _init_clients ( source , tokens , base_url )
35- self .cur_client = None
36+ def __init__ (self , tokens : list [str ], base_url : str | None = None ):
37+ self .clients = []
38+ self .token_map = {}
3639
37- def _init_clients (self , source : str , tokens : list [str ], base_url : str | None ) -> list [dict ]:
38- clients = [{"client" : login (source , token , base_url ), "token" : token } for token in tokens ]
39- return clients
40+ for token in tokens :
41+ client = login (token , base_url )
42+ if client :
43+ self .clients .append (client )
44+ self .token_map [client ] = token
4045
41- def get_next_client (self ) -> IRepositoryAPI :
46+ if not self .clients :
47+ raise Exception ("No valid tokens for either GitHub or Forgejo" )
48+
49+ def _get_next_client (self ) -> tuple [IRepositoryAPI , str ]:
4250 client = None
4351 max_remaining_limit = - 1
4452
45- for client_tmp in self .clients :
46- remaining_limit , limit = client_tmp ["client" ].get_rate_limiting ()
47-
48- # можно добавить вывод износа токена
49- # можно дополнительно проверять на 403 и временно пропускать эти токены,
50- # либо завести константу "минимальный коэффициент износа" и не трогать "изношенные" токены
51-
52- if remaining_limit > max_remaining_limit :
53- client = client_tmp
54- max_remaining_limit = remaining_limit
55-
53+ for c in self .clients :
54+ remaining , _ = c .get_rate_limiting ()
55+ if remaining > max_remaining_limit :
56+ client = c
57+ max_remaining_limit = remaining
5658 sleep (TIMEDELTA )
5759
5860 if client is None :
5961 raise Exception ("No git clients available" )
62+ return client , self .token_map [client ]
6063
61- self .cur_client = client
62- return client
63-
64-
65- def get_next_repo (clients : Clients , repositories ):
66- with open (repositories , 'r' ) as file :
67- list_repos = [x for x in file .read ().split ('\n ' ) if x ]
68- for repo_name in list_repos :
69- try :
70- cur_client = clients .get_next_client ()
71- repo = cur_client ['client' ].get_repository (repo_name )
72- except Exception as err :
73- print (f'get_next_repo(): error { err } ' )
74- print (f'get_next_repo(): failed to load repository "{ repo_name } "' )
75- else :
76- yield cur_client ['client' ], repo , cur_client ['token' ]
64+ def get_next_client (self ) -> tuple [IRepositoryAPI , str ]:
65+ return self ._get_next_client ()
7766
7867
7968def get_next_binded_repo (clients : Clients , repositories : list [str ]):
0 commit comments