aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/nb.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/nb.py')
-rw-r--r--scripts/nb.py17
1 files changed, 8 insertions, 9 deletions
diff --git a/scripts/nb.py b/scripts/nb.py
index 1901182..7cb753f 100644
--- a/scripts/nb.py
+++ b/scripts/nb.py
@@ -1,15 +1,14 @@
-#!/bin/python3
+#!/usr/bin/env python3
+''' Get exchange rates from nb '''
import os
import sys
import csv
-import json
-import requests
import tempfile
-
from datetime import datetime
from datetime import timedelta
from tzlocal import get_localzone
+import requests
from common import dbi
@@ -31,16 +30,16 @@ temp = tempfile.NamedTemporaryFile()
try:
url = apiUrl + "&startPeriod=" + startTime + "&endPeriod=" + endTime
- response = requests.get(url)
+ response = requests.get(url, timeout=10)
if response.status_code != 200:
print(response.status_code)
print("Oh shit")
response.raise_for_status()
- with open(temp.name,'w') as fd:
+ with open(temp.name,'w', encoding="utf-8") as fd:
fd.write(response.text)
-except Exception as e:
+except requests.exceptions.RequestException as e:
print("oh lol")
sys.exit(e)
@@ -48,7 +47,7 @@ except Exception as e:
### insert data into database
values = []
-with open(temp.name) as csvfile:
+with open(temp.name, encoding="utf-8") as csvfile:
csvReader = csv.DictReader(csvfile, delimiter=';')
for item in csvReader:
@@ -56,4 +55,4 @@ with open(temp.name) as csvfile:
temp.close()
-dbi("INSERT INTO " + pg_table + " VALUES(%s,%s,%s,%s) ON CONFLICT (startdate,base_cur,quote_cur) DO NOTHING", values,verbose=True)
+dbi("INSERT INTO " + pg_table + " VALUES(%s,%s,%s,%s) ON CONFLICT (startdate,base_cur,quote_cur) DO NOTHING", values, verbose=True)