diff --git a/examples/fetch_reports.py b/examples/fetch_reports.py index d4fb28a..6dbb587 100644 --- a/examples/fetch_reports.py +++ b/examples/fetch_reports.py @@ -1,4 +1,5 @@ import logging +import sys from _login import get_account_sync @@ -8,30 +9,30 @@ from findmy.reports import RemoteAnisetteProvider # URL to (public or local) anisette server ANISETTE_SERVER = "http://localhost:6969" -# Private base64-encoded key to look up -KEY_PRIV = "" - -# Optional, to verify that advertisement key derivation works for your key -KEY_ADV = "" - logging.basicConfig(level=logging.DEBUG) -def fetch_reports(lookup_key: KeyPair) -> None: - anisette = RemoteAnisetteProvider(ANISETTE_SERVER) - acc = get_account_sync(anisette) +def fetch_reports(priv_key: str) -> int: + key = KeyPair.from_b64(priv_key) + acc = get_account_sync( + RemoteAnisetteProvider(ANISETTE_SERVER), + ) print(f"Logged in as: {acc.account_name} ({acc.first_name} {acc.last_name})") # It's that simple! - reports = acc.fetch_last_reports(lookup_key) + reports = acc.fetch_last_reports(key) for report in sorted(reports): print(report) + return 1 + if __name__ == "__main__": - key = KeyPair.from_b64(KEY_PRIV) - if KEY_ADV: # verify that your adv key is correct :D - assert key.adv_key_b64 == KEY_ADV + if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + print(file=sys.stderr) + print("The private key should be base64-encoded.", file=sys.stderr) + sys.exit(1) - fetch_reports(key) + sys.exit(fetch_reports(sys.argv[1])) diff --git a/examples/fetch_reports_async.py b/examples/fetch_reports_async.py index 1e73b4e..2e41249 100644 --- a/examples/fetch_reports_async.py +++ b/examples/fetch_reports_async.py @@ -1,5 +1,6 @@ import asyncio import logging +import sys from _login import get_account_async @@ -9,35 +10,33 @@ from findmy.reports import RemoteAnisetteProvider # URL to (public or local) anisette server ANISETTE_SERVER = "http://localhost:6969" -# Private base64-encoded key to look up -KEY_PRIV = "" - -# Optional, to verify that advertisement key derivation works for your key -KEY_ADV = "" - logging.basicConfig(level=logging.DEBUG) -async def fetch_reports(lookup_key: KeyPair) -> None: - anisette = RemoteAnisetteProvider(ANISETTE_SERVER) - - acc = await get_account_async(anisette) +async def fetch_reports(priv_key: str) -> int: + key = KeyPair.from_b64(priv_key) + acc = await get_account_async( + RemoteAnisetteProvider(ANISETTE_SERVER), + ) try: print(f"Logged in as: {acc.account_name} ({acc.first_name} {acc.last_name})") # It's that simple! - reports = await acc.fetch_last_reports(lookup_key) + reports = await acc.fetch_last_reports(key) for report in sorted(reports): print(report) - finally: await acc.close() + return 0 + if __name__ == "__main__": - key = KeyPair.from_b64(KEY_PRIV) - if KEY_ADV: # verify that your adv key is correct :D - assert key.adv_key_b64 == KEY_ADV + if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + print(file=sys.stderr) + print("The private key should be base64-encoded.", file=sys.stderr) + sys.exit(1) - asyncio.run(fetch_reports(key)) + asyncio.run(fetch_reports(sys.argv[1])) diff --git a/examples/real_airtag.py b/examples/real_airtag.py index fef44f6..51abca8 100644 --- a/examples/real_airtag.py +++ b/examples/real_airtag.py @@ -3,6 +3,7 @@ Example showing how to fetch locations of an AirTag, or any other FindMy accesso """ from __future__ import annotations +import sys from pathlib import Path from _login import get_account_sync @@ -13,13 +14,10 @@ from findmy.reports import RemoteAnisetteProvider # URL to (public or local) anisette server ANISETTE_SERVER = "http://localhost:6969" -# Path to a .plist dumped from the Find My app. -PLIST_PATH = Path("airtag.plist") - -def main() -> None: +def main(plist_path: str) -> int: # Step 0: create an accessory key generator - with PLIST_PATH.open("rb") as f: + with Path(plist_path).open("rb") as f: airtag = FindMyAccessory.from_plist(f) # Step 1: log into an Apple account @@ -37,6 +35,14 @@ def main() -> None: for report in reports: print(f" - {report}") + return 0 + if __name__ == "__main__": - main() + if len(sys.argv) < 2: + print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + print(file=sys.stderr) + print("The plist file should be dumped from MacOS's FindMy app.", file=sys.stderr) + sys.exit(1) + + sys.exit(main(sys.argv[1]))