Last active
May 5, 2020 08:42
-
-
Save MarkusHackspacher/dad944e86f1e5c9cabea74adbe3bbe82 to your computer and use it in GitHub Desktop.
look for pressure_at_sealevel in http://api.luftdaten.info/static/v1/data.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
license CC BY-SA 3.0 | |
http://creativecommons.org/licenses/by-sa/3.0/deed.de | |
""" | |
import datetime | |
import json | |
import requests | |
print('Start') | |
def requestWeb(): | |
r = requests.get('http://api.luftdaten.info/static/v1/data.json') | |
with open('data.json', 'wb') as json_file: | |
json_file.writelines(r) | |
print('Daten heruntergeladen und als data.json gespeichert') | |
return r.json() | |
try: | |
with open('data.json') as json_file: | |
luftdaten = json.load(json_file) | |
except FileNotFoundError: | |
print("Datei nicht gefunden, neue Daten werden heruntergeladen!") | |
luftdaten = requestWeb() | |
timestamp = datetime.datetime.strptime(luftdaten[0]['timestamp'], "%Y-%m-%d %H:%M:%S") | |
difference = datetime.datetime.now()-timestamp | |
if ((difference.days * 24 * 60 * 60) + difference.seconds) > (6 * 60 * 60): | |
print("Zeitstempel alt {}, neue Daten werden heruntergeladen!".format(difference)) | |
luftdaten = requestWeb() | |
timestamp = datetime.datetime.strptime(luftdaten[0]['timestamp'], "%Y-%m-%d %H:%M:%S") | |
for id in luftdaten: | |
foundPressure = False | |
if (48.5 < float(id['location']['latitude']) < 49 | |
and 10.0 < float(id['location']['longitude']) < 10.2): | |
for sensordata in id["sensordatavalues"]: | |
if sensordata["value_type"] == 'pressure_at_sealevel': | |
foundPressure = True | |
if foundPressure: | |
place = 'lat={:6.3f}, lon={:6.3f}, alt={:4.1f}'.format( | |
float(id['location']['latitude']), | |
float(id['location']['longitude']), | |
float(id['location']['altitude'])) | |
time ='{}'.format(id['timestamp']) | |
print('ID:{:5} {}'.format( | |
id['sensor']['id'], place)) | |
for sensordata in id["sensordatavalues"]: | |
if sensordata["value_type"] == 'temperature': | |
print('Temperatur {:8.1f}°C'.format( | |
float(sensordata['value']))) | |
if sensordata["value_type"] == 'pressure' and False: | |
print('Luftdruck gemessen {:9.2f}'.format( | |
float(sensordata['value'])/100)) | |
if sensordata["value_type"] == 'pressure_at_sealevel': | |
print('Luftdruck Meerhöhe {:9.2f}'.format( | |
float(sensordata['value'])/100)) | |
last = id | |
if sensordata["value_type"] == 'humidity': | |
print('Luftfeuchte {:8.1f}%'.format( | |
float(sensordata['value']))) | |
print('##########') | |
print(last) | |
print('Daten von {}'.format(timestamp)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment