Create dedicated package seeparklib and add openweathermap module.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 4 Jul 2018 18:33:09 +0000 (20:33 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 4 Jul 2018 18:33:09 +0000 (20:33 +0200)
seeparklib/__init__.py [new file with mode: 0644]
seeparklib/openweathermap.py [new file with mode: 0644]

diff --git a/seeparklib/__init__.py b/seeparklib/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/seeparklib/openweathermap.py b/seeparklib/openweathermap.py
new file mode 100644 (file)
index 0000000..d350eb6
--- /dev/null
@@ -0,0 +1,20 @@
+import requests
+
+
+class OpenWeatherMapError(RuntimeError):
+    pass
+
+
+def openweathermap_json(apikey, cityid):
+    """Returns parsed JSON as returned by openweathermap for the given cityid.
+    In case of errors, an OpenWeatherMapError is raised."""
+    baseurl = 'http://api.openweathermap.org/data/2.5/weather'
+    query = baseurl + '?units=metric&APPID={}&id={}&lang=de'.format(apikey, cityid)
+    try:
+        response = requests.get(query)
+        if response.status_code != 200:
+            raise OpenWeatherMapError('Got status code {} ({}).'.format(response.status_code, response.reason))
+        else:
+            return response.json()
+    except requests.exceptions.RequestException as error:
+        raise OpenWeatherMapError('Request not successful: {}'.format(error))