From 3fdaf913e397a2368d4d4fcbef1f68a8c154fb0b Mon Sep 17 00:00:00 2001 From: Elias Kamenka Date: Tue, 3 Jun 2025 18:42:13 +0000 Subject: [PATCH] Initial commit --- IneraScraper.conf | 6 ++++ main.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 IneraScraper.conf create mode 100644 main.py diff --git a/IneraScraper.conf b/IneraScraper.conf new file mode 100644 index 0000000..5f12241 --- /dev/null +++ b/IneraScraper.conf @@ -0,0 +1,6 @@ +[General] +endpoint = https://portail.inera.ch/data/getData.php + +[Cookies] +# Add your cookie 'selfcare' cookie here +auth = \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..869d20e --- /dev/null +++ b/main.py @@ -0,0 +1,71 @@ +import requests +import json +import configparser +from datetime import datetime, timedelta + +def get_timestamps(hours_ago=1): + """ + Calculate debut and fin timestamps for the specified number of hours ago. + Returns timestamps in milliseconds as strings. + fin = current time + debut = current time - hours_ago + """ + end_time = datetime.now() + start_time = end_time - timedelta(hours=hours_ago) + + # Convert to milliseconds timestamp strings + debut = str(int(start_time.timestamp() * 1000)) + fin = str(int(end_time.timestamp() * 1000)) + + return debut, fin + +def read_latest_data(jsonData): + """ + Gets the latest data points from the json + """ + return jsonData['dataProvider'][-1] # Get the last entry + +def get_data(time_range=1): + """ + Get the data + time_range is used to determine the time range from which data is being requested. The time range is the current time - time_range + """ + url = config['General']['endpoint'] + + cookies = { + "selfcare": config['Cookies']['auth'] + } + + # Get timestamps for last 24 hours + debut, fin = get_timestamps(time_range) + + data = { + "data": "courbeDeCharge", + "communaute": "5213", + "appartement": "4783", + "debut": debut, + "fin": fin + } + + response = requests.post( + url, + cookies=cookies, + data=data, + verify=True # SSL verification + ) + + # Get the latest value from the response + response_data = response.json() + + return response_data + +if __name__ == "__main__": + + # Read configuration + config = configparser.ConfigParser() + config.read('IneraScraper.conf') + + # Get data and latest value + latest = read_latest_data(get_data()) + + print(f"Latest reading - Time: {latest['date']}, PV: {latest['pv']}kW, Grid: {latest['grd']}kW, Total: {latest['total']}kW, Autoconso: {latest['autoconso']}%")