|
import requests |
|
|
|
from .common import config |
|
|
|
|
|
def get_weather(location:str= "", **kwargs): |
|
""" |
|
Returns the CURRENT weather in a specified location. |
|
Args: |
|
location (string) : Required. The name of the location, could be a city or lat/longitude in the following format latitude,longitude (example: 37.7749,-122.4194). |
|
""" |
|
|
|
url = f"http://api.weatherapi.com/v1/current.json?key={config.WEATHER_API_KEY}&q={location}&aqi=no" |
|
print(url) |
|
|
|
|
|
response = requests.get(url) |
|
|
|
if response.status_code != 200: |
|
return f"Failed to get weather data: {response.status_code}, {response.text}" |
|
|
|
|
|
weather_data = response.json() |
|
|
|
|
|
location = weather_data['location']['name'] |
|
region = weather_data['location']['region'] |
|
country = weather_data['location']['country'] |
|
time = weather_data['location']['localtime'] |
|
temperature_c = weather_data['current']['temp_c'] |
|
condition_text = weather_data['current']['condition']['text'] |
|
if 'wind_kph' in weather_data['current']: |
|
wind_kph = weather_data['current']['wind_kph'] |
|
humidity = weather_data['current']['humidity'] |
|
feelslike_c = weather_data['current']['feelslike_c'] |
|
|
|
|
|
weather_sentences = ( |
|
f"The current weather in {location}, {region}, {country} is {condition_text} " |
|
f"with a temperature of {temperature_c}°C that feels like {feelslike_c}°C. " |
|
f"Humidity is at {humidity}%. " |
|
f"Wind speed is {wind_kph} kph." if 'wind_kph' in weather_data['current'] else "" |
|
) |
|
return weather_sentences, weather_data |
|
|
|
|
|
def get_forecast(city_name:str= "", when = 0, **kwargs): |
|
""" |
|
Returns the weather forecast in a specified number of days for a specified city . |
|
Args: |
|
city_name (string) : Required. The name of the city. |
|
when (int) : Required. in number of days (until the day for which we want to know the forecast) (example: tomorrow is 1, in two days is 2, etc.) |
|
""" |
|
|
|
when +=1 |
|
|
|
url = f"http://api.weatherapi.com/v1/forecast.json?key={WEATHER_API_KEY}&q={city_name}&days={str(when)}&aqi=no" |
|
|
|
|
|
|
|
response = requests.get(url) |
|
|
|
if response.status_code == 200: |
|
|
|
data = response.json() |
|
|
|
|
|
forecast_sentences = "" |
|
|
|
|
|
location = data.get('location', {}) |
|
city_name = location.get('name', 'the specified location') |
|
|
|
|
|
|
|
|
|
|
|
forecast_days = data.get('forecast', {}).get('forecastday', [])[when-1:] |
|
|
|
|
|
|
|
|
|
for day in forecast_days: |
|
date = day.get('date', 'a specific day') |
|
conditions = day.get('day', {}).get('condition', {}).get('text', 'weather conditions') |
|
max_temp_c = day.get('day', {}).get('maxtemp_c', 'N/A') |
|
min_temp_c = day.get('day', {}).get('mintemp_c', 'N/A') |
|
chance_of_rain = day.get('day', {}).get('daily_chance_of_rain', 'N/A') |
|
|
|
if when == 1: |
|
number_str = 'today' |
|
elif when == 2: |
|
number_str = 'tomorrow' |
|
else: |
|
number_str = f'in {when-1} days' |
|
|
|
|
|
forecast_sentence = f"On {date} ({number_str}) in {city_name}, the weather will be {conditions} with a high of {max_temp_c}°C and a low of {min_temp_c}°C. There's a {chance_of_rain}% chance of rain. " |
|
|
|
|
|
|
|
forecast_sentences += forecast_sentence |
|
return forecast_sentences |
|
else: |
|
|
|
print( f"Failed to get weather data: {response.status_code}, {response.text}") |
|
return f'error {response.status_code}' |