Skip to content

Commit

Permalink
Add better error handling for twitter login
Browse files Browse the repository at this point in the history
  • Loading branch information
Volara committed Aug 9, 2024
1 parent c8676c8 commit 46f73ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
19 changes: 12 additions & 7 deletions cli/auth/twitter/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ def get_active_account() -> T.Optional[Account]:
return None


def set_active_account() -> None:
def set_active_account() -> bool:
click.echo("Setting active twitter account...")
email = click.prompt("Enter your twitter email")
username = click.prompt("Enter your twitter username")
password = click.prompt("Enter your twitter password", hide_input=True)
_set_active_account(email, username, password)
try:
_set_active_account(email, username, password)
except Exception as e:
click.echo(click.style(str(e), fg="red"))
click.echo(
click.style(f"Failed to authenticate X account {username}.", fg="red"),
err=True,
)
return False
click.echo("Active twitter account successfully set.")
return True


def remove_active_account() -> None:
Expand All @@ -39,11 +48,7 @@ def remove_active_account() -> None:


def _set_active_account(email: str, username: str, password: str) -> None:
try:
account = Account(email=email, username=username, password=password)
except Exception:
click.echo("Failed to authenticate.")
return
account = Account(email=email, username=username, password=password)
os.makedirs(os.path.dirname(TMP_TWITTER_AUTH), exist_ok=True)
with open(TMP_TWITTER_AUTH, "w") as file:
file.write(json.dumps(dict(account.session.cookies)))
4 changes: 3 additions & 1 deletion cli/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ def start(background: bool):
click.echo("Checking twitter account credentials...")
if twitter_auth.get_active_account() is None:
click.echo("No active twitter account found. Requesting credentials...")
twitter_auth.set_active_account()
if not twitter_auth.set_active_account():
click.echo("Exiting...")
return
click.echo("Starting mining daemon...")
if background:
mining.start_daemon()
Expand Down

0 comments on commit 46f73ba

Please sign in to comment.