Skip to content

Commit cb5ec83

Browse files
committed
version 1.0.50: Support for Insider-transactions and Earnings-Dates API
1 parent 0d55a16 commit cb5ec83

File tree

8 files changed

+95
-24
lines changed

8 files changed

+95
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## v1.0.51 (2021-08-11)
3+
Insider-transactions and Earnings-Dates API support:
4+
list the available files and download them to automate your process
25

36
## v1.0.49 (2021-07-16)
47
Text analytics: support for uploading via source_url

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ include CONTRIBUTING.rst
33
include HISTORY.rst
44
include LICENSE
55
include README.rst
6-
include ravenpackapi/upload/*
76

7+
recursive-include ravenpackapi *
88
recursive-include tests *
99
recursive-exclude * __pycache__
1010
recursive-exclude * .cache

ravenpackapi/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from ravenpackapi import Dataset
55
from ravenpackapi.exceptions import get_exception
6+
from ravenpackapi.key_events.module import KeyEventsApi
67
from ravenpackapi.models.dataset_list import DatasetList
78
from ravenpackapi.models.job import Job
89
from ravenpackapi.models.mapping import RPMappingResults
@@ -14,7 +15,7 @@
1415
from ravenpackapi.utils.dynamic_sessions import DynamicSession
1516

1617
_VALID_METHODS = ('get', 'post', 'put', 'delete', 'patch')
17-
VERSION = '1.0.50'
18+
VERSION = '1.0.51'
1819

1920
logger = logging.getLogger("ravenpack.core")
2021

@@ -46,6 +47,8 @@ def __init__(self, api_key=None):
4647
self.log_curl_commands = True
4748
self.session = DynamicSession()
4849
self.upload = UploadApi(self)
50+
self.insider_trasactions = KeyEventsApi(self, "insider-transactions")
51+
self.earnings_dates = KeyEventsApi(self, "earnings-dates")
4952

5053
@property
5154
def headers(self):

ravenpackapi/key_events/__init__.py

Whitespace-only changes.

ravenpackapi/key_events/module.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
3+
from ravenpackapi.utils.file_saving import save_stream_response
4+
5+
6+
class KeyEventsApi(object):
7+
def __init__(self, api, product):
8+
self.api = api
9+
self.product = product
10+
11+
def _request_list(self, file_type):
12+
13+
response = self.api.request(
14+
"/%s/history/%s/" % (self.product, file_type)
15+
)
16+
data = response.json()
17+
return data
18+
19+
def list_yearly_archives(self):
20+
return self._request_list('yearly')
21+
22+
def list_daily_files(self):
23+
return self._request_list('daily')
24+
25+
def list_reference_files(self):
26+
return self._request_list('reference')
27+
28+
def download_file(self, file_id, filename=None):
29+
if file_id.startswith("/"):
30+
url = file_id
31+
if filename is None:
32+
filename = os.path.basename(file_id)
33+
else:
34+
# the id is the filename - we can get the url from there
35+
if file_id.endswith(".zip"):
36+
file_type = "yearly"
37+
elif "-reference." in file_id:
38+
file_type = "reference"
39+
else:
40+
file_type = "daily"
41+
url = "/%(product)s/file/%(file_type)s/%(id)s" % {
42+
"product": self.product,
43+
"file_type": file_type,
44+
"id": file_id
45+
}
46+
if filename is None:
47+
filename = file_id
48+
response = self.api.session.get(self.api._BASE_URL + url,
49+
headers=self.api.headers,
50+
stream=True,
51+
**self.api.common_request_params
52+
)
53+
save_stream_response(response, filename=filename)

ravenpackapi/models/job.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
APIException,
77
DataFileTimeout,
88
JobNotProcessing)
9-
from ravenpackapi.util import to_curl, parse_csv_line
9+
from ravenpackapi.util import parse_csv_line
10+
from ravenpackapi.utils.file_saving import save_stream_response
1011

1112
logger = logging.getLogger(__name__)
1213

@@ -104,26 +105,16 @@ def wait_for_completion(self, timeout_seconds=None):
104105
def save_to_file(self, filename):
105106
api = self.api
106107
job = self # just to be clear
107-
with open(filename, 'wb') as output:
108-
job.wait_for_completion()
109-
logger.info(u"Writing to %s" % filename)
110-
111-
# this is a different request than the normal API
112-
# streaming the file in chunks
113-
response = api.session.get(job.url,
114-
headers=api.headers,
115-
stream=True,
116-
**api.common_request_params
117-
)
118-
if response.status_code != 200:
119-
logger.error("Error calling the API, we tried: %s" % to_curl(response.request))
120-
raise APIException(
121-
'Got an error {status}: body was \'{error_message}\''.format(
122-
status=response.status_code, error_message=response.text
123-
), response=response)
124-
for chunk in response.iter_content(chunk_size=self._CHUNK_SIZE):
125-
if chunk:
126-
output.write(chunk)
108+
job.wait_for_completion()
109+
110+
# this is a different request than the normal API
111+
# streaming the file in chunks
112+
response = api.session.get(job.url,
113+
headers=api.headers,
114+
stream=True,
115+
**api.common_request_params
116+
)
117+
save_stream_response(response, filename, chunk_size=self._CHUNK_SIZE)
127118

128119
@api_method
129120
def iterate_results(self, include_headers=False):

ravenpackapi/utils/file_saving.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import logging
2+
3+
from ravenpackapi.exceptions import APIException
4+
from ravenpackapi.util import to_curl
5+
6+
logger = logging.getLogger(__name__)
7+
8+
9+
def save_stream_response(response, filename, chunk_size=8192):
10+
if response.status_code != 200:
11+
logger.error("Error calling the API, we tried: %s" % to_curl(response.request))
12+
raise APIException(
13+
'Got an error {status}: body was \'{error_message}\''.format(
14+
status=response.status_code, error_message=response.text
15+
), response=response)
16+
17+
logger.info(u"Writing to %s" % filename)
18+
with open(filename, "wb") as output:
19+
for chunk in response.iter_content(chunk_size=chunk_size):
20+
if chunk:
21+
output.write(chunk)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '1.0.50'
3+
VERSION = '1.0.51'
44

55
with open('README.rst') as readme_file:
66
readme = readme_file.read()

0 commit comments

Comments
 (0)