Move from Met Office to weatherapi.com

This commit is contained in:
Scott Wallace 2021-06-22 16:09:56 +01:00
parent 75b6b1c3c2
commit 3d5249272d
Signed by: scott
GPG key ID: AA742FDC5AFE2A72
3 changed files with 25 additions and 12 deletions

4
README.md Normal file
View file

@ -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.

10
docker-compose.yaml Normal file
View file

@ -0,0 +1,10 @@
---
version: '3.7'
services:
weather-exporter:
image: weather-exporter:latest
container_name: weather-exporter
environment:
- WEATHER_LOCATION=<Location search string>
- WEATHER_APIKEY=<weatherapi.com API key>
restart: unless-stopped

23
main.py
View file

@ -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']),
},
]