-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
107 lines (88 loc) · 3.54 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
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
from email.mime.image import MIMEImage
from nordpool import elspot # , elbas
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from dateutil import tz
from datetime import date, timedelta
from tabulate import tabulate
import matplotlib.pyplot as plt
from io import BytesIO
import yaml
import os
from dotenv import load_dotenv, find_dotenv
def formatTable(myList, format):
html = """
<html><body>
<p>Nordpool Day-ahead prices:</p>
{table}
<br>
Chart is attached to this email.
</body></html>
"""
html = html.format(
table=tabulate(myList, headers="keys", tablefmt=format, stralign="right", numalign="decimal", floatfmt=".2f")
)
return html
def sendEmail(text, imageFile):
message = MIMEMultipart("alternative", None, [MIMEText(text), MIMEText(text, 'html')])
message['Subject'] = "Nordpool Day-ahead prices for " + (date.today() + timedelta(days=1)).strftime("%d.%m.%Y")
message['From'] = config['email']['from']
message['To'] = config['email']['to']
if config['email']['bcc']:
message['Bcc'] = ', '.join(config['email']['bcc'])
image = MIMEImage(imageFile.getvalue(), name='chart.png')
message.attach(image)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(config['smtp']['server'], config['smtp']['port'], context=context) as server:
server.login(SMTP_LOGIN, SMTP_PASSWORD)
server.send_message(message)
def plot(mylist):
x = [x['start'] for x in mylist]
y = [x['price_kwh'] for x in mylist]
plt.figure(figsize=(config['plot']['width'], config['plot']['height']))
plt.title(config['plot']['title'])
plt.xlabel(config['plot']['xlabel'])
plt.ylabel(config['plot']['ylabel'])
plt.grid(axis='y')
bar_colors = ['red' if x['price_kwh'] > config['plot']['red_threshold'] else 'orange' if x['price_kwh'] > config['plot']['orange_threshold'] else 'blue' if x['price_kwh'] > config['plot']['blue_threshold'] else 'green' for x in mylist]
bars = plt.bar(x, y, label=x, width=0.8, align='center', color=bar_colors)
plt.bar_label(bars, padding=10)
image = BytesIO()
plt.savefig(image, format='png')
return image
# Read configs
config = yaml.safe_load(open("config.yml", encoding='utf-8'))
# Load env variables
load_dotenv(find_dotenv())
SMTP_LOGIN = os.getenv('SMTP_LOGIN')
SMTP_PASSWORD = os.getenv('SMTP_PASSWORD')
# Initialize class for fetching Elspot prices
prices_spot = elspot.Prices()
# Fetch hourly Elspot prices for defined areas
hourly = prices_spot.hourly(areas=['LV', 'FI'])
# Generate empty list
mylist = [{} for sub in range(24)]
# Fill list with data
for i in range(24):
time = hourly["areas"]["LV"]["values"][i]['start']
mylist[i]['date'] = time.astimezone(tz.gettz('Europe/Riga')).strftime("%d.%m.%Y")
mylist[i]['start'] = time.astimezone(tz.gettz('Europe/Riga')).strftime("%H:00")
time = hourly["areas"]["LV"]["values"][i]['end']
mylist[i]['end'] = time.astimezone(tz.gettz('Europe/Riga')).strftime("%H:00")
lv_price = hourly["areas"]["LV"]["values"][i]['value']
fi_price = hourly["areas"]["FI"]["values"][i]['value']
diff = abs(lv_price - fi_price)
our_price = round((diff + f) / 1000, 2)
mylist[i]['LV EUR/kwh'] = round(lv_price / 1000, 2)
mylist[i]['FI EUR/kwh'] = round(fi_price / 1000, 2)
mylist[i]['our price EUR/kwh'] = our_price
# Generate chart
image = plot(mylist)
# Create table
table = formatTable(mylist, "html")
# Send email
sendEmail(table, image)
# Debug
# print(formatTable(mylist, "simple"))