-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c26fdff
commit 781fddb
Showing
4 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# api_get_weatherapi.py | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
# github.com/FlyingFathead/TelegramBot-OpenAI-API/ | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
# | ||
# This API functionality requires WeatherAPI key. | ||
# You can get the API key from the corresponding service provider. | ||
# Once you have the API key, add it to your environment variables: | ||
# export WEATHERAPI_KEY="<your API key>" | ||
# (or on i.e. Linux, add to your `~/.bashrc`: export WEATHERAPI_KEY="<your API key>" ) | ||
|
||
import httpx | ||
import os | ||
import logging | ||
|
||
# Function to check for WeatherAPI key | ||
def get_weatherapi_key(): | ||
api_key = os.getenv('WEATHERAPI_KEY') | ||
if not api_key: | ||
logging.error("[WARNING] WeatherAPI key not set. You need to set the 'WEATHERAPI_KEY' environment variable to use WeatherAPI functionalities!") | ||
return None | ||
return api_key | ||
|
||
# get moon phase data | ||
async def get_moon_phase(lat, lon): | ||
api_key = get_weatherapi_key() | ||
if not api_key: | ||
return None | ||
|
||
logging.info(f"Fetching moon phase data for coordinates: Latitude: {lat}, Longitude: {lon}") | ||
base_url = 'http:https://api.weatherapi.com/v1/astronomy.json' | ||
url = f"{base_url}?key={api_key}&q={lat},{lon}" | ||
|
||
async with httpx.AsyncClient() as client: | ||
response = await client.get(url) | ||
logging.info(f"Moon phase response status: {response.status_code}") | ||
|
||
if response.status_code == 200: | ||
data = response.json() | ||
logging.info(f"Moon phase data: {data}") | ||
moon_phase = data['astronomy']['astro']['moon_phase'] | ||
return moon_phase | ||
else: | ||
logging.error(f"Failed to fetch moon phase data: {response.text}") | ||
return None | ||
|
||
# get timezone for the coordinates | ||
async def get_timezone(lat, lon): | ||
api_key = get_weatherapi_key() | ||
if not api_key: | ||
return None | ||
|
||
logging.info(f"Fetching timezone data for coordinates: Latitude: {lat}, Longitude: {lon}") | ||
base_url = 'http:https://api.weatherapi.com/v1/timezone.json' | ||
url = f"{base_url}?key={api_key}&q={lat},{lon}" | ||
|
||
async with httpx.AsyncClient() as client: | ||
response = await client.get(url) | ||
logging.info(f"Timezone response status: {response.status_code}") | ||
|
||
if response.status_code == 200: | ||
data = response.json() | ||
logging.info(f"Timezone data: {data}") | ||
timezone = data['location']['tz_id'] | ||
return timezone | ||
else: | ||
logging.error(f"Failed to fetch timezone data: {response.text}") | ||
return None | ||
|
||
# get daily forecast, safety alerts, and air quality index | ||
async def get_daily_forecast(location): | ||
api_key = get_weatherapi_key() | ||
if not api_key: | ||
return None | ||
|
||
logging.info(f"Fetching daily forecast data for location: {location}") | ||
base_url = 'http:https://api.weatherapi.com/v1/forecast.json' | ||
url = f"{base_url}?key={api_key}&q={location}&days=1&alerts=yes&aqi=yes" | ||
|
||
async with httpx.AsyncClient() as client: | ||
response = await client.get(url) | ||
logging.info(f"Daily forecast response status: {response.status_code}") | ||
|
||
if response.status_code == 200: | ||
data = response.json() | ||
logging.info(f"Daily forecast data: {data}") | ||
forecast = data['forecast']['forecastday'][0] | ||
current = data['current'] | ||
alerts = data.get('alerts', {}) | ||
air_quality = current['air_quality'] | ||
|
||
return { | ||
'date': forecast['date'], | ||
'temperature': forecast['day']['avgtemp_c'], | ||
'condition': forecast['day']['condition']['text'], | ||
'wind': forecast['day']['maxwind_kph'], | ||
'precipitation': forecast['day']['totalprecip_mm'], | ||
'uv_index': forecast['day']['uv'], | ||
'air_quality': air_quality, | ||
'alerts': alerts | ||
} | ||
else: | ||
logging.error(f"Failed to fetch daily forecast data: {response.text}") | ||
return None | ||
|
||
# Additional WeatherAPI-related functions can be added here |
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