11from time import sleep
2- from interface_wrapper import IRepositoryAPI , RepositoryFactory
2+ from interface_wrapper import IRepositoryAPI , RepositoryFactory , IClients
33
44TIMEDELTA = 0.05
55TIMEZONE = 'Europe/Moscow'
66
77
88def login (token ):
9- try :
9+ if 1 :
1010 client = RepositoryFactory .create_api ("github" , token )
11- except Exception as err :
12- print (f'Github: Connect: error { err } ' )
13- print ('Github: Connect: user could not be authenticated please try again.' )
14- exit (1 )
15- else :
11+ # except Exception as err:
12+ # print(f'Github: Connect: error {err}')
13+ # print('Github: Connect: user could not be authenticated please try again.')
14+ # exit(1)
15+ # else:
1616 return client
1717
1818
@@ -23,10 +23,11 @@ def get_tokens_from_file(tokens_path: str) -> list[str]:
2323 return tokens
2424
2525
26- class GithubClients :
26+ class GithubClients ( IClients ) :
2727 def __init__ (self , tokens : list [str ]):
2828 self .clients = self ._init_clients (tokens )
2929 self .cur_client = None
30+ self .last_client = - 1
3031
3132 def _init_clients (self , tokens : list [str ]) -> list [dict ]:
3233 clients = [{"client" : login (token ), "token" : token } for token in tokens ]
@@ -35,44 +36,30 @@ def _init_clients(self, tokens: list[str]) -> list[dict]:
3536 return clients
3637
3738 def get_next_client (self ):
38- client = None
39- max_remaining_limit = - 1
40-
41- for client_tmp in self .clients :
42- remaining_limit , limit = client_tmp ["client" ].rate_limiting
43-
44- # можно добавить вывод износа токена
45- # можно дополнительно проверять на 403 и временно пропускать эти токены,
46- # либо завести константу "минимальный коэффициент износа" и не трогать "изношенные" токены
47-
48- if remaining_limit > max_remaining_limit :
49- client = client_tmp
50- max_remaining_limit = remaining_limit
51-
52- sleep (TIMEDELTA )
53-
54- if client is None :
39+ if not self .clients :
5540 raise Exception ("No github-clients available" )
5641
57- self .cur_client = client
58- return client
42+ self .last_client = (self .last_client + 1 ) % len (self .clients )
43+ self .cur_client = self .clients [self .last_client ]
44+ return self .cur_client
45+
5946
6047
61- def get_next_repo (client : IRepositoryAPI , repositories ):
48+ def get_next_repo (clients : IClients , repositories ):
6249 with open (repositories , 'r' ) as file :
6350 list_repos = [x for x in file .read ().split ('\n ' ) if x ]
6451 print (list_repos )
6552 for repo_name in list_repos :
66- try :
53+ if 1 :
6754 cur_client = clients .get_next_client ()
6855 repo = cur_client ['client' ].get_repository (repo_name )
6956 if not repo :
7057 raise Exception (f"Repository { repo_name } not found." )
71- except Exception as err :
72- print (f'Github: Connect: error { err } ' )
73- print (f'Github: Connect: failed to load repository "{ repo_name } "' )
74- exit (1 )
75- else :
58+ # except Exception as err:
59+ # print(f'Github: Connect: error {err}')
60+ # print(f'Github: Connect: failed to load repository "{repo_name}"')
61+ # exit(1)
62+ # else:
7663 yield repo
7764
7865
0 commit comments