aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/yr.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/yr.py')
-rw-r--r--scripts/yr.py37
1 files changed, 12 insertions, 25 deletions
diff --git a/scripts/yr.py b/scripts/yr.py
index d4dc159..2fa17b1 100644
--- a/scripts/yr.py
+++ b/scripts/yr.py
@@ -1,41 +1,28 @@
#!/usr/bin/env python3
""" Get weatherdata from yr.no """
-import os
import sys
import common
import requests
-location = int(os.environ["el_location"])
-lat = str(os.environ["el_yr_lat"])
-lon = str(os.environ["el_yr_lon"])
+apiUrl = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat={}&lon={}"
+location = int(common.env("el_location"))
+lat = common.env("el_yr_lat")
+lon = common.env("el_yr_lon")
-apiUrl = (
- "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat="
- + lat
- + "&lon="
- + lon
-)
+url = apiUrl.format(lat, lon)
+headers = {
+ "User-Agent": "gratis.morell@litepost.no",
+ "Cache-Control": "no-cache",
+}
### Get the data
try:
- url = apiUrl
-
- # Request headers
- hdr = {
- "User-Agent": "gratis.morell@litepost.no",
- "Cache-Control": "no-cache",
- }
-
- response = requests.get(url, headers=hdr, timeout=10)
- if response.status_code != 200:
- print(response.status_code)
- print("Oh shit")
- response.raise_for_status()
-
+ response = requests.get(url, headers=headers, timeout=10)
+ response.raise_for_status()
except requests.exceptions.RequestException as e:
- print("oh lol")
+ print("Error in request: %s", e)
sys.exit(e)
data = response.json()