feat: move authentication inside Auditui class
This commit is contained in:
23
main.py
23
main.py
@@ -683,9 +683,8 @@ class Auditui(App):
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def authenticate():
|
||||
"""Authenticate with Audible and return auth and client objects."""
|
||||
def authenticate(self) -> None:
|
||||
"""Authenticate with Audible and set auth and client objects."""
|
||||
auth_path = Path.home() / ".config" / "auditui" / "auth.json"
|
||||
auth_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -693,7 +692,9 @@ def authenticate():
|
||||
try:
|
||||
authenticator = audible.Authenticator.from_file(str(auth_path))
|
||||
audible_client = audible.Client(auth=authenticator)
|
||||
return authenticator, audible_client
|
||||
self.auth = authenticator
|
||||
self.client = audible_client
|
||||
return
|
||||
except (OSError, ValueError, KeyError) as e:
|
||||
print(f"Failed to load existing auth: {e}")
|
||||
print("Please re-authenticate.")
|
||||
@@ -706,9 +707,10 @@ def authenticate():
|
||||
|
||||
email = input("\nEmail: ")
|
||||
password = getpass("Password: ")
|
||||
marketplace = input("Marketplace locale (default: US): ").strip().upper() or "US"
|
||||
marketplace = (
|
||||
input("Marketplace locale (default: US): ").strip().upper() or "US"
|
||||
)
|
||||
|
||||
try:
|
||||
authenticator = audible.Authenticator.from_login(
|
||||
username=email, password=password, locale=marketplace
|
||||
)
|
||||
@@ -717,14 +719,11 @@ def authenticate():
|
||||
authenticator.to_file(str(auth_path))
|
||||
print("Authentication successful!")
|
||||
audible_client = audible.Client(auth=authenticator)
|
||||
return authenticator, audible_client
|
||||
|
||||
except (OSError, ValueError, KeyError) as e:
|
||||
print(f"Authentication failed: {e}")
|
||||
sys.exit(1)
|
||||
self.auth = authenticator
|
||||
self.client = audible_client
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = Auditui()
|
||||
app.auth, app.client = authenticate()
|
||||
app.authenticate()
|
||||
app.run()
|
||||
|
||||
Reference in New Issue
Block a user