diff options
author | Dennis Eriksen <d@ennis.no> | 2023-11-09 14:11:35 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-11-09 14:11:35 +0100 |
commit | e8e33997b360bfee461d5939b7c9fef5f3de6e8c (patch) | |
tree | 8f61e55c69edbd6abf140aba462bf535c83e1f22 /scripts/nb.py | |
parent | added new function to get env (diff) | |
download | energyscripts-e8e33997b360bfee461d5939b7c9fef5f3de6e8c.tar.gz |
optimize and simplify
- use more fstrings
- use exceptions
- don't print "oh lol"
- drop tempfiles
Diffstat (limited to 'scripts/nb.py')
-rw-r--r-- | scripts/nb.py | 32 |
1 files changed, 11 insertions, 21 deletions
diff --git a/scripts/nb.py b/scripts/nb.py index 6afb74e..fad142a 100644 --- a/scripts/nb.py +++ b/scripts/nb.py @@ -5,6 +5,7 @@ import csv import sys import tempfile from datetime import datetime, timedelta +from io import StringIO import common import requests @@ -27,39 +28,28 @@ temp = tempfile.NamedTemporaryFile() ### Get the data try: - url = apiUrl + "&startPeriod=" + startTime + "&endPeriod=" + endTime + url = f"{apiUrl}&startPeriod={startTime}&endPeriod={endTime}" response = requests.get(url, timeout=10) if response.status_code != 200: - print(response.status_code) - print("Oh shit") + print(f"Request Error, Status code: {response.status_code}") response.raise_for_status() - with open(temp.name, "w", encoding="utf-8") as fd: - fd.write(response.text) + data = StringIO(response.text) except requests.exceptions.RequestException as e: - print("oh lol") + print("Error: %s", e) sys.exit(e) +values = [] +csvReader = csv.DictReader(data, delimiter=";") +for item in csvReader: + values.append( + (item["TIME_PERIOD"], item["BASE_CUR"], item["QUOTE_CUR"], item["OBS_VALUE"]) + ) ### insert data into database -values = [] -with open(temp.name, encoding="utf-8") as csvfile: - csvReader = csv.DictReader(csvfile, delimiter=";") - - for item in csvReader: - values.append( - ( - item["TIME_PERIOD"], - item["BASE_CUR"], - item["QUOTE_CUR"], - item["OBS_VALUE"], - ) - ) - -temp.close() # SQL sql = """INSERT INTO nbex |