-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
37 lines (29 loc) · 1.26 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests
from config import Configuration
class Weather:
@staticmethod
def get_weather(): # move to a seperate module
pocasi = None
params = {"q": "Prague",
"appid": Configuration.weatherAppID,
"units": "metric",
"lang": "cz"}
req = requests.get("http://api.openweathermap.org/data/2.5/weather", params=params)
if req.status_code == 200:
try:
parsed = req.json()
pocasi = {"success": True, "message": "200"}
if "main" in parsed:
pocasi["icon"] = parsed["weather"][0]["icon"]
pocasi["teplota"] = parsed["main"]["temp"]
pocasi["vlhkost"] = parsed["main"]["humidity"]
if "wind" in parsed:
pocasi["vitr_rychlost"] = parsed["wind"]["speed"]
pocasi["vitr_uhel"] = parsed["wind"]["deg"]
if "weather" in parsed:
pocasi["popis"] = parsed["weather"][0]["description"]
except Exception as exp:
pocasi = {"success": False, "message": str(exp)}
else:
pocasi = {"success": False, "message": "Non-OK code returned."}
return pocasi