Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions examples/fetch_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ def fetch_reports(priv_key: str) -> int:

# Step 1: construct a key object and get its location reports
key = KeyPair.from_b64(priv_key)
reports = acc.fetch_last_reports(key)
location = acc.fetch_location(key)

# Step 2: print the reports!
for report in sorted(reports):
print(report)
# Step 2: print it!
print("Last known location:")
print(f" - {location}")

# We can save the report to a file if we want
report.to_json("last_report.json")
# Step 3 (optional): We can save the location report to a file if we want.
# BUT WATCH OUT! This file will contain the tag's private key!
if location is not None:
location.to_json("last_report.json")

# Step 3: Make sure to save account state when you're done!
# To load it later:
# loc = LocationReport.from_json("last_report.json")

# Step 4: Make sure to save account state when you're done!
# Otherwise you have to log in again...
acc.to_json(STORE_PATH)

return 0
Expand Down
18 changes: 12 additions & 6 deletions examples/fetch_reports_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@ async def fetch_reports(priv_key: str) -> int:

# Step 1: construct a key object and get its location reports
key = KeyPair.from_b64(priv_key)
reports = await acc.fetch_last_reports(key)
location = await acc.fetch_location(key)

# Step 2: print the reports!
for report in sorted(reports):
print(report)
# Step 2: print it!
print("Last known location:")
print(f" - {location}")

# We can save the report to a file if we want
report.to_json("last_report.json")
# Step 3 (optional): We can save the location report to a file if we want.
# BUT WATCH OUT! This file will contain the tag's private key!
if location is not None:
location.to_json("last_report.json")

# To load it later:
# loc = LocationReport.from_json("last_report.json")
finally:
await acc.close()

# Make sure to save account state when you're done!
# Otherwise you have to log in again...
acc.to_json(STORE_PATH)

return 0
Expand Down
29 changes: 29 additions & 0 deletions examples/plist_to_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

from pathlib import Path

from findmy import FindMyAccessory


def main(output: Path, accessory_plist: Path, alignment_plist: Path | None = None) -> int:
accessory = FindMyAccessory.from_plist(accessory_plist, alignment_plist)
accessory.to_json(output)
return 0


if __name__ == "__main__":
import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument("accessory_plist", type=Path, help="Input accessory plist file")
parser.add_argument("output", type=Path, help="Output JSON file")
parser.add_argument(
"--alignment-plist",
type=Path,
help="Input alignment plist file (if available)",
default=None,
)
args = parser.parse_args()

sys.exit(main(args.output, args.accessory_plist, args.alignment_plist))
27 changes: 13 additions & 14 deletions examples/real_airtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

from __future__ import annotations

import argparse
import logging
import sys
from pathlib import Path

from _login import get_account_sync

Expand All @@ -29,35 +31,32 @@
logging.basicConfig(level=logging.INFO)


def main(plist_path: str) -> int:
def main(airtag_path: Path) -> int:
# Step 0: create an accessory key generator
airtag = FindMyAccessory.from_plist(plist_path)
airtag = FindMyAccessory.from_json(airtag_path)

# Step 1: log into an Apple account
print("Logging into account")
acc = get_account_sync(STORE_PATH, ANISETTE_SERVER, ANISETTE_LIBS_PATH)

# step 2: fetch reports!
print("Fetching reports")
reports = acc.fetch_last_reports(airtag)
print("Fetching location")
location = acc.fetch_location(airtag)

# step 3: print 'em
print()
print("Location reports:")
for report in sorted(reports):
print(f" - {report}")
print("Last known location:")
print(f" - {location}")

# step 4: save current account state to disk
acc.to_json(STORE_PATH)
airtag.to_json(airtag_path)

return 0


if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <path to accessory plist>", 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)
parser = argparse.ArgumentParser()
parser.add_argument("airtag_path", type=Path)
args = parser.parse_args()

sys.exit(main(sys.argv[1]))
sys.exit(main(args.airtag_path))
36 changes: 27 additions & 9 deletions examples/device_scanner.py → examples/scanner.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import argparse
import asyncio
import logging
import sys
from pathlib import Path

from findmy import KeyPair
from findmy.accessory import FindMyAccessory
from findmy.scanner import (
NearbyOfflineFindingDevice,
OfflineFindingScanner,
Expand Down Expand Up @@ -35,7 +37,7 @@ def _print_separated(device: SeparatedOfflineFindingDevice) -> None:
print()


async def scan(check_key: KeyPair | None = None) -> None:
async def scan(check_key: KeyPair | FindMyAccessory | None = None) -> bool:
scanner = await OfflineFindingScanner.create()

print("Scanning for FindMy-devices...")
Expand All @@ -56,15 +58,31 @@ async def scan(check_key: KeyPair | None = None) -> None:
if check_key and device.is_from(check_key):
scan_device = device

print()
if scan_device:
print("Key or accessory was found in scan results! :D")
print("Device was found in scan results! :D")
elif check_key:
print("Selected key or accessory was not found in scan results... :c")
print("Device was not found in scan results... :c")

return scan_device is not None and check_key is not None

if __name__ == "__main__":
key = None
if len(sys.argv) >= 2:
key = KeyPair.from_b64(sys.argv[1])

asyncio.run(scan(key))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("--private_key", type=str)
group.add_argument("--airtag_file", type=Path)
args = parser.parse_args()

dev: KeyPair | FindMyAccessory | None = None
if args.private_key:
dev = KeyPair.from_b64(args.private_key)
elif args.airtag_file:
dev = FindMyAccessory.from_json(args.airtag_file)

device_found = asyncio.run(scan(dev))

if device_found and isinstance(dev, FindMyAccessory):
print("Current scan results were used to align the accessory.")
print(f'Updated alignment will be saved to "{args.airtag_file}".')
dev.to_json(args.airtag_file)
5 changes: 2 additions & 3 deletions findmy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from importlib.metadata import version
from pathlib import Path

from .plist import get_key, list_accessories
from .plist import list_accessories


def main() -> None: # noqa: D103
Expand Down Expand Up @@ -96,8 +96,7 @@ def get_path(d, acc) -> Path | None: # noqa: ANN001
d.mkdir(parents=True, exist_ok=True)
return d / f"{acc.identifier}.json"

key = get_key()
accs = list_accessories(key=key)
accs = list_accessories()
jsons = [acc.to_json(get_path(out_dir, acc)) for acc in accs]
print(json.dumps(jsons, indent=4, ensure_ascii=False)) # noqa: T201

Expand Down
Loading