Compare commits

...

3 Commits

3 changed files with 80 additions and 28 deletions

View File

@@ -1,6 +1,5 @@
"""Authentication helpers for the Auditui app.""" """Authentication helpers for the Auditui app."""
from getpass import getpass
from pathlib import Path from pathlib import Path
from typing import Tuple from typing import Tuple
@@ -13,30 +12,14 @@ def authenticate(
auth_path: Path = AUTH_PATH, auth_path: Path = AUTH_PATH,
) -> Tuple[audible.Authenticator, audible.Client]: ) -> Tuple[audible.Authenticator, audible.Client]:
"""Authenticate with Audible and return authenticator and client.""" """Authenticate with Audible and return authenticator and client."""
auth_path.parent.mkdir(parents=True, exist_ok=True) if not auth_path.exists():
raise FileNotFoundError(
"Authentication file not found. Please run 'auditui configure' to set up authentication.")
if auth_path.exists(): 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
return authenticator, audible_client except (OSError, ValueError, KeyError) as exc:
except (OSError, ValueError, KeyError) as exc: raise ValueError(
print(f"Failed to load existing auth: {exc}") f"Failed to load existing authentication: {exc}") from exc
print("Please re-authenticate.")
print("Please authenticate with your Audible account.")
email = input("\nEmail: ")
password = getpass("Password: ")
marketplace = input(
"Marketplace locale (default: US): ").strip().upper() or "US"
authenticator = audible.Authenticator.from_login(
username=email, password=password, locale=marketplace
)
auth_path.parent.mkdir(parents=True, exist_ok=True)
authenticator.to_file(str(auth_path))
print("Authentication successful!")
audible_client = audible.Client(auth=authenticator)
return authenticator, audible_client

40
auditui/configure.py Normal file
View File

@@ -0,0 +1,40 @@
"""Configuration helpers for the Auditui app."""
from getpass import getpass
from pathlib import Path
from typing import Tuple
import audible
from .constants import AUTH_PATH
def configure(
auth_path: Path = AUTH_PATH,
) -> Tuple[audible.Authenticator, audible.Client]:
"""Force re-authentication and save credentials."""
if auth_path.exists():
response = input(
"Configuration already exists. Are you sure you want to overwrite it? (y/N): "
).strip().lower()
if response not in ("yes", "y"):
print("Configuration cancelled.")
raise SystemExit(0)
print("Please authenticate with your Audible account.")
email = input("\nEmail: ")
password = getpass("Password: ")
marketplace = input(
"Marketplace locale (default: US): ").strip().upper() or "US"
authenticator = audible.Authenticator.from_login(
username=email, password=password, locale=marketplace
)
auth_path.parent.mkdir(parents=True, exist_ok=True)
authenticator.to_file(str(auth_path))
print("Authentication successful!")
audible_client = audible.Client(auth=authenticator)
return authenticator, audible_client

31
main.py
View File

@@ -1,13 +1,42 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Auditui entrypoint.""" """Auditui entrypoint."""
import sys
from pathlib import Path
from auditui.app import Auditui from auditui.app import Auditui
from auditui.auth import authenticate from auditui.auth import authenticate
from auditui.configure import configure
from auditui.constants import AUTH_PATH
def main() -> None: def main() -> None:
"""Authenticate and launch the app.""" """Authenticate and launch the app."""
auth, client = authenticate() if len(sys.argv) > 1 and sys.argv[1] == "configure":
try:
configure()
print("Configuration completed successfully.")
except Exception as exc:
print(f"Configuration error: {exc}")
sys.exit(1)
return
config_dir = AUTH_PATH.parent
if not config_dir.exists():
print("No configuration yet, please run 'auditui configure' to create it")
sys.exit(1)
try:
auth, client = authenticate()
except Exception as exc:
print(f"Authentication error: {exc}")
if not AUTH_PATH.exists():
print("No configuration yet, please run 'auditui configure' to create it")
else:
print("Please re-authenticate by running 'auditui configure'")
sys.exit(1)
app = Auditui(auth=auth, client=client) app = Auditui(auth=auth, client=client)
app.run() app.run()