Browse Source

simplify exception handling

master
rscmbbng 5 years ago
parent
commit
72301e2a89
  1. 16
      slog.py

16
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))

Loading…
Cancel
Save