-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrent_Weather_API.py
31 lines (28 loc) · 1.23 KB
/
Current_Weather_API.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
import requests as re ##--> Request module to request URL from web
import json ##--> To read json formatted data from web
class Weather_detail():
def __init__(self, name):
self.name = name
def weather(self): ##--> method of extracting weather detail from openweathermap API
baseurl = 'https://api.openweathermap.org/data/2.5/weather'
params_dict = {}
params_dict["APPID"] = 'Your_API_key'
#params_dict['zip'] = input("Enter City") --by zip code
#params_dict['q'] = input("Enter City Name : \t")
try :
params_dict['q'] = self.name
res = re.get(baseurl, params=params_dict)
page = res.json()
#print(page)
weather_type = page['weather'][0]['description']
#print(weather_type)
temp_c = round((page['main']['temp'] - 273.15), 2)
#print(temp_c)
details = [page['name'], weather_type, temp_c]
except:
weather_type = 'Spelling Mistake'
temp_c = 0
details = ['City Not Found', weather_type, temp_c]
return details
def __repr__(self):
return f"Object for {self.name}"