· 6 years ago · Mar 24, 2020, 07:48 PM
1def weather():
2 # Python program to find current
3 # using openweathermap api
4
5 # import required modules
6 import requests, json
7
8 # Enter your API key here
9 api_key = "c012abf83e8afdf40810863f64ae8618"
10
11 # base_url variable to store url
12 base_url = "http://api.openweathermap.org/data/2.5/weather?"
13
14 # Give city name
15 send_message("в каком городе вы живете?")
16 city_name = event.obj.text.lower()
17
18 # complete_url variable to store
19 # complete url address
20 complete_url = base_url + "appid=" + api_key + "&q=" + city_name
21
22 # get method of requests module
23 # return response object
24 response = requests.get(complete_url)
25
26 # json method of response object
27 # convert json format data into
28 # python format data
29 x = response.json()
30
31 # Now x contains list of nested dictionaries
32 # Check the value of "cod" key is equal to
33 # "404", means city is found otherwise,
34 # city is not found
35 if x["cod"] != "404":
36
37 # store the value of "main"
38 # key in variable y
39 y = x["main"]
40
41 # store the value corresponding
42 # to the "temp" key of y
43 current_temperature = y["temp"]
44
45 # store the value corresponding
46 # to the "pressure" key of y
47 current_pressure = y["pressure"]
48
49 # store the value corresponding
50 # to the "humidity" key of y
51 current_humidiy = y["humidity"]
52
53 # store the value of "weather"
54 # key in variable z
55 z = x["weather"]
56
57 # store the value corresponding
58 # to the "description" key at
59 # the 0th index of z
60 weather_description = z[0]["description"]
61
62 # print following values
63 print(" Temperature (in kelvin unit) = " +
64 str(current_temperature) +
65 "\n atmospheric pressure (in hPa unit) = " +
66 str(current_pressure) +
67 "\n humidity (in percentage) = " +
68 str(current_humidiy) +
69 "\n description = " +
70 str(weather_description))
71
72 else:
73 print(" City Not Found ")