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.

39 lines
857 B

6 years ago
#!/usr/bin/env python3
import requests, datetime, os
import sqlite3, time
6 years ago
now = datetime.datetime.now()
day = now.strftime('%F')
the_time = now.strftime('%H:%M')
6 years ago
url = 'https://solar.lowtechmagazine.com/api/stats.json'
6 years ago
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()
6 years ago
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
6 years ago
except Exception as e:
error = str(e)
6 years ago
data = None
6 years ago
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data))
db.commit()
6 years ago
db.close()