fr24 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Abraham Cheung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
fr24/__init__.py ADDED
File without changes
fr24/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .cli import app
2
+
3
+ app(prog_name="fr24")
fr24/authentication.py ADDED
@@ -0,0 +1,56 @@
1
+ from __future__ import annotations
2
+
3
+ import configparser
4
+ import os
5
+ from pathlib import Path
6
+
7
+ import httpx
8
+ from appdirs import user_config_dir
9
+
10
+ from .common import DEFAULT_HEADERS
11
+ from .types.fr24 import Authentication
12
+
13
+ username = os.environ.get("fr24_username", None)
14
+ password = os.environ.get("fr24_password", None)
15
+ subscription_key = os.environ.get("fr24_subscription_key", None)
16
+ token = os.environ.get("fr24_token", None)
17
+
18
+ if (config_file := (Path(user_config_dir("fr24")) / "fr24.conf")).exists():
19
+ config = configparser.ConfigParser()
20
+ config.read(config_file.as_posix())
21
+
22
+ username = config.get("global", "username", fallback=None)
23
+ password = config.get("global", "password", fallback=None)
24
+ subscription_key = config.get("global", "subscription_key", fallback=None)
25
+ token = config.get("global", "token", fallback=None)
26
+
27
+
28
+ async def login(client: httpx.AsyncClient) -> None | Authentication:
29
+ """
30
+ Create credentials for the user (if any). If the environment variables:
31
+
32
+ - `subscription_key` and `token` is set: it returns the pair
33
+ - `username` and `password` is set: it fetches the token and
34
+ subscription key from the API
35
+ - otherwise, it returns `None`
36
+ """
37
+ if username is None or password is None:
38
+ if not subscription_key:
39
+ return None
40
+ return {
41
+ "userData": {
42
+ "subscriptionKey": subscription_key,
43
+ "accessToken": token,
44
+ },
45
+ "message": "using subscriptionKey and/or accessToken.",
46
+ }
47
+
48
+ res = await client.post(
49
+ "https://www.flightradar24.com/user/login",
50
+ data={"email": username, "password": password},
51
+ headers=DEFAULT_HEADERS,
52
+ )
53
+ res.raise_for_status()
54
+ return res.json() # type: ignore
55
+ # json['userData']['accessToken'] => Bearer
56
+ # json['userData']['subscriptionKey'] => token
fr24/base.py ADDED
@@ -0,0 +1,157 @@
1
+ from __future__ import annotations
2
+
3
+ from abc import ABC, abstractmethod
4
+ from pathlib import Path
5
+ from typing import Any, Generic, TypeVar
6
+
7
+ import httpx
8
+ import pyarrow as pa
9
+ import pyarrow.parquet as pq
10
+ from loguru import logger
11
+ from typing_extensions import Self
12
+
13
+ import pandas as pd
14
+
15
+ from .authentication import login
16
+ from .types.fr24 import Authentication
17
+
18
+
19
+ class HTTPClient:
20
+ """
21
+ An HTTPX client for making requests to the API and storing
22
+ authentication data.
23
+ """
24
+
25
+ def __init__(self, *, retries: int = 5) -> None:
26
+ transport = httpx.AsyncHTTPTransport(retries=retries)
27
+ self.client = httpx.AsyncClient(transport=transport)
28
+ self.auth: Authentication | None = None
29
+
30
+ async def __aenter__(self) -> Self:
31
+ self.auth = await login(self.client)
32
+ return self
33
+
34
+ async def __aexit__(self, *args: Any) -> None:
35
+ if self.client is not None:
36
+ await self.client.aclose()
37
+
38
+
39
+ ApiRsp = TypeVar("ApiRsp")
40
+ """Type returned by the API, e.g. [fr24.types.fr24.FlightList][]"""
41
+ Ctx = TypeVar("Ctx")
42
+ """Type of the context for the service, a TypedDict"""
43
+
44
+
45
+ class APIBase(Generic[ApiRsp, Ctx], ABC):
46
+ def __init__(self, http: HTTPClient, ctx: Ctx) -> None:
47
+ self.http = http
48
+ self.ctx = ctx
49
+
50
+ @abstractmethod
51
+ async def fetch(self, *args: Any, **kwargs: Any) -> ApiRsp:
52
+ """Fetch data from the API."""
53
+ ...
54
+
55
+
56
+ class ArrowBase(Generic[ApiRsp, Ctx], ABC):
57
+ """A base class for handling arrow tables."""
58
+
59
+ schema: pa.Schema | None = None
60
+
61
+ def __init__(self, base_dir: str, ctx: Ctx) -> None:
62
+ self.base_dir = Path(base_dir)
63
+ self._table: pa.Table | None = None
64
+ self.ctx = ctx
65
+
66
+ @property
67
+ @abstractmethod
68
+ def fp(self) -> Path:
69
+ """Path to the parquet file."""
70
+ ...
71
+
72
+ @property
73
+ def table(self) -> pa.Table | None:
74
+ return self._table
75
+
76
+ @table.setter
77
+ def table(self, table: pa.Table) -> None:
78
+ """Set the arrow table or extend it if it already exists."""
79
+ self._table = (
80
+ table
81
+ if self._table is None
82
+ else self._concat_tables(self._table, table)
83
+ )
84
+
85
+ @staticmethod
86
+ def _concat_tables(tbl_old: pa.Table, tbl_new: pa.Table) -> pa.Table:
87
+ """Override this to customize how tables should be concatenated."""
88
+ return pa.concat_tables([tbl_old, tbl_new])
89
+
90
+ @abstractmethod
91
+ def add_api_response(self, data: ApiRsp) -> Self:
92
+ """Parse API response and add to the arrow table."""
93
+
94
+ def add_parquet(self, fp: Path | None = None) -> Self:
95
+ """
96
+ Read parquet file and add to the arrow table.
97
+
98
+ :param fp: Path to the parquet file - if not provided, the default cache
99
+ directory is used.
100
+ """
101
+ out_fp = fp if fp is not None else self.fp
102
+
103
+ # pq.read_table apparently doesn't read schema metadata:
104
+ # overwriting self.schema using pq.read_schema instead.
105
+ sch = pq.read_schema(out_fp)
106
+ if sch_diffs := set(sch).difference(set(self.schema or [])):
107
+ logger.warning(f"Parquet schema mismatch:\n{sch_diffs}, will cast.")
108
+ sch = sch.cast(self.schema) # attempt to cast the schema
109
+ self.schema = sch
110
+
111
+ self.table = pq.read_table(out_fp, **self._schema_kwargs)
112
+ num_rows = self._table.num_rows if self._table is not None else 0
113
+ logger.debug(f"Read {out_fp}: {num_rows=}")
114
+ return self
115
+
116
+ def save_parquet(self, fp: Path | None = None) -> Self:
117
+ """
118
+ Write the arrow table to a parquet file.
119
+ :param fp: Path to the parquet file - if not provided, the default cache
120
+ directory is used.
121
+ """
122
+ if self._table is None:
123
+ logger.warning("No data to write.")
124
+ return self
125
+ out_fp = fp if fp is not None else self.fp
126
+ out_fp.parent.mkdir(parents=True, exist_ok=True)
127
+ with pq.ParquetWriter(out_fp, **self._schema_kwargs) as writer:
128
+ writer.write_table(self._table)
129
+ logger.debug(f"Saved {self._table.num_rows} rows to {out_fp}")
130
+ return self
131
+
132
+ def clear(self) -> None:
133
+ """Clear the arrow table."""
134
+ self._table = None
135
+
136
+ @property
137
+ def _schema_kwargs(self) -> dict[str, Any]:
138
+ return {} if self.schema is None else {"schema": self.schema}
139
+
140
+ @property
141
+ def df(self) -> pd.DataFrame | None:
142
+ return self._table.to_pandas() if self._table is not None else None
143
+
144
+
145
+ ApiBaseT = TypeVar("ApiBaseT")
146
+ """`APIBase[ApiRsp]`"""
147
+ FileBaseT = TypeVar("FileBaseT")
148
+ """`FileBase[ApiRsp]`"""
149
+
150
+
151
+ class ServiceBase(Generic[ApiBaseT, FileBaseT, Ctx]):
152
+ """A service to handle the API and file operations."""
153
+
154
+ def __init__(self, api: ApiBaseT, data: FileBaseT, ctx: Ctx) -> None:
155
+ self.api = api
156
+ self.data = data
157
+ self.ctx = ctx
fr24/cli.py ADDED
@@ -0,0 +1,102 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ from datetime import datetime
5
+ from pathlib import Path
6
+ from typing import Annotated, Optional
7
+
8
+ import rich
9
+ import typer
10
+ from appdirs import user_cache_dir, user_config_dir
11
+
12
+ from .core import FR24, FlightListService, LiveFeedService, PlaybackService
13
+ from .tui.tui import main as tui_main
14
+
15
+ app = typer.Typer(no_args_is_help=True)
16
+
17
+
18
+ @app.command()
19
+ def dirs() -> None:
20
+ """Shows relevant directories"""
21
+ rich.print(f"Config: {user_config_dir('fr24')}")
22
+ rich.print(f" Cache: {user_cache_dir('fr24')}")
23
+
24
+
25
+ @app.command()
26
+ def tui() -> None:
27
+ """Starts the TUI"""
28
+
29
+ tui_main()
30
+
31
+
32
+ app_auth = typer.Typer()
33
+ app.add_typer(
34
+ app_auth,
35
+ name="auth",
36
+ no_args_is_help=True,
37
+ help="Commands for authentication",
38
+ )
39
+
40
+
41
+ @app_auth.command()
42
+ def show() -> None:
43
+ """Shows authentication status"""
44
+
45
+ async def show_() -> None:
46
+ async with FR24() as fr24:
47
+ if fr24.http.auth is None:
48
+ rich.print(
49
+ "[bold red]You are not authenticated.[/bold red]\n"
50
+ "Provide credentials in environment variables: either \n"
51
+ "- fr24_username + fr24_password or \n"
52
+ "- fr24_subscription_key + fr24_token\n"
53
+ "Alternatively, copy the example config file to "
54
+ f"{Path(user_config_dir('fr24')) / 'fr24.conf'}."
55
+ )
56
+ else:
57
+ rich.print("[bold green]Authenticated[/bold green]")
58
+ rich.print(fr24.http.auth)
59
+
60
+ asyncio.run(show_())
61
+
62
+
63
+ def get_success_message(
64
+ service: FlightListService | LiveFeedService | PlaybackService,
65
+ ) -> str:
66
+ num_rows = t.num_rows if (t := service.data.table) is not None else 0
67
+ size = service.data.fp.stat().st_size
68
+ return (
69
+ f"[bold green]Success: Saved {num_rows} rows ({size} bytes) "
70
+ f"to {service.data.fp}.[/bold green]"
71
+ )
72
+
73
+
74
+ @app.command()
75
+ def feed(
76
+ timestamp: Annotated[
77
+ Optional[int], typer.Option(help="Start unix timestamp (s, UTC)")
78
+ ] = None,
79
+ time: Annotated[
80
+ Optional[datetime], typer.Option(help="Start datetime (UTC)")
81
+ ] = None,
82
+ ) -> None:
83
+ """Fetches current livefeed / playback of live feed at a given time"""
84
+ # if time is None and timestamp is None:
85
+ # raise typer.BadParameter("Either time or timestamp must be set")
86
+ if time is not None and timestamp is not None:
87
+ raise typer.BadParameter("Only one of time and timestamp can be set")
88
+ if time is not None:
89
+ timestamp = int(time.timestamp())
90
+
91
+ async def feed_() -> None:
92
+ async with FR24() as fr24:
93
+ lf = fr24.livefeed(timestamp)
94
+ lf.data.add_api_response(await lf.api.fetch())
95
+ lf.data.save_parquet()
96
+ rich.print(get_success_message(lf))
97
+
98
+ asyncio.run(feed_())
99
+
100
+
101
+ if __name__ == "__main__":
102
+ app()
fr24/common.py ADDED
@@ -0,0 +1,45 @@
1
+ from __future__ import annotations
2
+
3
+ from datetime import datetime
4
+
5
+ import pandas as pd
6
+
7
+ DEFAULT_HEADERS = {
8
+ "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0)"
9
+ "Gecko/20100101 Firefox/116.0",
10
+ "Accept": "text/html,application/xhtml+xml,application/xml;"
11
+ "q=0.9,image/avif,image/webp,*/*;q=0.8",
12
+ "Accept-Language": "en-US,en;q=0.5",
13
+ "Accept-Encoding": "gzip, deflate, br",
14
+ "Origin": "https://www.flightradar24.com",
15
+ "Connection": "keep-alive",
16
+ "Referer": "https://www.flightradar24.com/",
17
+ "Sec-Fetch-Dest": "empty",
18
+ "Sec-Fetch-Mode": "cors",
19
+ "Sec-Fetch-Site": "same-site",
20
+ "TE": "trailers",
21
+ }
22
+
23
+ DEFAULT_HEADERS_GRPC = {
24
+ **DEFAULT_HEADERS,
25
+ "Accept": "*/*",
26
+ "fr24-device-id": "web-00000000000000000000000000000000",
27
+ "x-envoy-retry-grpc-on": "unavailable",
28
+ "Content-Type": "application/grpc-web+proto",
29
+ "X-User-Agent": "grpc-web-javascript/0.1",
30
+ "X-Grpc-Web": "1",
31
+ "DNT": "1",
32
+ }
33
+
34
+
35
+ def to_unix_timestamp(
36
+ timestamp: int | datetime | pd.Timestamp | str | None,
37
+ ) -> int | None:
38
+ """
39
+ Casts timestamp-like object to Unix timestamp.
40
+ """
41
+ if isinstance(timestamp, (str, datetime)):
42
+ return int(pd.Timestamp(timestamp).timestamp())
43
+ if isinstance(timestamp, pd.Timestamp):
44
+ return int(timestamp.timestamp())
45
+ return timestamp