def weather(city=None):
if not city:
city = get_location()['city']
# Checks country
country = get_location()['country_name']
# If country is US, shows weather in Fahrenheit
if country == 'United States':
send_url = (
"http://api.openweathermap.org/data/2.5/weather?q={0}"
"&APPID=ab6ec687d641ced80cc0c935f9dd8ac9&units=imperial".format(
city)
)
unit = ' ºF in '
# If country is not US, shows weather in Celsius
else:
send_url = (
"http://api.openweathermap.org/data/2.5/weather?q={0}"
"&APPID=ab6ec687d641ced80cc0c935f9dd8ac9&units=metric".format(
city)
)
unit = ' ºC in '
r = requests.get(send_url)
j = json.loads(r.text)
# check if the city entered is not found
if 'message' in j and j['message'] == 'city not found':
print(Fore.BLUE + "City Not Found" + Fore.RESET)
return False
else:
temperature = j['main']['temp']
description = j['weather'][0]['main']
print(Fore.BLUE + "It's " + str(temperature) + unit +
str(city) + " (" + str(description) + ")" + Fore.RESET)
return True
评论列表
文章目录