feat: move authentication inside Auditui class

This commit is contained in:
2025-12-06 16:04:09 +01:00
parent 1088517cd5
commit cc3a1c6818

23
main.py
View File

@@ -683,9 +683,8 @@ class Auditui(App):
except Exception: except Exception:
return None return None
def authenticate(self) -> None:
def authenticate(): """Authenticate with Audible and set auth and client objects."""
"""Authenticate with Audible and return auth and client objects."""
auth_path = Path.home() / ".config" / "auditui" / "auth.json" auth_path = Path.home() / ".config" / "auditui" / "auth.json"
auth_path.parent.mkdir(parents=True, exist_ok=True) auth_path.parent.mkdir(parents=True, exist_ok=True)
@@ -693,7 +692,9 @@ def authenticate():
try: try:
authenticator = audible.Authenticator.from_file(str(auth_path)) authenticator = audible.Authenticator.from_file(str(auth_path))
audible_client = audible.Client(auth=authenticator) audible_client = audible.Client(auth=authenticator)
return authenticator, audible_client self.auth = authenticator
self.client = audible_client
return
except (OSError, ValueError, KeyError) as e: except (OSError, ValueError, KeyError) as e:
print(f"Failed to load existing auth: {e}") print(f"Failed to load existing auth: {e}")
print("Please re-authenticate.") print("Please re-authenticate.")
@@ -706,9 +707,10 @@ def authenticate():
email = input("\nEmail: ") email = input("\nEmail: ")
password = getpass("Password: ") 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( authenticator = audible.Authenticator.from_login(
username=email, password=password, locale=marketplace username=email, password=password, locale=marketplace
) )
@@ -717,14 +719,11 @@ def authenticate():
authenticator.to_file(str(auth_path)) authenticator.to_file(str(auth_path))
print("Authentication successful!") print("Authentication successful!")
audible_client = audible.Client(auth=authenticator) audible_client = audible.Client(auth=authenticator)
return authenticator, audible_client self.auth = authenticator
self.client = audible_client
except (OSError, ValueError, KeyError) as e:
print(f"Authentication failed: {e}")
sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":
app = Auditui() app = Auditui()
app.auth, app.client = authenticate() app.authenticate()
app.run() app.run()