examples: take inputs from command line instead of variable

This commit is contained in:
Mike A
2024-04-25 19:51:04 +02:00
parent fe534931cf
commit 5bc1e2dd3c
3 changed files with 42 additions and 36 deletions

View File

@@ -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]} <private key>", 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]))