flexreportfinance 0.1.0__tar.gz
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.
- flexreportfinance-0.1.0/.gitignore +5 -0
- flexreportfinance-0.1.0/LICENSE +21 -0
- flexreportfinance-0.1.0/PKG-INFO +75 -0
- flexreportfinance-0.1.0/README.md +61 -0
- flexreportfinance-0.1.0/pyproject.toml +21 -0
- flexreportfinance-0.1.0/src/flexreportfinance/__init__.py +3 -0
- flexreportfinance-0.1.0/src/flexreportfinance/_client.py +58 -0
- flexreportfinance-0.1.0/src/flexreportfinance/catalog.py +17 -0
- flexreportfinance-0.1.0/src/flexreportfinance/events.py +45 -0
- flexreportfinance-0.1.0/src/flexreportfinance/reports.py +56 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Curt Beck
|
|
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.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: flexreportfinance
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python client for Flexreport Finance's REST API, dedicated to the streaming of real-time stock updates and full-research reports.
|
|
5
|
+
Project-URL: Repository, https://github.com/cbecks1212/flexreport-python-client
|
|
6
|
+
Project-URL: Documentation, https://app.flexreportfinapi.com/api-docs
|
|
7
|
+
Author-email: Curt Beck <curt@flexreportfinapi.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Python: >=3.11
|
|
11
|
+
Requires-Dist: httpx>=0.28.0
|
|
12
|
+
Requires-Dist: tqdm>=4.70
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# Flexreport Finance
|
|
16
|
+
|
|
17
|
+
Python client for the Flexreport Finance REST API. A username and password are required to use the client. If you do not have those, please create a free account at https://app.flexreportfinapi.com/register-api. For more information, please visit https://app.flexreportfinapi.com/ and https://app.flexreportfinapi.com/api-docs.
|
|
18
|
+
|
|
19
|
+
## Quick Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install flexreportfinance
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick example of real-time streaming
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from flexreportfinance import FlexreportClient
|
|
29
|
+
|
|
30
|
+
with FlexreportClient("username", "password") as client:
|
|
31
|
+
for event_id, event in client.events.stream(symbols=["AAPL"]):
|
|
32
|
+
print(event_id, event)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`stream()` keeps the connection open and yields events as they arrive, so the loop runs until you break out of it. Live updates are not published on weekends.
|
|
36
|
+
|
|
37
|
+
## Quick example of downloading the latest research for AAPL
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import base64
|
|
41
|
+
from flexreportfinance import FlexreportClient
|
|
42
|
+
|
|
43
|
+
with FlexreportClient("username", "password") as client:
|
|
44
|
+
report_data = client.reports.get(symbols=["AAPL"])
|
|
45
|
+
|
|
46
|
+
for symbol, report in report_data["rendered"].items():
|
|
47
|
+
file_path = f"{symbol}_report.pdf"
|
|
48
|
+
with open(file_path, "wb") as f:
|
|
49
|
+
f.write(base64.b64decode(report["result"]["pdf"]))
|
|
50
|
+
print(f"{report['result']['headline']} -> saved to {file_path}")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Downloading all of the latest reports from today
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
import base64
|
|
57
|
+
from datetime import date
|
|
58
|
+
from flexreportfinance import FlexreportClient
|
|
59
|
+
|
|
60
|
+
today = date.today().isoformat() # YYYY-MM-DD
|
|
61
|
+
with FlexreportClient("username", "password") as client:
|
|
62
|
+
report_plans = client.reports.available(report_date=today)
|
|
63
|
+
symbols = list(dict.fromkeys(plan["symbol"] for plan in report_plans))
|
|
64
|
+
if not symbols:
|
|
65
|
+
raise SystemExit(f"No reports for {today} (none are published on weekends).")
|
|
66
|
+
report_data = client.reports.get(symbols=symbols)
|
|
67
|
+
|
|
68
|
+
for symbol, report in report_data["rendered"].items():
|
|
69
|
+
file_path = f"{symbol}_report.pdf"
|
|
70
|
+
with open(file_path, "wb") as f:
|
|
71
|
+
f.write(base64.b64decode(report["result"]["pdf"]))
|
|
72
|
+
print(f"{report['result']['headline']} -> saved to {file_path}")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The client logs in on the first authenticated call and logs in again if the token expires.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Flexreport Finance
|
|
2
|
+
|
|
3
|
+
Python client for the Flexreport Finance REST API. A username and password are required to use the client. If you do not have those, please create a free account at https://app.flexreportfinapi.com/register-api. For more information, please visit https://app.flexreportfinapi.com/ and https://app.flexreportfinapi.com/api-docs.
|
|
4
|
+
|
|
5
|
+
## Quick Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install flexreportfinance
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick example of real-time streaming
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from flexreportfinance import FlexreportClient
|
|
15
|
+
|
|
16
|
+
with FlexreportClient("username", "password") as client:
|
|
17
|
+
for event_id, event in client.events.stream(symbols=["AAPL"]):
|
|
18
|
+
print(event_id, event)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`stream()` keeps the connection open and yields events as they arrive, so the loop runs until you break out of it. Live updates are not published on weekends.
|
|
22
|
+
|
|
23
|
+
## Quick example of downloading the latest research for AAPL
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import base64
|
|
27
|
+
from flexreportfinance import FlexreportClient
|
|
28
|
+
|
|
29
|
+
with FlexreportClient("username", "password") as client:
|
|
30
|
+
report_data = client.reports.get(symbols=["AAPL"])
|
|
31
|
+
|
|
32
|
+
for symbol, report in report_data["rendered"].items():
|
|
33
|
+
file_path = f"{symbol}_report.pdf"
|
|
34
|
+
with open(file_path, "wb") as f:
|
|
35
|
+
f.write(base64.b64decode(report["result"]["pdf"]))
|
|
36
|
+
print(f"{report['result']['headline']} -> saved to {file_path}")
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Downloading all of the latest reports from today
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import base64
|
|
43
|
+
from datetime import date
|
|
44
|
+
from flexreportfinance import FlexreportClient
|
|
45
|
+
|
|
46
|
+
today = date.today().isoformat() # YYYY-MM-DD
|
|
47
|
+
with FlexreportClient("username", "password") as client:
|
|
48
|
+
report_plans = client.reports.available(report_date=today)
|
|
49
|
+
symbols = list(dict.fromkeys(plan["symbol"] for plan in report_plans))
|
|
50
|
+
if not symbols:
|
|
51
|
+
raise SystemExit(f"No reports for {today} (none are published on weekends).")
|
|
52
|
+
report_data = client.reports.get(symbols=symbols)
|
|
53
|
+
|
|
54
|
+
for symbol, report in report_data["rendered"].items():
|
|
55
|
+
file_path = f"{symbol}_report.pdf"
|
|
56
|
+
with open(file_path, "wb") as f:
|
|
57
|
+
f.write(base64.b64decode(report["result"]["pdf"]))
|
|
58
|
+
print(f"{report['result']['headline']} -> saved to {file_path}")
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The client logs in on the first authenticated call and logs in again if the token expires.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "flexreportfinance"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A Python client for Flexreport Finance's REST API, dedicated to the streaming of real-time stock updates and full-research reports."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Curt Beck", email = "curt@flexreportfinapi.com" }]
|
|
13
|
+
requires-python = ">=3.11"
|
|
14
|
+
dependencies = [
|
|
15
|
+
"httpx>=0.28.0",
|
|
16
|
+
"tqdm>=4.70",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Repository = "https://github.com/cbecks1212/flexreport-python-client"
|
|
21
|
+
Documentation = "https://app.flexreportfinapi.com/api-docs"
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
from .catalog import Catalog
|
|
6
|
+
from .events import Events
|
|
7
|
+
from .reports import Reports
|
|
8
|
+
|
|
9
|
+
BASE_URL = "https://flexreportfinapi.com"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class _TokenAuth(httpx.Auth):
|
|
13
|
+
"""Adds the bearer token, fetching it on first use and again if the API answers 401."""
|
|
14
|
+
|
|
15
|
+
def __init__(self, client: "FlexreportClient"):
|
|
16
|
+
self._client = client
|
|
17
|
+
self._token: str | None = None
|
|
18
|
+
|
|
19
|
+
def auth_flow(self, request):
|
|
20
|
+
if self._token is None:
|
|
21
|
+
self._token = self._client._fetch_token()
|
|
22
|
+
request.headers["Authorization"] = f"Bearer {self._token}"
|
|
23
|
+
response = yield request
|
|
24
|
+
|
|
25
|
+
if response.status_code == 401:
|
|
26
|
+
self._token = self._client._fetch_token()
|
|
27
|
+
request.headers["Authorization"] = f"Bearer {self._token}"
|
|
28
|
+
yield request
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class FlexreportClient:
|
|
32
|
+
def __init__(self, username: str, password: str, base_url: str = BASE_URL, timeout: float = 30):
|
|
33
|
+
self._username = username
|
|
34
|
+
self._password = password
|
|
35
|
+
self._http = httpx.Client(base_url=base_url, timeout=timeout, auth=_TokenAuth(self))
|
|
36
|
+
|
|
37
|
+
self.catalog = Catalog(self)
|
|
38
|
+
self.reports = Reports(self)
|
|
39
|
+
self.events = Events(self)
|
|
40
|
+
|
|
41
|
+
def _fetch_token(self) -> str:
|
|
42
|
+
resp = self._http.post("/token", data={"username": self._username, "password": self._password}, auth=None)
|
|
43
|
+
resp.raise_for_status()
|
|
44
|
+
return resp.json()["access_token"]
|
|
45
|
+
|
|
46
|
+
def _json(self, method: str, url: str, **kwargs) -> Any:
|
|
47
|
+
resp = self._http.request(method, url, **kwargs)
|
|
48
|
+
resp.raise_for_status()
|
|
49
|
+
return resp.json()
|
|
50
|
+
|
|
51
|
+
def close(self) -> None:
|
|
52
|
+
self._http.close()
|
|
53
|
+
|
|
54
|
+
def __enter__(self) -> "FlexreportClient":
|
|
55
|
+
return self
|
|
56
|
+
|
|
57
|
+
def __exit__(self, *exc) -> None:
|
|
58
|
+
self.close()
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING, Any
|
|
2
|
+
|
|
3
|
+
if TYPE_CHECKING:
|
|
4
|
+
from ._client import FlexreportClient
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Catalog:
|
|
8
|
+
"""Public reference data; these endpoints need no login."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, client: "FlexreportClient"):
|
|
11
|
+
self._client = client
|
|
12
|
+
|
|
13
|
+
def symbols(self) -> Any:
|
|
14
|
+
return self._client._json("GET", "/list-tickers", auth=None)
|
|
15
|
+
|
|
16
|
+
def topics(self) -> Any:
|
|
17
|
+
return self._client._json("GET", "/list-realtime-event-options", auth=None)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from collections.abc import Iterator
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from ._client import FlexreportClient
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class Events:
|
|
12
|
+
def __init__(self, client: "FlexreportClient"):
|
|
13
|
+
self._client = client
|
|
14
|
+
|
|
15
|
+
def stream(
|
|
16
|
+
self,
|
|
17
|
+
topics: list[str] | None = None,
|
|
18
|
+
symbols: list[str] | None = None,
|
|
19
|
+
last_event_id: str | None = None,
|
|
20
|
+
read_timeout: float = 30,
|
|
21
|
+
) -> Iterator[tuple[str | None, dict]]:
|
|
22
|
+
"""Yield (event_id, event) from the server-sent event stream.
|
|
23
|
+
|
|
24
|
+
Pass the last event_id you saw as last_event_id to resume after a disconnect.
|
|
25
|
+
"""
|
|
26
|
+
params = {k: v for k, v in {"topics": topics, "symbols": symbols}.items() if v is not None}
|
|
27
|
+
headers = {"Last-Event-ID": last_event_id} if last_event_id else {}
|
|
28
|
+
|
|
29
|
+
with self._client._http.stream(
|
|
30
|
+
"GET", "/events", params=params, headers=headers, timeout=httpx.Timeout(10, read=read_timeout)
|
|
31
|
+
) as resp:
|
|
32
|
+
resp.raise_for_status()
|
|
33
|
+
frame: dict[str, str] = {}
|
|
34
|
+
|
|
35
|
+
for line in resp.iter_lines():
|
|
36
|
+
if line:
|
|
37
|
+
if line.startswith(":"):
|
|
38
|
+
continue
|
|
39
|
+
key, _, value = line.partition(":")
|
|
40
|
+
value = value.removeprefix(" ")
|
|
41
|
+
frame[key] = f"{frame[key]}\n{value}" if key == "data" and key in frame else value
|
|
42
|
+
continue
|
|
43
|
+
if frame.get("event") == "event" and "data" in frame:
|
|
44
|
+
yield frame.get("id"), json.loads(frame["data"])
|
|
45
|
+
frame = {}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from typing import TYPE_CHECKING, Any
|
|
3
|
+
|
|
4
|
+
from tqdm.auto import tqdm
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from ._client import FlexreportClient
|
|
8
|
+
|
|
9
|
+
TERMINAL = {"SUCCESS", "FAILURE", "REVOKED"}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Reports:
|
|
13
|
+
def __init__(self, client: "FlexreportClient"):
|
|
14
|
+
self._client = client
|
|
15
|
+
|
|
16
|
+
def available(self, report_date: str, event_types: list[str] | None = None) -> Any:
|
|
17
|
+
return self._client._json("POST", "/list-available-reports", json={"event_types": event_types, "report_date": report_date}, timeout=60)
|
|
18
|
+
|
|
19
|
+
def get(self, symbols: list[str], *, max_wait: float = 900, poll_every: float = 3.0, progress: bool = True) -> dict:
|
|
20
|
+
if not symbols:
|
|
21
|
+
raise ValueError("symbols must not be empty")
|
|
22
|
+
|
|
23
|
+
data = self._client._json("POST", "/get-cached-reports", json=symbols, timeout=900)
|
|
24
|
+
cached = data.get("result", [])
|
|
25
|
+
missing = data.get("missing", [])
|
|
26
|
+
rendering = data.get("rendering", {}) # {ticker: {"task_id", "status", "symbol"}}
|
|
27
|
+
|
|
28
|
+
total = len(cached) + len(missing) + len(rendering)
|
|
29
|
+
rendered, failed = {}, {}
|
|
30
|
+
with tqdm(total=total, desc="Reports", unit="sym", disable=not progress) as pbar:
|
|
31
|
+
pbar.update(len(cached) + len(missing))
|
|
32
|
+
|
|
33
|
+
pending = {t: v["task_id"] for t, v in rendering.items()}
|
|
34
|
+
deadline = time.monotonic() + max_wait
|
|
35
|
+
|
|
36
|
+
while pending and time.monotonic() < deadline:
|
|
37
|
+
for ticker, task_id in list(pending.items()):
|
|
38
|
+
body = self._client._json("GET", "/task-status", params={"task_id": task_id})
|
|
39
|
+
status = body.get("status")
|
|
40
|
+
|
|
41
|
+
if status in TERMINAL:
|
|
42
|
+
(rendered if status == "SUCCESS" else failed)[ticker] = body
|
|
43
|
+
del pending[ticker]
|
|
44
|
+
pbar.update(1)
|
|
45
|
+
else:
|
|
46
|
+
pbar.set_postfix_str(f"{ticker}: {status}")
|
|
47
|
+
|
|
48
|
+
if pending:
|
|
49
|
+
time.sleep(poll_every)
|
|
50
|
+
|
|
51
|
+
if pending:
|
|
52
|
+
if progress:
|
|
53
|
+
tqdm.write(f"Timed out waiting on: {sorted(pending)}")
|
|
54
|
+
failed.update({t: {"status": "TIMEOUT", "task_id": tid} for t, tid in pending.items()})
|
|
55
|
+
|
|
56
|
+
return {"cached": cached, "rendered": rendered, "missing": missing, "failed": failed}
|