зеркало из
				https://github.com/viginum-datalab/twscrape.git
				synced 2025-10-31 14:06:12 +02:00 
			
		
		
		
	feat: ability to remove account; show stats for login_all method in cli
Этот коммит содержится в:
		
							родитель
							
								
									308ba9d196
								
							
						
					
					
						Коммит
						6bceb0a6bf
					
				| @ -87,6 +87,16 @@ class AccountsPool: | |||||||
|         ) |         ) | ||||||
|         await self.save(account) |         await self.save(account) | ||||||
| 
 | 
 | ||||||
|  |     async def delete_accounts(self, usernames: str | list[str]): | ||||||
|  |         usernames = usernames if isinstance(usernames, list) else [usernames] | ||||||
|  |         usernames = list(set(usernames)) | ||||||
|  |         if not usernames: | ||||||
|  |             logger.warning("No usernames provided") | ||||||
|  |             return | ||||||
|  | 
 | ||||||
|  |         qs = f"""DELETE FROM accounts WHERE username IN ({','.join([f'"{x}"' for x in usernames])})""" | ||||||
|  |         await execute(self._db_file, qs) | ||||||
|  | 
 | ||||||
|     async def get(self, username: str): |     async def get(self, username: str): | ||||||
|         qs = "SELECT * FROM accounts WHERE username = :username" |         qs = "SELECT * FROM accounts WHERE username = :username" | ||||||
|         rs = await fetchone(self._db_file, qs, {"username": username}) |         rs = await fetchone(self._db_file, qs, {"username": username}) | ||||||
| @ -113,8 +123,10 @@ class AccountsPool: | |||||||
|         try: |         try: | ||||||
|             await login(account) |             await login(account) | ||||||
|             logger.info(f"Logged in to {account.username} successfully") |             logger.info(f"Logged in to {account.username} successfully") | ||||||
|  |             return True | ||||||
|         except Exception as e: |         except Exception as e: | ||||||
|             logger.error(f"Error logging in to {account.username}: {e}") |             logger.error(f"Error logging in to {account.username}: {e}") | ||||||
|  |             return False | ||||||
|         finally: |         finally: | ||||||
|             await self.save(account) |             await self.save(account) | ||||||
| 
 | 
 | ||||||
| @ -125,9 +137,12 @@ class AccountsPool: | |||||||
|         accounts = [Account.from_rs(rs) for rs in rs] |         accounts = [Account.from_rs(rs) for rs in rs] | ||||||
|         # await asyncio.gather(*[login(x) for x in self.accounts]) |         # await asyncio.gather(*[login(x) for x in self.accounts]) | ||||||
| 
 | 
 | ||||||
|  |         counter = {"total": len(accounts), "success": 0, "failed": 0} | ||||||
|         for i, x in enumerate(accounts, start=1): |         for i, x in enumerate(accounts, start=1): | ||||||
|             logger.info(f"[{i}/{len(accounts)}] Logging in {x.username} - {x.email}") |             logger.info(f"[{i}/{len(accounts)}] Logging in {x.username} - {x.email}") | ||||||
|             await self.login(x) |             status = await self.login(x) | ||||||
|  |             counter["success" if status else "failed"] += 1 | ||||||
|  |         return counter | ||||||
| 
 | 
 | ||||||
|     async def relogin(self, usernames: str | list[str]): |     async def relogin(self, usernames: str | list[str]): | ||||||
|         usernames = usernames if isinstance(usernames, list) else [usernames] |         usernames = usernames if isinstance(usernames, list) else [usernames] | ||||||
|  | |||||||
| @ -77,7 +77,7 @@ async def main(args): | |||||||
|         return |         return | ||||||
| 
 | 
 | ||||||
|     if args.command == "login_accounts": |     if args.command == "login_accounts": | ||||||
|         await pool.login_all() |         print(await pool.login_all()) | ||||||
|         return |         return | ||||||
| 
 | 
 | ||||||
|     if args.command == "relogin_failed": |     if args.command == "relogin_failed": | ||||||
| @ -143,14 +143,20 @@ def run(): | |||||||
|         return p |         return p | ||||||
| 
 | 
 | ||||||
|     subparsers.add_parser("version", help="Show version") |     subparsers.add_parser("version", help="Show version") | ||||||
| 
 |  | ||||||
|     subparsers.add_parser("accounts", help="List all accounts") |     subparsers.add_parser("accounts", help="List all accounts") | ||||||
|  | 
 | ||||||
|     add_accounts = subparsers.add_parser("add_accounts", help="Add accounts") |     add_accounts = subparsers.add_parser("add_accounts", help="Add accounts") | ||||||
|     add_accounts.add_argument("file_path", help="File with accounts") |     add_accounts.add_argument("file_path", help="File with accounts") | ||||||
|     add_accounts.add_argument("line_format", help="args of Pool.add_account splited by same delim") |     add_accounts.add_argument("line_format", help="args of Pool.add_account splited by same delim") | ||||||
|  | 
 | ||||||
|  |     del_accounts = subparsers.add_parser("del_accounts", help="Delete accounts") | ||||||
|  |     del_accounts.add_argument("usernames", nargs="+", default=[], help="Usernames to delete") | ||||||
|  | 
 | ||||||
|     subparsers.add_parser("login_accounts", help="Login accounts") |     subparsers.add_parser("login_accounts", help="Login accounts") | ||||||
|  | 
 | ||||||
|     relogin = subparsers.add_parser("relogin", help="Re-login selected accounts") |     relogin = subparsers.add_parser("relogin", help="Re-login selected accounts") | ||||||
|     relogin.add_argument("usernames", nargs="+", default=[], help="Usernames to re-login") |     relogin.add_argument("usernames", nargs="+", default=[], help="Usernames to re-login") | ||||||
|  | 
 | ||||||
|     subparsers.add_parser("relogin_failed", help="Retry login for failed accounts") |     subparsers.add_parser("relogin_failed", help="Retry login for failed accounts") | ||||||
|     subparsers.add_parser("stats", help="Get current usage stats") |     subparsers.add_parser("stats", help="Get current usage stats") | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Загрузка…
	
	
			
			x
			
			
		
	
		Ссылка в новой задаче
	
	Block a user
	 Vlad Pronsky
						Vlad Pronsky