Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatBot/weather.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
16 lines (14 sloc)
1.27 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests #This library is necessary to get access to URL and its data if a web allows. | |
'''The function is giving the temperature and short weather condition for a particular location. ''' | |
def weather_ask(sentence): | |
try: | |
city = sentence | |
url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + '&APPID=f7d590dc5f61801f09e17f3922567a55' #The variable city can be either from stored data (global variable) or from user input (when asking for a different location). | |
res = requests.get(url) | |
data = res.json() #JSON format is necessary to get the data from the website. | |
temp = data['main']['temp'] #Choose which data wants to get from the web page, in this case, it is temperature. | |
temp_C = temp - 273.15 #Making conversion from Kelvin degrees into Celsius degrees. | |
weather_description = data['weather'][0]['description'] #Weather description data is taken from web page. | |
print('Temperature is: ', round(temp_C, 1), 'degree and', weather_description) | |
except: | |
print("Invalid command this is used by typing the following: weather in [city] OR weather for [city] where [city] is replaced with the city you want to find the weather for") |