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']}%")