Skip to content

Commit

Permalink
feat: ibkr flex query web API downloader (reds-ibkr-flexquery-download)
Browse files Browse the repository at this point in the history
the new command is: reds-ibkr-flexquery-download
  • Loading branch information
redstreet committed Jul 19, 2024
1 parent 13f0ddc commit c08e6eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions beancount_reds_importers/importers/ibkr/flexquery_download.py
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()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"console_scripts": [
"ofx-summarize = beancount_reds_importers.util.ofx_summarize:summarize",
"bean-download = beancount_reds_importers.util.bean_download:cli",
"reds-ibkr-flexquery-download = beancount_reds_importers.importers.ibkr.flexquery_download:flexquery_download",
]
},
zip_safe=False,
Expand Down

0 comments on commit c08e6eb

Please sign in to comment.