-
Notifications
You must be signed in to change notification settings - Fork 2
/
Weather.py
35 lines (29 loc) · 1.08 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
import requests
class Weather:
'''
usecase
>>> w = Weather("1271715", "9864a219384ffd7d670f6fa0ad93c653")
>>> w.getWeather()&APPID=
'''
def __init__(self, cityid, appid):
self.url = ('https://api.openweathermap.org/data/2.5/weather?id=' +
cityid + '&APPID=' + appid)
def getWeather(self):
res = requests.get(self.url)
if res.status_code != 200:
raise ValueError
res = res.json()
return {"weather": res["weather"][0]["icon"],
"temperature": str(int(res["main"]["temp"] - 273.15)) + "°",
"humidity": str(int(res["main"]["humidity"])),
"description": res["weather"][0]["description"]}
if __name__ == "__main__":
w = Weather("1271715", "9864a219384ffd7d670f6fa0ad93c653")
try:
d = w.getWeather()
print("Weather = " + d["weather"])
print("Temperature = " + d["temperature"])
print("Humidity = " + d["humidity"])
print("description = " + d["description"])
except ValueError:
print("invalid response")