refactor: switch to a class approach

This commit is contained in:
2025-11-17 17:17:12 +01:00
parent a33e2997aa
commit 9b317e20cb

31
main.py
View File

@@ -11,15 +11,19 @@ except ImportError:
sys.exit(1) sys.exit(1)
def login_to_audible(): class Auditui:
def __init__(self):
self.auth = None
def login_to_audible(self):
auth_file = Path.home() / ".config" / "auditui" / "auth.json" auth_file = Path.home() / ".config" / "auditui" / "auth.json"
auth_file.parent.mkdir(parents=True, exist_ok=True) auth_file.parent.mkdir(parents=True, exist_ok=True)
if auth_file.exists(): if auth_file.exists():
try: try:
auth = audible.Authenticator.from_file(str(auth_file)) self.auth = audible.Authenticator.from_file(str(auth_file))
print("Loaded existing authentication.") print("Loaded existing authentication.")
return auth return
except Exception as e: except Exception 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.")
@@ -36,24 +40,23 @@ def login_to_audible():
"Marketplace locale (default: US): ").strip().upper() or "US" "Marketplace locale (default: US): ").strip().upper() or "US"
try: try:
auth = audible.Authenticator.from_login( self.auth = audible.Authenticator.from_login(
username=email, username=email,
password=password, password=password,
locale=marketplace locale=marketplace
) )
auth_file.parent.mkdir(parents=True, exist_ok=True) auth_file.parent.mkdir(parents=True, exist_ok=True)
auth.to_file(str(auth_file)) self.auth.to_file(str(auth_file))
print("Authentication successful! Credentials saved.") print("Authentication successful! Credentials saved.")
return auth
except Exception as e: except Exception as e:
print(f"Authentication failed: {e}") print(f"Authentication failed: {e}")
import traceback import traceback
traceback.print_exc() traceback.print_exc()
sys.exit(1) sys.exit(1)
@staticmethod
def format_runtime(minutes): def format_runtime(minutes):
if minutes is None or minutes == 0: if minutes is None or minutes == 0:
return "Unknown length" return "Unknown length"
@@ -67,9 +70,8 @@ def format_runtime(minutes):
return f"{hours} hour{'s' if hours != 1 else ''}" return f"{hours} hour{'s' if hours != 1 else ''}"
return f"{hours} hour{'s' if hours != 1 else ''} {mins} minute{'s' if mins != 1 else ''}" return f"{hours} hour{'s' if hours != 1 else ''} {mins} minute{'s' if mins != 1 else ''}"
def list_library(self):
def list_library(auth): client = audible.Client(auth=self.auth)
client = audible.Client(auth=auth)
try: try:
print("\nFetching your library...") print("\nFetching your library...")
@@ -147,7 +149,7 @@ def list_library(auth):
elif isinstance(runtime, (int, float)): elif isinstance(runtime, (int, float)):
minutes = int(runtime) minutes = int(runtime)
runtime_str = format_runtime(minutes) runtime_str = self.format_runtime(minutes)
asin = product.get("asin") or item.get("asin", "") asin = product.get("asin") or item.get("asin", "")
@@ -170,8 +172,9 @@ def list_library(auth):
def main(): def main():
auth = login_to_audible() client = Auditui()
list_library(auth) client.login_to_audible()
client.list_library()
if __name__ == "__main__": if __name__ == "__main__":