forked from nsccs/SmartMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_weather.py
82 lines (63 loc) · 2.08 KB
/
get_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Python program to find current
# weather details of any city
# using openweathermap api
# import required modules
import requests, json
# Enter your API key here
api_key = "611454210432c7064822cbcbe7dc14d7"
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
city_name = input("Enter city name : ")
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
# get method of requests module
# return response object
response = requests.get(complete_url)
# json method of response object
# convert json format data into
# python format data
x = response.json()
# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] != "404":
# store the value of "main"
# key in variable y
y = x["main"]
# store the value corresponding
# to the "temp" key of y
current_temperature_k = y["temp"]
# store the value corresponding
# to the "pressure" key of y
current_pressure = y["pressure"]
# store the value corresponding
# to the "humidity" key of y
current_humidiy = y["humidity"]
# store the value of "weather"
# key in variable z
z = x["weather"]
# store the value corresponding
# to the "description" key at
# the 0th index of z
weather_description = z[0]["description"]
# converts temperature from Kelvin to Fahrenheit
current_temperature_f = 1.8 * (current_temperature_k - 273) + 32
# print following values
print("\nTemperature: " + format(current_temperature_f, '.0f') + " F")
# print("Atmospheric pressure: " + str(current_pressure) + " hPa")
print("Humidity: " + str(current_humidiy) + "%")
print("Description: " + str(weather_description))
print(x)
else:
print(" City Not Found ")
'''
What we want to be returned:
1. Temp
2. Feels Like
3. Description:
4. Wind Speed
5. Sun Set Time
'''