-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkWikifolio.py
131 lines (93 loc) · 4.32 KB
/
checkWikifolio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from logger import logger, CPrint
import requests
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
import re
import json
from datetime import datetime
from pathlib import Path
#import databaseHandling
class CheckWikifolio():
'''Class to be instantiated to check current account balance'''
def __init__(self, session, symbol):
self.session = session
self.todays_date = datetime.now().strftime('%Y-%m-%d')
self.filename = Path(f'daily_trades/{self.todays_date}')
Path('daily_trades').mkdir(parents=True, exist_ok=True)
self.symbol = symbol
self.url = f'https://www.wikifolio.com/api/wikifolio/{symbol}/portfolio'
self.item_url = f'https://www.wikifolio.com/api/wikifolio/{symbol}/portfolio?country=de&language=de'
def check_balance(self):
'''returns a dictionary "values", containing both the total Portfolio Amount and the free available cash amount'''
m1_start = f'Checking Account balance for {self.url} ...'
CPrint.color('n', m1_start)
logger.info(m1_start)
try:
r = self.session.get(self.url).text
content = BeautifulSoup(r,'xml')
#find Portfolio's Total Value (typically last Element of XML tree)
totalValue = str(content.findAll('TotalValue'))
x = totalValue.split('>')[-2:][0].split("<")[0]
totalValueFloat = float(x)
#find Portfolios free available Cash (last element before Total Value)
cashValue = str(content.findAll('Value')[-1])
y = cashValue.split('>')[-2:][0].split("<")[0]
totalValueCashFloat = float(y)
datum = datetime.today().astimezone()
values = {}
values['total'] = totalValueFloat
values['cash'] = totalValueCashFloat
#databaseHandling.appendTableWithPortfolioValue([[datum], [amount]])
totalValueFloatText = f'Total portfolio value: {totalValueFloat} EUR.'
logger.info(totalValueFloatText)
CPrint.color('g', totalValueFloatText)
totalValueCashFloatText = f'Free cash amount: {totalValueCashFloat} EUR.'
logger.info(totalValueCashFloatText)
CPrint.color('g', totalValueCashFloatText)
return values
except Exception as e:
CPrint.color('r', e)
logger.info(f'Error: {e}')
def get_items(self):
'''returns a dictionary containing all portfolio items (ISIN: QUANTITY)'''
m1_start = f'Scanning current Portfolio items for {self.url}...'
CPrint.color('n', m1_start)
logger.info(m1_start)
try:
r = self.session.get(self.item_url).text
soup = BeautifulSoup(r, 'lxml-xml')
soupString = str(soup.encode('ascii'))
blob = re.split('[< >]', soupString)
blob_len = len(blob)
i = 0
isin_list = []
quan_list = []
for index, elem in enumerate(blob):
if elem == 'Isin':
isin_list.append(blob[index+1])
for index, elem in enumerate(blob):
if elem == '/WikifolioDetailPortfolioItemModel':
quantity = blob[index-3].strip()
quantity = float(quantity)
quan_list.append(quantity)
portfolio_items = dict(zip(isin_list, quan_list))
m1_success = f'Successfully retrieved all Portfolio Items: Items \n {portfolio_items}'
CPrint.color('g', m1_success)
logger.info(m1_success)
return portfolio_items
except Exception as e:
CPrint.color('r', e)
logger.info(f'Error: {e}')
if __name__ == "__main__":
symbol = 'WF50060055'
from activateSession import SessionActivator
from credentials import credentials
cre = credentials()
sessionActivator = SessionActivator(cre)
returnValue = sessionActivator.activateSession()
session = returnValue['session']
CPrint.color('i', "Testing module checkAccountBalance...")
accountBalanceChecker = CheckAccountBalance(session, symbol)
accountBalanceChecker.check_balance()
accountBalanceChecker.get_items()
CPrint.color('i', "Finished testing checkAccountBalance!")