rscmbbng
6 years ago
commit
5d435a1e30
2 changed files with 65 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||||
|
|
||||
|
Log availability and stats of <solar.lowtechmagazine.com> |
||||
|
|
||||
|
You need: |
||||
|
python3 |
||||
|
tinydb |
||||
|
|
||||
|
You get: |
||||
|
solar_server_logs/{day}-stats.json |
||||
|
|
||||
|
Run it every 30 mins |
||||
|
???? |
||||
|
Stats |
@ -0,0 +1,52 @@ |
|||||
|
import urllib.request, json, datetime,os |
||||
|
from urllib.error import URLError, HTTPError |
||||
|
from tinydb import TinyDB, Query |
||||
|
|
||||
|
folder = 'solar_server_stats' |
||||
|
now = datetime.datetime.now() |
||||
|
day = now.strftime('%F') |
||||
|
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')) |
||||
|
|
||||
|
|
||||
|
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) |
||||
|
error = e.code |
||||
|
stats['server'] = None |
||||
|
pass |
||||
|
except URLError as e: |
||||
|
print('We failed to reach a server.') |
||||
|
print('Reason: ', e.reason) |
||||
|
error = e.reason |
||||
|
stats['server'] = None |
||||
|
pass |
||||
|
|
||||
|
|
||||
|
else: |
||||
|
# everything is fine |
||||
|
data = response.read().decode('utf-8') |
||||
|
stats['server'] = json.loads(data) |
||||
|
error = None |
||||
|
|
||||
|
if error: |
||||
|
stats['error'] = error.args[1] |
||||
|
else: |
||||
|
stats['error'] = error |
||||
|
|
||||
|
stats['day'] = day |
||||
|
stats['time'] = now.strftime('%H:%M') |
||||
|
|
||||
|
db.insert(stats) |
||||
|
|
Loading…
Reference in new issue