-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (55 loc) · 1.82 KB
/
main.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
import os
import ctypes
import time as sleeptime
from datetime import datetime, time
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from avanza import Avanza
import config
# Account details
avanza = Avanza(
{
"username": config.avanzaUsername,
"password": config.avanzaPassword,
"totpSecret": config.totpSecret,
}
)
def in_market_hours(market_open, market_close):
current_time = datetime.now().time()
return current_time >= market_open and current_time <= market_close
while True:
try:
# Only execute API calls on weekdays (to minimize load on API)
if datetime.today().weekday() < 5 and in_market_hours(
time(9, 00), time(22, 00)
):
overview = avanza.get_account_overview(config.accountID)
ownedCapital = str(int(overview["ownCapital"]))
balanceText = ownedCapital + " sek"
# Generate image
W, H = (1920, 1080)
img = Image.new("RGBA", (W, H), config.backgroundColor)
draw = ImageDraw.Draw(img)
myFont = ImageFont.truetype("font.ttf", size=75)
w, h = draw.textsize(balanceText, font=myFont)
# Draws centered text
draw.text(
((W - w) / 2, (H - h) / 2),
balanceText,
font=myFont,
fill="white",
align="center",
)
img.save("balance.png", "PNG")
# Set as wallpaper
ctypes.windll.user32.SystemParametersInfoW(
20,
0,
os.getcwd() + "\\balance.png",
0,
)
# Sleeps for x seconds before looping again
sleeptime.sleep(config.updateIntervalSeconds)
except Exception as e:
print("Error occurred: ", e)