Add backup script, README and LICENSE
Read config from config file
This commit is contained in:
parent
5dd224f6e4
commit
69b05c15a6
4 changed files with 182 additions and 3 deletions
44
run_backup
Executable file
44
run_backup
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
#!/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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue