aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/nb.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/nb.py')
-rw-r--r--scripts/nb.py32
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