logging availability and stats of solar.lowtechmagazine.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
857 B

#!/usr/bin/env python3
import requests, datetime, os
import sqlite3, time
now = datetime.datetime.now()
day = now.strftime('%F')
the_time = now.strftime('%H:%M')
url = 'https://solar.lowtechmagazine.com/api/stats.json'
if not os.path.exists('stats.db'):
db = sqlite3.connect('stats.db')
c = db.cursor()
c.execute("""CREATE TABLE stats(date text, time text, error text, server text)
""")
else:
db = sqlite3.connect('stats.db')
c = db.cursor()
time.sleep(5) #otherwise we risk grabbing an empty file
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
except Exception as e:
error = str(e)
data = None
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data))
db.commit()
db.close()