feat: move authentication inside Auditui class

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

55
main.py
View File

@@ -683,32 +683,34 @@ class Auditui(App):
except Exception: except Exception:
return None return None
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)
def authenticate(): if auth_path.exists():
"""Authenticate with Audible and return auth and client objects.""" try:
auth_path = Path.home() / ".config" / "auditui" / "auth.json" authenticator = audible.Authenticator.from_file(str(auth_path))
auth_path.parent.mkdir(parents=True, exist_ok=True) audible_client = audible.Client(auth=authenticator)
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.")
if auth_path.exists(): print("Please authenticate with your Audible account.")
try: print("You will need to provide:")
authenticator = audible.Authenticator.from_file(str(auth_path)) print(" - Your Audible email/username")
audible_client = audible.Client(auth=authenticator) print(" - Your password")
return authenticator, audible_client print(" - Your marketplace locale (e.g., 'US', 'UK', 'DE', 'FR')")
except (OSError, ValueError, KeyError) as e:
print(f"Failed to load existing auth: {e}")
print("Please re-authenticate.")
print("Please authenticate with your Audible account.") email = input("\nEmail: ")
print("You will need to provide:") password = getpass("Password: ")
print(" - Your Audible email/username") marketplace = (
print(" - Your password") input("Marketplace locale (default: US): ").strip().upper() or "US"
print(" - Your marketplace locale (e.g., 'US', 'UK', 'DE', 'FR')") )
email = input("\nEmail: ")
password = getpass("Password: ")
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()