From d4a5fcef10cc4593fb48535fc2a5595b7e5bb6c5 Mon Sep 17 00:00:00 2001 From: Mike A Date: Tue, 6 Feb 2024 23:26:16 +0100 Subject: [PATCH] Cleanup examples --- examples/device_scanner.py | 5 +++-- examples/fetch_reports.py | 24 ++++++++++++------------ examples/fetch_reports_async.py | 25 ++++++++++++------------- examples/real_airtag.py | 15 ++++++++------- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/examples/device_scanner.py b/examples/device_scanner.py index e58f0da..f0275b4 100644 --- a/examples/device_scanner.py +++ b/examples/device_scanner.py @@ -1,3 +1,4 @@ +# ruff: noqa: T201, D103 import asyncio import logging @@ -6,7 +7,7 @@ from findmy.scanner import OfflineFindingScanner logging.basicConfig(level=logging.INFO) -async def scan(): +async def scan() -> None: scanner = await OfflineFindingScanner.create() print("Scanning for FindMy-devices...") @@ -18,7 +19,7 @@ async def scan(): print(f" Lookup key: {device.hashed_adv_key_b64}") print(f" Status byte: {device.status:x}") print(f" Hint byte: {device.hint:x}") - print(f" Extra data:") + print(" Extra data:") for k, v in sorted(device.additional_data.items()): print(f" {k:20}: {v}") print() diff --git a/examples/fetch_reports.py b/examples/fetch_reports.py index bee7789..bac7704 100644 --- a/examples/fetch_reports.py +++ b/examples/fetch_reports.py @@ -1,12 +1,13 @@ +# ruff: noqa: T201, D103, S101 import json import logging -import os +from pathlib import Path +from findmy import KeyPair from findmy.reports import ( AppleAccount, LoginState, RemoteAnisetteProvider, - keys, SmsSecondFactorMethod, ) @@ -15,7 +16,7 @@ ANISETTE_SERVER = "http://localhost:6969" # Apple account details ACCOUNT_EMAIL = "test@test.com" -ACCOUNT_PASS = "1234" +ACCOUNT_PASS = "" # Private base64-encoded key to look up KEY_PRIV = "" @@ -26,7 +27,7 @@ KEY_ADV = "" logging.basicConfig(level=logging.DEBUG) -def login(account: AppleAccount): +def login(account: AppleAccount) -> None: state = account.login(ACCOUNT_EMAIL, ACCOUNT_PASS) if state == LoginState.REQUIRE_2FA: # Account requires 2FA @@ -46,20 +47,19 @@ def login(account: AppleAccount): # This automatically finishes the post-2FA login flow method.submit(code) - return account - -def fetch_reports(lookup_key): +def fetch_reports(lookup_key: KeyPair) -> None: anisette = RemoteAnisetteProvider(ANISETTE_SERVER) acc = AppleAccount(anisette) # Save / restore account logic - if os.path.isfile("account.json"): - with open("account.json") as f: + acc_store = Path("account.json") + try: + with acc_store.open() as f: acc.restore(json.load(f)) - else: + except FileNotFoundError: login(acc) - with open("account.json", "w+") as f: + with acc_store.open("w+") as f: json.dump(acc.export(), f) print(f"Logged in as: {acc.account_name} ({acc.first_name} {acc.last_name})") @@ -70,7 +70,7 @@ def fetch_reports(lookup_key): if __name__ == "__main__": - key = keys.KeyPair.from_b64(KEY_PRIV) + key = KeyPair.from_b64(KEY_PRIV) if KEY_ADV: # verify that your adv key is correct :D assert key.adv_key_b64 == KEY_ADV diff --git a/examples/fetch_reports_async.py b/examples/fetch_reports_async.py index ed736bd..1c86420 100644 --- a/examples/fetch_reports_async.py +++ b/examples/fetch_reports_async.py @@ -1,14 +1,15 @@ +# ruff: noqa: T201, D103, S101 import asyncio import json import logging -import os +from pathlib import Path +from findmy import KeyPair from findmy.reports import ( AsyncAppleAccount, LoginState, RemoteAnisetteProvider, SmsSecondFactorMethod, - keys, ) # URL to (public or local) anisette server @@ -16,7 +17,7 @@ ANISETTE_SERVER = "http://localhost:6969" # Apple account details ACCOUNT_EMAIL = "test@test.com" -ACCOUNT_PASS = "1234" +ACCOUNT_PASS = "" # Private base64-encoded key to look up KEY_PRIV = "" @@ -27,7 +28,7 @@ KEY_ADV = "" logging.basicConfig(level=logging.DEBUG) -async def login(account: AsyncAppleAccount): +async def login(account: AsyncAppleAccount) -> None: state = await account.login(ACCOUNT_EMAIL, ACCOUNT_PASS) if state == LoginState.REQUIRE_2FA: # Account requires 2FA @@ -47,21 +48,19 @@ async def login(account: AsyncAppleAccount): # This automatically finishes the post-2FA login flow await method.submit(code) - return account - -async def fetch_reports(lookup_key): +async def fetch_reports(lookup_key: KeyPair) -> None: anisette = RemoteAnisetteProvider(ANISETTE_SERVER) acc = AsyncAppleAccount(anisette) try: - # Save / restore account logic - if os.path.isfile("account.json"): - with open("account.json") as f: + acc_store = Path("account.json") + try: + with acc_store.open() as f: acc.restore(json.load(f)) - else: + except FileNotFoundError: await login(acc) - with open("account.json", "w+") as f: + with acc_store.open("w+") as f: json.dump(acc.export(), f) print(f"Logged in as: {acc.account_name} ({acc.first_name} {acc.last_name})") @@ -75,7 +74,7 @@ async def fetch_reports(lookup_key): if __name__ == "__main__": - key = keys.KeyPair.from_b64(KEY_PRIV) + key = KeyPair.from_b64(KEY_PRIV) if KEY_ADV: # verify that your adv key is correct :D assert key.adv_key_b64 == KEY_ADV diff --git a/examples/real_airtag.py b/examples/real_airtag.py index abb8644..abc4c26 100644 --- a/examples/real_airtag.py +++ b/examples/real_airtag.py @@ -1,6 +1,7 @@ +# ruff: noqa: T201, D103, S101 """ -Example showing how to retrieve the primary key of your own AirTag -(or any other FindMy-accessory). +Example showing how to retrieve the primary key of your own AirTag, or any other FindMy-accessory. + This key can be used to retrieve the device's location for a single day. """ @@ -29,20 +30,20 @@ def main() -> None: for i in range(MAX_LOOKAHEAD): prim_key, sec_key = airtag.keys_at(i) - if LOOKUP_KEY == prim_key.adv_key_b64 or LOOKUP_KEY == prim_key.adv_key_b64: - print(f"KEY FOUND!!") + if LOOKUP_KEY in (prim_key.adv_key_b64, prim_key.adv_key_b64): + print("KEY FOUND!!") print(f"This key was found at index {i}." f" It was likely paired approximately {i * 15} minutes ago") print() print("KEEP THE BELOW KEY SECRET! IT CAN BE USED TO RETRIEVE THE DEVICE'S LOCATION!") - if LOOKUP_KEY == prim_key.adv_key_b64: + if prim_key.adv_key_b64 == LOOKUP_KEY: print(f"PRIMARY key: {prim_key.private_key_b64}") else: print(f"SECONDARY key: {sec_key.private_key_b64}") + break else: print("No match found! :(") - return -if __name__ == '__main__': +if __name__ == "__main__": main()