simplify exception handling

This commit is contained in:
rscmbbng 2018-12-03 05:04:20 +01:00
parent 2889c32576
commit 72301e2a89

16
slog.py
View File

@ -7,7 +7,7 @@ now = datetime.datetime.now()
day = now.strftime('%F') day = now.strftime('%F')
the_time = now.strftime('%H:%M') 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'): if not os.path.exists('stats.db'):
db = sqlite3.connect('stats.db') db = sqlite3.connect('stats.db')
@ -18,14 +18,16 @@ else:
db = sqlite3.connect('stats.db') db = sqlite3.connect('stats.db')
c = db.cursor() 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: except Exception as e:
error = r.status_code error = str(e)
data = None data = None
else:
error = None
data = r.text
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data)) c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data))