Skip to content

Commit

Permalink
Cleanup examples
Browse files Browse the repository at this point in the history
  • Loading branch information
malmeloo committed Feb 6, 2024
1 parent 7e46afc commit d4a5fce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
5 changes: 3 additions & 2 deletions examples/device_scanner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ruff: noqa: T201, D103
import asyncio
import logging

Expand All @@ -6,7 +7,7 @@
logging.basicConfig(level=logging.INFO)


async def scan():
async def scan() -> None:
scanner = await OfflineFindingScanner.create()

print("Scanning for FindMy-devices...")
Expand All @@ -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()
Expand Down
24 changes: 12 additions & 12 deletions examples/fetch_reports.py
Original file line number Diff line number Diff line change
@@ -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,
)

Expand All @@ -15,7 +16,7 @@

# Apple account details
ACCOUNT_EMAIL = "[email protected]"
ACCOUNT_PASS = "1234"
ACCOUNT_PASS = ""

# Private base64-encoded key to look up
KEY_PRIV = ""
Expand All @@ -26,7 +27,7 @@
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
Expand All @@ -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})")
Expand All @@ -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

Expand Down
25 changes: 12 additions & 13 deletions examples/fetch_reports_async.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# 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
ANISETTE_SERVER = "http://localhost:6969"

# Apple account details
ACCOUNT_EMAIL = "[email protected]"
ACCOUNT_PASS = "1234"
ACCOUNT_PASS = ""

# Private base64-encoded key to look up
KEY_PRIV = ""
Expand All @@ -27,7 +28,7 @@
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
Expand All @@ -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})")
Expand All @@ -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

Expand Down
15 changes: 8 additions & 7 deletions examples/real_airtag.py
Original file line number Diff line number Diff line change
@@ -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.
"""

Expand Down Expand Up @@ -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()

0 comments on commit d4a5fce

Please sign in to comment.