-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ibkr flex query web API downloader (reds-ibkr-flexquery-download)
the new command is: reds-ibkr-flexquery-download
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
beancount_reds_importers/importers/ibkr/flexquery_download.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
"""IBKR Flex Query Downloader""" | ||
|
||
import requests | ||
import click | ||
|
||
@click.command() | ||
@click.argument('token', required=True) | ||
@click.argument('query_id', required=True) | ||
def flexquery_download(token, query_id): | ||
url = "https://gdcdyn.interactivebrokers.com/Universal/servlet/FlexStatementService.SendRequest" | ||
|
||
# Request Flex Query | ||
request_payload = { | ||
"v": "3", | ||
"t": token, | ||
"q": query_id | ||
} | ||
|
||
response = requests.post(url, data=request_payload) | ||
|
||
if response.status_code == 200: | ||
request_id = response.text.split("<ReferenceCode>")[1].split("</ReferenceCode>")[0] | ||
# print(f"Request ID: {request_id}") | ||
|
||
# Construct URL to get the query result | ||
result_url = f"https://gdcdyn.interactivebrokers.com/Universal/servlet/FlexStatementService.GetStatement?q={request_id}&t={token}&v=3" | ||
|
||
result_response = requests.get(result_url) | ||
|
||
if result_response.status_code == 200: | ||
print(result_response.text) | ||
else: | ||
print(f"Failed to get the query result. Status Code: {result_response.status_code}") | ||
return None | ||
else: | ||
print(f"Failed to request the query. Status Code: {response.status_code}") | ||
return None | ||
|
||
if __name__ == '__main__': | ||
flexquery_download() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters