diff options
Diffstat (limited to 'scripts/elvia.py')
-rw-r--r-- | scripts/elvia.py | 31 |
1 files changed, 11 insertions, 20 deletions
diff --git a/scripts/elvia.py b/scripts/elvia.py index fffb6bf..a680104 100644 --- a/scripts/elvia.py +++ b/scripts/elvia.py @@ -17,42 +17,34 @@ startTime = startTime.isoformat("T") endTime = datetime.now(get_localzone()).isoformat("T") - ### Get the data try: - url = apiUrl + "?startTime=" + startTime + "&endTime=" + endTime + # Query parameters + params = {"startTime": startTime, "endTime": endTime} # Request headers - hdr = { + headers = { "Cache-Control": "no-cache", "Authorization": "Bearer " + apiKey, } - response = requests.get(url, headers=hdr, timeout=10) - if response.status_code != 200: - print(response.status_code) - print("Oh shit") - response.raise_for_status() + response = requests.get(apiUrl, params=params, headers=headers, timeout=10) + response.raise_for_status() except requests.exceptions.RequestException as e: - print("oh lol") sys.exit(e) data = response.json() -print( - "Got " - + str(len(data["meteringpoints"][0]["metervalue"]["timeSeries"])) - + " items from between " - + startTime - + " and " - + endTime -) -### insert data into database +num_items = len(data["meteringpoints"][0]["metervalue"]["timeSeries"]) +print(f"Got {num_items} items from between {startTime} and {endTime}") + +### Insert data into database values = [] + for item in data["meteringpoints"][0]["metervalue"]["timeSeries"]: - # Only deal with verified items. + # Only append verified items if item["verified"]: values.append( ( @@ -65,7 +57,6 @@ for item in data["meteringpoints"][0]["metervalue"]["timeSeries"]: ) ) - # SQL sql = """INSERT INTO elvia VALUES(%s, %s, %s, %s, %s, %s) |