Amazing-Python-Scripts
39 строк · 1.2 Кб
1import requests
2
3
4def get_temperature(json_data):
5temp_in_celcius = json_data['main']['temp']
6return temp_in_celcius
7
8
9def get_weather_type(json_data):
10weather_type = json_data['weather'][0]['description']
11return weather_type
12
13
14def get_wind_speed(json_data):
15wind_speed = json_data['wind']['speed']
16return wind_speed
17
18
19def get_weather_data(json_data, city):
20description_of_weather = json_data['weather'][0]['description']
21weather_type = get_weather_type(json_data)
22temperature = get_temperature(json_data)
23wind_speed = get_wind_speed(json_data)
24weather_details = ''
25return weather_details + ("The weather in {} is currently {} with a temperature of {} degrees and wind speeds reaching {} km/ph".format(city, weather_type, temperature, wind_speed))
26
27
28def main():
29api_address = 'https://api.openweathermap.org/data/2.5/weather?q=Sydney,au&appid=a10fd8a212e47edf8d946f26fb4cdef8&q='
30city = input("City Name : ")
31units_format = "&units=metric"
32final_url = api_address + city + units_format
33json_data = requests.get(final_url).json()
34weather_details = get_weather_data(json_data, city)
35# print formatted data
36print(weather_details)
37
38
39main()
40