Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login prompt wrapper and DeGiro constructor function #28

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
36 changes: 33 additions & 3 deletions degiroapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class DeGiro:
__LOGIN_URL = 'https://trader.degiro.nl/login/secure/login'
__LOGIN_TOTP_URL = 'https://trader.degiro.nl/login/secure/login/totp'
__CONFIG_URL = 'https://trader.degiro.nl/login/secure/config'

__LOGOUT_URL = 'https://trader.degiro.nl/trading/secure/logout'
Expand All @@ -18,6 +19,7 @@ class DeGiro:
__PRODUCT_INFO_URL = 'https://trader.degiro.nl/product_search/secure/v5/products/info'
__TRANSACTIONS_URL = 'https://trader.degiro.nl/reporting/secure/v4/transactions'
__ORDERS_URL = 'https://trader.degiro.nl/reporting/secure/v4/order-history'
__ACCOUNT_URL = 'https://trader.degiro.nl/reporting/secure/v6/accountoverview'

__PLACE_ORDER_URL = 'https://trader.degiro.nl/trading/secure/v5/checkOrder'
__ORDER_URL = 'https://trader.degiro.nl/trading/secure/v5/order/'
Expand All @@ -34,15 +36,24 @@ class DeGiro:
client_info = any
confirmation_id = any

def login(self, username, password):
def __init__(self, username=None, password=None, totp=None):
if username: # Login prompt
self.login_prompt(username=username, password=password, totp=totp)

def login(self, username, password, totp=None):
login_payload = {
'username': username,
'password': password,
'isPassCodeReset': False,
'isRedirectToMobile': False
}
login_response = self.__request(DeGiro.__LOGIN_URL, None, login_payload, request_type=DeGiro.__POST_REQUEST,
error_message='Could not login.')
if totp is not None:
login_payload["oneTimePassword"] = totp
login_response = self.__request(DeGiro.__LOGIN_TOTP_URL, None, login_payload, request_type=DeGiro.__POST_REQUEST,
error_message='Could not login.')
else:
login_response = self.__request(DeGiro.__LOGIN_URL, None, login_payload, request_type=DeGiro.__POST_REQUEST,
error_message='Could not login.')
self.session_id = login_response['sessionId']
client_info_payload = {'sessionId': self.session_id}
client_info_response = self.__request(DeGiro.__CLIENT_INFO_URL, None, client_info_payload,
Expand All @@ -59,6 +70,15 @@ def login(self, username, password):

return client_info_response

def login_prompt(self, username=None, password=None, totp=None):
import getpass

if not username: username = input("Username: ")
if not password: password = getpass.getpass("Password:")
if not totp: totp = getpass.getpass("totp (Leave empty if none):")

return self.login(username, password, totp or None)

def logout(self):
logout_payload = {
'intAccount': self.client_info.account_id,
Expand Down Expand Up @@ -127,6 +147,16 @@ def transactions(self, from_date, to_date, group_transactions=False):
return self.__request(DeGiro.__TRANSACTIONS_URL, None, transactions_payload,
error_message='Could not get transactions.')['data']

def account_overview(self, from_date, to_date):
account_payload = {
'fromDate': from_date.strftime('%d/%m/%Y'),
'toDate': to_date.strftime('%d/%m/%Y'),
'intAccount': self.client_info.account_id,
'sessionId': self.session_id
}
return self.__request(DeGiro.__ACCOUNT_URL, None, account_payload,
error_message='Could not get account overview.')['data']

def orders(self, from_date, to_date, not_executed=None):
orders_payload = {
'fromDate': from_date.strftime('%d/%m/%Y'),
Expand Down
15 changes: 15 additions & 0 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
degiro = degiroapi.DeGiro()
degiro.login("username", "password")

# Equivalent to:
#degiro = degiroapi.DeGiro("username", "password") # prompts for totp (optional)

# Password can be given dynamically using login_prompt:
# -not necessary to include in .py script
degiro = degiroapi.DeGiro()
degiro.login_prompt("username") # prompts for password and totp (optional)

#Equivalent to:
#degiro = degiroapi.DeGiro("username") # prompts for password and totp (optional)

# To dynamically provide all credentials use:
degiro = degiroapi.DeGiro()
degiro.login_prompt() #prompts for username, password and totp (optional)

# logout
degiro.logout()

Expand Down