changed to sqlite
This commit is contained in:
parent
b816539c60
commit
8e3b7b254a
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
__pychache__
|
||||
solar_server_stats/*
|
||||
*.json
|
||||
*.db
|
||||
|
45
slog.py
45
slog.py
@ -1,52 +1,55 @@
|
||||
import urllib.request, json, datetime,os
|
||||
from urllib.error import URLError, HTTPError
|
||||
from tinydb import TinyDB, Query
|
||||
import sqlite3
|
||||
|
||||
folder = 'solar_server_stats'
|
||||
now = datetime.datetime.now()
|
||||
day = now.strftime('%F')
|
||||
time = now.strftime('%H:%M')
|
||||
|
||||
url = 'https://solar.lowtechmagazine.com/api/stats.json'
|
||||
|
||||
if not os.path.exists(folder):
|
||||
os.mkdir(folder)
|
||||
|
||||
db = TinyDB(os.path.join(folder,day+'-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)
|
||||
""")
|
||||
db.commit()
|
||||
else:
|
||||
db = sqlite3.connect('stats.db')
|
||||
c = db.cursor()
|
||||
|
||||
|
||||
|
||||
request = urllib.request.Request(url)
|
||||
|
||||
stats = {}
|
||||
|
||||
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
|
||||
stats['server'] = None
|
||||
server = 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
|
||||
stats['server'] = None
|
||||
server = None
|
||||
pass
|
||||
|
||||
|
||||
else:
|
||||
# everything is fine
|
||||
data = response.read().decode('utf-8')
|
||||
stats['server'] = json.loads(data)
|
||||
server = data
|
||||
error = None
|
||||
|
||||
if error:
|
||||
stats['error'] = error.args[1]
|
||||
else:
|
||||
stats['error'] = error
|
||||
error = error.args[1]
|
||||
|
||||
stats['day'] = day
|
||||
stats['time'] = now.strftime('%H:%M')
|
||||
|
||||
db.insert(stats)
|
||||
|
||||
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, time, error, server))
|
||||
db.commit()
|
||||
db.close()
|
Loading…
Reference in New Issue
Block a user