зеркало из
https://github.com/viginum-datalab/twscrape.git
synced 2025-10-29 21:16:25 +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)
|
||||
|
||||
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):
|
||||
qs = "SELECT * FROM accounts WHERE username = :username"
|
||||
rs = await fetchone(self._db_file, qs, {"username": username})
|
||||
@ -113,8 +123,10 @@ class AccountsPool:
|
||||
try:
|
||||
await login(account)
|
||||
logger.info(f"Logged in to {account.username} successfully")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error logging in to {account.username}: {e}")
|
||||
return False
|
||||
finally:
|
||||
await self.save(account)
|
||||
|
||||
@ -125,9 +137,12 @@ class AccountsPool:
|
||||
accounts = [Account.from_rs(rs) for rs in rs]
|
||||
# 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):
|
||||
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]):
|
||||
usernames = usernames if isinstance(usernames, list) else [usernames]
|
||||
|
||||
@ -77,7 +77,7 @@ async def main(args):
|
||||
return
|
||||
|
||||
if args.command == "login_accounts":
|
||||
await pool.login_all()
|
||||
print(await pool.login_all())
|
||||
return
|
||||
|
||||
if args.command == "relogin_failed":
|
||||
@ -143,14 +143,20 @@ def run():
|
||||
return p
|
||||
|
||||
subparsers.add_parser("version", help="Show version")
|
||||
|
||||
subparsers.add_parser("accounts", help="List all 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("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")
|
||||
|
||||
relogin = subparsers.add_parser("relogin", help="Re-login selected accounts")
|
||||
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("stats", help="Get current usage stats")
|
||||
|
||||
|
||||
Загрузка…
x
Ссылка в новой задаче
Block a user