File size: 5,152 Bytes
cb47347 7b96044 cb47347 e0f71e1 60ee11d e0f71e1 cb47347 60ee11d cb47347 60ee11d cb47347 7b96044 e0f71e1 7b96044 cb47347 4438ccd cb47347 e0f71e1 cb47347 3ae3815 cb47347 3ae3815 cb47347 3ae3815 cb47347 e0f71e1 cb47347 60ee11d cb47347 60ee11d cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 cb47347 e0f71e1 |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
import requests
from .common import config, vehicle
def get_weather_current_location():
"""
Returns the CURRENT weather in current location.
When responding to user, only mention the weather condition, temperature, and the temperature that it feels like, unless the user asks for more information.
Returns:
dict: The weather data in the specified location.
"""
print(
f"get_weather: location is empty, using the vehicle location. ({vehicle.location})"
)
location = vehicle.location
return get_weather(location)
# current weather API
def get_weather(location: str = ""):
"""
Get the current weather in a specified location.
When responding to user, only mention the weather condition, temperature, and the temperature that it feels like, unless the user asks for more information.
Args:
location (string) : Optional. The name of the location, if empty, the vehicle location is used.
Returns:
dict: The weather data in the specified location.
"""
if location == "":
print(
f"get_weather: location is empty, using the vehicle location. ({vehicle.location})"
)
location = vehicle.location
# The endpoint URL provided by WeatherAPI
url = f"http://api.weatherapi.com/v1/current.json?key={config.WEATHER_API_KEY}&q={location}&aqi=no"
print(url)
# Make the API request
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to get weather data: {response.status_code}, {response.text}")
return f"Failed to get weather data, try again", response
# Parse the JSON response
weather_data = response.json()
# Extracting the necessary pieces of data
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"]
# Formulate the sentences - {region}, {country}
weather_sentences = (
f"The current weather in {location} 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
# weather forecast API
def get_forecast(city_name: str = "", when=0, **kwargs):
"""
Get the weather forecast in a specified number of days for a specified location.
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
# The endpoint URL provided by WeatherAPI
url = f"http://api.weatherapi.com/v1/forecast.json?key={WEATHER_API_KEY}&q={city_name}&days={str(when)}&aqi=no"
# Make the API request
response = requests.get(url)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Initialize an empty string to hold our result
forecast_sentences = ""
# Extract city information
location = data.get("location", {})
city_name = location.get("name", "the specified location")
# print(data)
# Extract the forecast days
forecast_days = data.get("forecast", {}).get("forecastday", [])[when - 1 :]
# number = 0
# print (forecast_days)
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"
# Generate a sentence for the day's forecast
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. "
# number = number + 1
# Add the sentence to the result
forecast_sentences += forecast_sentence
return forecast_sentences
else:
# Handle errors
print(f"Failed to get weather data: {response.status_code}, {response.text}")
return f"error {response.status_code}"
|