|
1 | | -"""SEC EDGAR API client library.""" |
| 1 | +"""SEC EDGAR API client library. |
| 2 | +
|
| 3 | +Top-level convenience functions let you skip the ``EdgarClient`` boilerplate:: |
| 4 | +
|
| 5 | + import edgar |
| 6 | +
|
| 7 | + company = edgar.company("AAPL") |
| 8 | + filings = edgar.get_filings("AAPL", form="10-K") |
| 9 | + results = edgar.search("revenue recognition") |
| 10 | +
|
| 11 | +Set the ``SEC_EDGAR_USER_AGENT`` environment variable to avoid passing |
| 12 | +``user_agent`` on every call:: |
| 13 | +
|
| 14 | + export SEC_EDGAR_USER_AGENT="Your Name your-email@example.com" |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import os |
2 | 20 |
|
3 | 21 | from edgar.client import EdgarClient |
4 | 22 | from edgar.exceptions import EdgarError, EdgarRequestError, EdgarParseError |
|
8 | 26 | "EdgarError", |
9 | 27 | "EdgarRequestError", |
10 | 28 | "EdgarParseError", |
| 29 | + "company", |
| 30 | + "get_filings", |
| 31 | + "search", |
| 32 | + "set_user_agent", |
11 | 33 | ] |
| 34 | + |
| 35 | +_ENV_VAR = "SEC_EDGAR_USER_AGENT" |
| 36 | +_user_agent: str | None = None |
| 37 | +_client: EdgarClient | None = None |
| 38 | + |
| 39 | + |
| 40 | +def set_user_agent(user_agent: str) -> None: |
| 41 | + """Set the default user-agent for module-level convenience functions. |
| 42 | +
|
| 43 | + This value takes priority over the ``SEC_EDGAR_USER_AGENT`` |
| 44 | + environment variable. |
| 45 | + """ |
| 46 | + |
| 47 | + global _user_agent, _client # pylint: disable=global-statement |
| 48 | + _user_agent = user_agent |
| 49 | + _client = None # force re-creation on next call |
| 50 | + |
| 51 | + |
| 52 | +def _get_client() -> EdgarClient: |
| 53 | + """Return a lazily-initialised shared ``EdgarClient``. |
| 54 | +
|
| 55 | + Resolution order for the user-agent string: |
| 56 | +
|
| 57 | + 1. Value set via ``set_user_agent()``. |
| 58 | + 2. The ``SEC_EDGAR_USER_AGENT`` environment variable. |
| 59 | +
|
| 60 | + Raises ``EdgarError`` if neither is available. |
| 61 | + """ |
| 62 | + |
| 63 | + global _client # pylint: disable=global-statement |
| 64 | + if _client is not None: |
| 65 | + return _client |
| 66 | + |
| 67 | + agent = _user_agent or os.environ.get(_ENV_VAR) |
| 68 | + if not agent: |
| 69 | + raise EdgarError( |
| 70 | + "No user-agent configured. Either call edgar.set_user_agent() " |
| 71 | + f"or set the {_ENV_VAR} environment variable." |
| 72 | + ) |
| 73 | + _client = EdgarClient(user_agent=agent) |
| 74 | + return _client |
| 75 | + |
| 76 | + |
| 77 | +def company(identifier: str) -> object: |
| 78 | + """Return a ``Company`` object for the given ticker or CIK. |
| 79 | +
|
| 80 | + >>> import edgar |
| 81 | + >>> edgar.company("AAPL").name |
| 82 | + 'Apple Inc.' |
| 83 | + """ |
| 84 | + |
| 85 | + return _get_client().company(identifier) |
| 86 | + |
| 87 | + |
| 88 | +def get_filings( |
| 89 | + identifier: str, |
| 90 | + form: str | None = None, |
| 91 | + start: int = 0, |
| 92 | + number_of_filings: int = 100, |
| 93 | +) -> list: |
| 94 | + """Return filings for a company as ``Filing`` model objects. |
| 95 | +
|
| 96 | + >>> import edgar |
| 97 | + >>> edgar.get_filings("AAPL", form="10-K")[0].form_type |
| 98 | + '10-K' |
| 99 | + """ |
| 100 | + |
| 101 | + return _get_client().company(identifier).get_filings( |
| 102 | + form=form, start=start, number_of_filings=number_of_filings |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def search( |
| 107 | + q: str, |
| 108 | + form_types: list[str] | None = None, |
| 109 | + start_date: str | None = None, |
| 110 | + end_date: str | None = None, |
| 111 | + start: int = 0, |
| 112 | + size: int = 100, |
| 113 | +) -> list: |
| 114 | + """Full-text search across SEC EDGAR filings. |
| 115 | +
|
| 116 | + >>> import edgar |
| 117 | + >>> edgar.search("revenue recognition", form_types=["10-K"])[0].company_name |
| 118 | + 'Apple Inc. ...' |
| 119 | + """ |
| 120 | + |
| 121 | + return _get_client().search( |
| 122 | + q=q, |
| 123 | + form_types=form_types, |
| 124 | + start_date=start_date, |
| 125 | + end_date=end_date, |
| 126 | + start=start, |
| 127 | + size=size, |
| 128 | + ) |
0 commit comments