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.
 

53 lines
1.2 KiB

#!/usr/bin/env python3
import urllib.request, datetime, os, time
from urllib.error import URLError, HTTPError
import sqlite3
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()
request = urllib.request.Request(url)
try:
response = urllib.request.urlopen(request)
except HTTPError as e:
# print('The server couldn\'t fulfill the request.')
# print('Error code: ', e.code)
error = e.code
data = None
pass
except URLError as e:
# print('We failed to reach a server.')
# print('Reason: ', e.reason)
error = e.reason
data = None
pass
else:
# everything is fine
data = response.read().decode('utf-8')
error = None
if error:
error = error.args[1]
print(day,the_time,error, data)
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, the_time, error, data))
db.commit()
db.close()