from influxdb_client import InfluxDBClient, Point from influxdb_client.client.write_api import SYNCHRONOUS import requests from datetime import datetime, timedelta import pytz # Fetch data from NOAA METAR API response = requests.get('https://api.weather.gov/stations/KMIB/observations/latest') data = response.json() # Extract relevant information metar_data = data # Accessing the "id" key at the top level station_id = metar_data["id"] # Convert Zulu time to Central Time zulu_time = datetime.fromisoformat(metar_data["properties"]["timestamp"].replace("Z", "+00:00")) zulu_time = zulu_time.replace(tzinfo=pytz.utc) central_timezone = pytz.timezone('America/Chicago') central_time = zulu_time.astimezone(central_timezone) # Calculate data age current_time = datetime.now(tz=pytz.utc) current_time = current_time.astimezone(central_timezone) data_age = (current_time - central_time).total_seconds() / 60 # Convert seconds to minutes # Prepare data for InfluxDB measurement = 'metar_station' time = current_time.isoformat() # Create InfluxDB client client = InfluxDBClient(url="IP:PORT", token="TOKEN", org="ORG") # Create write API write_api = client.write_api(write_options=SYNCHRONOUS) # Define tags and fields tags = {"id": station_id} fields = { "temp": float(metar_data["properties"]["temperature"]["value"]), } # Write data to InfluxDB point = Point(measurement).time(time).tag("id", station_id) for field, value in fields.items(): point.field(field, value) write_api.write("BUCKET", "ORG", point)