Refactor device retrieval logic in get_device()

This commit is contained in:
2025-12-31 11:48:42 +01:00
committed by GitHub
parent db7aa503b3
commit 653e5fb163

View File

@@ -148,69 +148,44 @@ def get_device():
jd = Myjdapi() jd = Myjdapi()
jd.connect(MYJD_EMAIL, MYJD_PASSWORD) jd.connect(MYJD_EMAIL, MYJD_PASSWORD)
# Try to retrieve device list across myjdapi versions # list_devices() is reliable in your myjdapi version
devices = None devices = jd.list_devices()
# v1: update_devices() + attribute
try:
jd.update_devices()
devices = getattr(jd, "devices", None)
except Exception:
devices = None
# v2: list_devices() returns a list
if devices is None:
try:
devices = jd.list_devices()
except Exception:
devices = None
# v3: get_devices() / getDevices()
if devices is None:
for fn in ("get_devices", "getDevices"):
try:
devices = getattr(jd, fn)()
break
except Exception:
continue
if not devices: if not devices:
raise RuntimeError("No MyJDownloader devices available (JD online? correct credentials?)") raise RuntimeError("No MyJDownloader devices available (JD online/logged in?)")
# devices may be list[str] or list[dict] # devices is typically a list of dicts; accept strings too
def dev_name(d): def dev_name(d):
if isinstance(d, str): if isinstance(d, str):
return d return d
if isinstance(d, dict): if isinstance(d, dict):
return d.get("name") or d.get("deviceName") or d.get("id") or "" return (d.get("name") or d.get("deviceName") or "").strip()
return "" return ""
names = [dev_name(d).strip() for d in devices] names = [dev_name(d) for d in devices]
names = [n for n in names if n] names = [n for n in names if n]
if not names: if not names:
raise RuntimeError(f"MyJDownloader returned devices but no names: {devices}") raise RuntimeError(f"MyJDownloader returned devices but no usable names: {devices}")
wanted = (MYJD_DEVICE or "").strip() wanted = (MYJD_DEVICE or "").strip()
if wanted: if wanted:
# exact match first
for n in names: for n in names:
if n == wanted: if n == wanted:
return jd.get_device(n) return jd.get_device(n)
# case-insensitive fallback
for n in names: for n in names:
if n.lower() == wanted.lower(): if n.lower() == wanted.lower():
return jd.get_device(n) return jd.get_device(n)
raise RuntimeError(f"MYJD_DEVICE not found. Wanted '{wanted}', available: {names}")
# Prefer "jdownloader" looking device # Prefer JDownloader-ish name
for n in names: for n in names:
nl = n.lower() nl = n.lower()
if "jdownloader" in nl or nl in {"jd", "jd2"}: if "jdownloader" in nl or nl in {"jd", "jd2"}:
return jd.get_device(n) return jd.get_device(n)
# Otherwise first
return jd.get_device(names[0]) return jd.get_device(names[0])
def is_video_file(path: str) -> bool: def is_video_file(path: str) -> bool:
name = os.path.basename(path).lower() name = os.path.basename(path).lower()
_, ext = os.path.splitext(name) _, ext = os.path.splitext(name)