diff --git a/README.md b/README.md new file mode 100644 index 0000000..eeb6609 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Weather Exporter +Pulls weather data from weatherapi.com and provides it in Prometheus-style exporter format over HTTP. + +Used to monitor weather conditions over time. diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..e4b3cce --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,10 @@ +--- +version: '3.7' +services: + weather-exporter: + image: weather-exporter:latest + container_name: weather-exporter + environment: + - WEATHER_LOCATION= + - WEATHER_APIKEY= + restart: unless-stopped diff --git a/main.py b/main.py index 140d520..01eb85a 100644 --- a/main.py +++ b/main.py @@ -12,15 +12,16 @@ from flask import Flask, Response app = Flask(__name__) -def fetch_metoffice_data(location: int, apikey: str) -> dict: +def fetch_weather_data(location: int, apikey: str) -> dict: """ Fetch current data from the Met Office for the provided postcode """ obs_data = requests.get( - f'http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/{location}?' + f'http://api.weatherapi.com/v1/current.json?' f'key={apikey}&' - 'res=hourly' + f'q={location}&' + f'aqi=yes' ) return json.loads(obs_data.content) @@ -31,29 +32,27 @@ def metrics(): """ Output Prometheus-style metrics """ - metoff_apikey = os.environ.get('METOFF_APIKEY') - metoff_location = os.environ.get('METOFF_LOCATION') + apikey = os.environ.get('WEATHER_APIKEY') + location = os.environ.get('WEATHER_LOCATION') - hour_data = fetch_metoffice_data(metoff_location, metoff_apikey,)['SiteRep']['DV'][ - 'Location' - ]['Period'][-1]['Rep'][time.gmtime().tm_hour] + latest_data = fetch_weather_data(location, apikey)['current'] ret_data = [ { 'key': 'sensor_weather_outdoor_temperature_celsius', 'labels': { - 'location': metoff_location, + 'location': location, }, 'type': 'gauge', - 'value': float(hour_data['T']), + 'value': float(latest_data['temp_c']), }, { 'key': 'sensor_weather_outdoor_humidity_percent', 'labels': { - 'location': metoff_location, + 'location': location, }, 'type': 'gauge', - 'value': float(hour_data['H']), + 'value': float(latest_data['humidity']), }, ]