From 72301e2a89541b4d8b516486be1868901d378a5a Mon Sep 17 00:00:00 2001 From: rscmbbng Date: Mon, 3 Dec 2018 05:04:20 +0100 Subject: [PATCH] simplify exception handling --- slog.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/slog.py b/slog.py index 98ae414..703a263 100644 --- a/slog.py +++ b/slog.py @@ -7,7 +7,7 @@ now = datetime.datetime.now() day = now.strftime('%F') the_time = now.strftime('%H:%M') -url = 'https://solar.lowtechmagazine.com/api/stats.json' +url = 'https://solar.lowechmagazine.com/api/stats.json' if not os.path.exists('stats.db'): db = sqlite3.connect('stats.db') @@ -18,14 +18,16 @@ else: db = sqlite3.connect('stats.db') c = db.cursor() -r = requests.get(url) +try: + r = requests.get(url) + r.raise_for_status() # in case of 404 + if r.status_code == requests.codes.ok: + data = r.text + error = None -if not r.status_code == 200: - error = r.status_code +except Exception as e: + error = str(e) data = None -else: - error = None - data = r.text c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data))