mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-28 20:23:36 +00:00
* merge branch develop with main * bump desktop version * fix the merge * sync Cargo.lock to desktop 2.0.0 * fix cli conftest.py --------- Co-authored-by: deeleeramone <>
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
"""Documents handed to the Workspace file viewer."""
|
|
|
|
PDF_MAGIC = b"%PDF"
|
|
|
|
|
|
async def _download(url: str) -> bytes | None:
|
|
"""Read one document, or nothing when the host will not serve it."""
|
|
from openbb_tmx.utils.cache import amake_request
|
|
|
|
try:
|
|
body = await amake_request(
|
|
url, use_cache=False, accept_type="binary", referer=url
|
|
)
|
|
except Exception: # noqa: BLE001
|
|
return None
|
|
|
|
return body if isinstance(body, bytes) and body.startswith(PDF_MAGIC) else None
|
|
|
|
|
|
async def as_viewer_files(selections: "list[tuple[str, str]]") -> list[dict]:
|
|
"""Serve each selected document to the file viewer.
|
|
|
|
Parameters
|
|
----------
|
|
selections : list[tuple[str, str]]
|
|
The URL and the file name of each selected document.
|
|
|
|
Returns
|
|
-------
|
|
list[dict]
|
|
One entry per document, carrying the document itself where the host
|
|
served it and the URL to load where it did not.
|
|
"""
|
|
import asyncio
|
|
from base64 import b64encode
|
|
|
|
documents = await asyncio.gather(*(_download(url) for url, _ in selections))
|
|
results: list[dict] = []
|
|
|
|
for (url, filename), document in zip(selections, documents):
|
|
entry: dict = {"data_format": {"data_type": "pdf", "filename": filename}}
|
|
|
|
if document is None:
|
|
entry["url"] = url
|
|
else:
|
|
entry["content"] = b64encode(document).decode("utf-8")
|
|
|
|
results.append(entry)
|
|
|
|
return results
|