Last active
July 19, 2024 21:56
-
-
Save schlan/f72d823dd5c1c1d19dfd784eb392dded to your computer and use it in GitHub Desktop.
Download Smart Meter stats from https://myaccount.esbnetworks.ie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
from bs4 import BeautifulSoup | |
import re | |
import json | |
from datetime import datetime, timedelta, timezone | |
def load_esb_data(user, password, mpnr, start_date): | |
s = requests.Session() | |
s.headers.update({ | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', | |
}) | |
login_page = s.get('https://myaccount.esbnetworks.ie/', allow_redirects=True) | |
result = re.findall(r"(?<=var SETTINGS = )\S*;", str(login_page.content)) | |
settings = json.loads(result[0][:-1]) | |
s.post( | |
'https://login.esbnetworks.ie/esbntwkscustportalprdb2c01.onmicrosoft.com/B2C_1A_signup_signin/SelfAsserted?tx=' + settings['transId'] + '&p=B2C_1A_signup_signin', | |
data={ | |
'signInName': user, | |
'password': password, | |
'request_type': 'RESPONSE' | |
}, | |
headers={ | |
'x-csrf-token': settings['csrf'], | |
}, | |
allow_redirects=False) | |
confirm_login = s.get( | |
'https://login.esbnetworks.ie/esbntwkscustportalprdb2c01.onmicrosoft.com/B2C_1A_signup_signin/api/CombinedSigninAndSignup/confirmed', | |
params={ | |
'rememberMe': False, | |
'csrf_token': settings['csrf'], | |
'tx': settings['transId'], | |
'p': 'B2C_1A_signup_signin', | |
} | |
) | |
soup = BeautifulSoup(confirm_login.content, 'html.parser') | |
form = soup.find('form', {'id': 'auto'}) | |
s.post( | |
form['action'], | |
allow_redirects=False, | |
data={ | |
'state': form.find('input', {'name': 'state'})['value'], | |
'client_info': form.find('input', {'name': 'client_info'})['value'], | |
'code': form.find('input', {'name': 'code'})['value'], | |
}, | |
) | |
data = s.get('https://myaccount.esbnetworks.ie/datahub/GetHdfContent?mprn=' + mpnr + '&startDate=' + start_date.strftime('%Y-%m-%d')) | |
return data.json() | |
def parse_date(date_str): | |
if len(date_str) == 19: | |
return datetime.strptime(date_str, '%Y-%m-%dT%H:%M:%S') | |
else: | |
dt = datetime.strptime(date_str[:19], '%Y-%m-%dT%H:%M:%S') | |
tz_offset = int(date_str[-6:-3]) | |
tz = timezone(timedelta(hours=tz_offset)) | |
return dt.replace(tzinfo=tz) | |
def load_smart_meter_stats_v2(user, password, mpnr): | |
last_month = datetime.today() - timedelta(days=30) | |
smart_meter_data = load_esb_data(user, password, mpnr, last_month) | |
result = {} | |
for item in smart_meter_data['imports']: | |
# https://www.esbnetworks.ie/help-centre/esb-networks-online-account-faqs/your-energy-consumption | |
result[parse_date(item['x'])] = item['y'] * 0.5 | |
return result | |
mpnr = "" | |
user = "" | |
password = "" | |
print(load_smart_meter_stats_v2(user, password, mpnr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really great!