Python lib/script to export Foreflight logbook
1import sys
2import time
3from typing import cast
4
5import fflogex
6from getpass import getpass
7import argparse
8from dataclasses import dataclass
9
10
11@dataclass
12class Args:
13 username: str
14 password: str | None
15 output: str
16
17
18def main():
19 parser = argparse.ArgumentParser(
20 prog="fflogex",
21 description="Export Foreflight Logbook",
22 )
23 _ = parser.add_argument("-u", "--username", required=True)
24 _ = parser.add_argument(
25 "-p",
26 "--password",
27 help="Specify password inline; You can also skip this and the program will ask you during the execution instead. BE CAREFUL: Your shell might record your password if you choose to use this option.",
28 )
29 _ = parser.add_argument(
30 "-o", "--output", help="Output file name (.csv)", default="logbook.csv"
31 )
32
33 opts = cast(Args, cast(object, parser.parse_args()))
34 if opts.password is None:
35 opts.password = getpass()
36 exporter = fflogex.ForeflightLogbookExporter()
37
38 print("Logging in...")
39 exporter.login(opts.username, opts.password)
40
41 print("Requesting logbook export...", end="", flush=True)
42 requestId = exporter.request_export()
43 print(f" [Done] Request ID: {requestId}")
44
45 print("Waiting ...", end="", flush=True)
46 elapsed = 0
47 while elapsed <= 60:
48 elapsed += 1
49 time.sleep(1)
50 print(".", end="", flush=True)
51 link = exporter.get_link(requestId)
52 if link is not None:
53 print(" [OK]")
54 print("Downloading...", end="", flush=True)
55 with open(opts.output, "w", encoding="utf-8") as f:
56 _ = f.write(exporter.download_link(link))
57 print(f" [OK] Wrote to {opts.output}")
58 sys.exit(0)
59
60 print("")
61 print("[FAILED] Timeout while waiting for the export")
62 sys.exit(1)
63
64
65if __name__ == "__main__":
66 main()