-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
executable file
·56 lines (42 loc) · 1.4 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
# #Python program to find current
# weather details of any city
# using openweathermap api
# import required modules
import requests, json, os
#do pip install python-dotenv, or if you dont want to store your keys in a env file put them in the string
from dotenv import load_dotenv
load_dotenv()
# Enter your API key here
api_key = str(os.getenv('APIKEY'))
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
city_name = str(os.getenv('CITY'))
# 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()
# print(x)
# 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
#changing the kalvin value to celcius
current_temperature = int(round(y["temp"] - 273, 0))
if(current_temperature < 30):
current_temperature = str(current_temperature)
else:
current_temperature = str(current_temperature)
print(current_temperature)