Browse Source

simplify

master
rscmbbng 6 years ago
parent
commit
7912ef9f6b
  1. 25
      slog.py

25
slog.py

@ -1,4 +1,4 @@
import urllib.request, json, datetime,os import urllib.request, datetime, os, time
from urllib.error import URLError, HTTPError from urllib.error import URLError, HTTPError
import sqlite3 import sqlite3
@ -8,7 +8,6 @@ time = now.strftime('%H:%M')
url = 'https://solar.lowtechmagazine.com/api/stats.json' url = 'https://solar.lowtechmagazine.com/api/stats.json'
if not os.path.exists('stats.db'): if not os.path.exists('stats.db'):
db = sqlite3.connect('stats.db') db = sqlite3.connect('stats.db')
c = db.cursor() c = db.cursor()
@ -19,37 +18,35 @@ else:
db = sqlite3.connect('stats.db') db = sqlite3.connect('stats.db')
c = db.cursor() c = db.cursor()
request = urllib.request.Request(url) request = urllib.request.Request(url)
try: try:
response = urllib.request.urlopen(request) response = urllib.request.urlopen(request)
except HTTPError as e: except HTTPError as e:
print('The server couldn\'t fulfill the request.') # print('The server couldn\'t fulfill the request.')
print('Error code: ', e.code) # print('Error code: ', e.code)
error = e.code error = e.code
server = None data = None
pass pass
except URLError as e: except URLError as e:
print('We failed to reach a server.') # print('We failed to reach a server.')
print('Reason: ', e.reason) # print('Reason: ', e.reason)
error = e.reason error = e.reason
server = None data = None
pass pass
else: else:
# everything is fine # everything is fine
data = response.read().decode('utf-8') data = response.read().decode('utf-8')
server = data
error = None error = None
if error: if error:
error = error.args[1] error = error.args[1]
time.sleep(1)
print(server) c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, time, error, data))
c.execute("""INSERT INTO stats VALUES(?,?,?,?)""", (day, time, error, server)) time.sleep(1)
db.commit() db.commit()
time.sleep(1)
db.close() db.close()

Loading…
Cancel
Save