flights/run_backup
2023-09-29 08:13:27 +01:00

45 lines
1.3 KiB
Python
Executable file

#!/usr/bin/python3
"""Download CSV of flights from OpenFlights."""
import configparser
import os.path
from playwright.sync_api import Playwright, sync_playwright
config_file_path = os.path.expanduser(
os.path.join(os.getenv("XDG_CONFIG_HOME", "~/.config"), "OpenFlights", "config")
)
config = configparser.ConfigParser()
config.read(os.path.expanduser(config_file_path))
backup_dir = os.path.expanduser(config.get("backup", "backup_dir"))
username = config.get("backup", "username")
password = config.get("backup", "password")
def run(playwright: Playwright) -> None:
"""Run backup."""
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto("https://openflights.org/")
page.locator('input[name="name"]').fill(username)
page.locator('input[name="pw"]').fill(password)
page.get_by_role("button", name="Log in").click()
page.get_by_role("button", name="Settings").click()
with page.expect_download() as download_info:
page.get_by_role("button", name="Backup to CSV").click()
download = download_info.value
download.save_as(os.path.join(backup_dir, download.suggested_filename))
page.close()
context.storage_state(path="auth.json")
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)