Skip to content

Commit 797017b

Browse files
added .get_file(url, return_binary=True) to RenderApi
1 parent 61cb1e0 commit 797017b

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ url = "https://www.sec.gov/Archives/edgar/data/1662684/000110465921082303/tm2119
163163
filing = renderApi.get_filing(url)
164164

165165
print(filing)
166+
167+
# for non-text data, such as a PDF files or an images,
168+
# use get_file() and set `return_binary=True` to get the binary data
169+
pdf_file_url = "https://www.sec.gov/Archives/edgar/data/1798925/999999999724004095/filename1.pdf"
170+
binary_data = renderApi.get_file(pdf_file_url, return_binary=True)
171+
172+
with open("filename.pdf", "wb") as f:
173+
f.write(binary_data)
166174
```
167175

168176
> See the documentation for more details: https://sec-api.io/docs/sec-filings-render-api

examples.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,17 @@
3030
url="https://www.sec.gov/ix?doc=/Archives/edgar/data/1320695/000132069520000148/ths12-31x201910krecast.htm"
3131
)
3232
33-
print(filing_data)
33+
print(filing_data[:300])
34+
35+
# for non-text data, such as a PDF files or an images,
36+
# use get_file() and set `return_binary=True` to get the binary data
37+
pdf_file_url = (
38+
"https://www.sec.gov/Archives/edgar/data/1798925/999999999724004095/filename1.pdf"
39+
)
40+
binary_data = renderApi.get_file(pdf_file_url, return_binary=True)
41+
42+
with open("filename.pdf", "wb") as f:
43+
f.write(binary_data)
3444
# """
3545

3646
#

sec_api/index.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def __init__(self, api_key, proxies=None):
105105
self.api_endpoint = render_api_endpoint
106106
self.proxies = proxies if proxies else {}
107107

108-
def get_filing(self, url, as_pdf=False):
108+
def get_filing(self, url, return_binary=False):
109109
response = {}
110110
# remove "ix?doc=/" from URL
111111
filename = re.sub(r"ix\?doc=/", "", url)
@@ -116,7 +116,27 @@ def get_filing(self, url, as_pdf=False):
116116
for x in range(3):
117117
response = requests.get(_url, proxies=self.proxies)
118118
if response.status_code == 200:
119-
return response.text
119+
return response.text if not return_binary else response.content
120+
elif response.status_code == 429:
121+
# wait 500 * (x + 1) milliseconds and try again
122+
time.sleep(0.5 * (x + 1))
123+
else:
124+
handle_api_error(response)
125+
else:
126+
handle_api_error(response)
127+
128+
def get_file(self, url, return_binary=False):
129+
response = {}
130+
# remove "ix?doc=/" from URL
131+
filename = re.sub(r"ix\?doc=/", "", url)
132+
filename = re.sub(r"https://www.sec.gov/Archives/edgar/data", "", filename)
133+
_url = self.api_endpoint + filename + "?token=" + self.api_key
134+
135+
# use backoff strategy to handle "too many requests" error.
136+
for x in range(3):
137+
response = requests.get(_url, proxies=self.proxies)
138+
if response.status_code == 200:
139+
return response.text if not return_binary else response.content
120140
elif response.status_code == 429:
121141
# wait 500 * (x + 1) milliseconds and try again
122142
time.sleep(0.5 * (x + 1))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="sec-api",
8-
version="1.0.22",
8+
version="1.0.23",
99
author="SEC API",
1010
author_email="support@sec-api.io",
1111
description="SEC EDGAR Filings API",

0 commit comments

Comments
 (0)