This commit is contained in:
rscmbbng 2018-12-02 19:23:57 +01:00
parent b4ff2f77a5
commit 7912ef9f6b

25
slog.py
View File

@ -1,4 +1,4 @@
import urllib.request, json, datetime,os
import urllib.request, datetime, os, time
from urllib.error import URLError, HTTPError
import sqlite3
@ -8,7 +8,6 @@ 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()
@ -19,37 +18,35 @@ 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)
# print('The server couldn\'t fulfill the request.')
# print('Error code: ', e.code)
error = e.code
server = None
data = None
pass
except URLError as e:
print('We failed to reach a server.')
print('Reason: ', e.reason)
# print('We failed to reach a server.')
# print('Reason: ', e.reason)
error = e.reason
server = None
data = None
pass
else:
# everything is fine
data = response.read().decode('utf-8')
server = data
error = None
if error:
error = error.args[1]
print(server)
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, time, error, server))
time.sleep(1)
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, time, error, data))
time.sleep(1)
db.commit()
time.sleep(1)
db.close()