reports: update real_airtag example

This commit is contained in:
Mike A
2024-04-23 21:36:46 +02:00
parent 3cc97863e2
commit 4e213e5a48
5 changed files with 160 additions and 125 deletions

View File

@@ -1,24 +1,14 @@
import asyncio
import json
import logging
from pathlib import Path
from _login import get_account_async
from findmy import KeyPair
from findmy.reports import (
AsyncAppleAccount,
LoginState,
RemoteAnisetteProvider,
SmsSecondFactorMethod,
TrustedDeviceSecondFactorMethod,
)
from findmy.reports import RemoteAnisetteProvider
# URL to (public or local) anisette server
ANISETTE_SERVER = "http://localhost:6969"
# Apple account details
ACCOUNT_EMAIL = "test@test.com"
ACCOUNT_PASS = ""
# Private base64-encoded key to look up
KEY_PRIV = ""
@@ -28,44 +18,12 @@ KEY_ADV = ""
logging.basicConfig(level=logging.DEBUG)
async def login(account: AsyncAppleAccount) -> None:
state = await account.login(ACCOUNT_EMAIL, ACCOUNT_PASS)
if state == LoginState.REQUIRE_2FA: # Account requires 2FA
# This only supports SMS methods for now
methods = await account.get_2fa_methods()
# Print the (masked) phone numbers
for i, method in enumerate(methods):
if isinstance(method, TrustedDeviceSecondFactorMethod):
print(f"{i} - Trusted Device")
elif isinstance(method, SmsSecondFactorMethod):
print(f"{i} - SMS ({method.phone_number})")
ind = int(input("Method? > "))
method = methods[ind]
await method.request()
code = input("Code? > ")
# This automatically finishes the post-2FA login flow
await method.submit(code)
async def fetch_reports(lookup_key: KeyPair) -> None:
anisette = RemoteAnisetteProvider(ANISETTE_SERVER)
acc = AsyncAppleAccount(anisette)
acc = await get_account_async(anisette)
try:
acc_store = Path("account.json")
try:
with acc_store.open() as f:
acc.restore(json.load(f))
except FileNotFoundError:
await login(acc)
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})")
# It's that simple!