Use most recent CSV list of flights.
This commit is contained in:
parent
bf682d7803
commit
a2b30a9ac7
14
show
14
show
|
@ -1,10 +1,11 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
|
import os
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
# from pprint import pprint
|
backup_loc = os.path.expanduser("~/backup/openflights")
|
||||||
|
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
since_date = now - timedelta(days=365 * 5)
|
since_date = now - timedelta(days=365 * 5)
|
||||||
|
|
||||||
|
@ -21,6 +22,12 @@ Flight = dict[str, str]
|
||||||
Flights = list[tuple[datetime, Flight]]
|
Flights = list[tuple[datetime, Flight]]
|
||||||
|
|
||||||
|
|
||||||
|
def get_most_recent(loc: str, pred: Callable[[str], bool] | None = None) -> str | None:
|
||||||
|
"""Get the most recent file in a directory."""
|
||||||
|
files = [f for f in os.listdir(loc) if pred is None or pred(f)]
|
||||||
|
return os.path.join(loc, max(files)) if files else None
|
||||||
|
|
||||||
|
|
||||||
def format_row(dt: datetime, f: Flight) -> str:
|
def format_row(dt: datetime, f: Flight) -> str:
|
||||||
"""Format flight."""
|
"""Format flight."""
|
||||||
d = dt.strftime("%a, %d %b %Y")
|
d = dt.strftime("%a, %d %b %Y")
|
||||||
|
@ -82,6 +89,7 @@ def show_flights(flights: Flights) -> None:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
filename = "/home/edward/backup/openflights/openflights-backup-2023-09-09.csv"
|
filename = get_most_recent(backup_loc, lambda f: f.endswith(".csv"))
|
||||||
|
assert filename
|
||||||
flights = read_flights(filename)
|
flights = read_flights(filename)
|
||||||
show_flights(flights)
|
show_flights(flights)
|
||||||
|
|
Loading…
Reference in a new issue