Skip to content

Commit

Permalink
Create sync_weather.py
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahCxrest authored Jan 6, 2025
1 parent 7008e1c commit 9a0eb28
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions tasks/sync_weather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import asyncio
import logging
from discord.ext import tasks
import aiohttp
from decouple import config

from utils.prc_api import ResponseFailure

@tasks.loop(minutes=5, reconnect=True)
async def sync_weather(bot):
try:
logging.info("Starting weather sync task...")

logging.info("Querying for servers with weather sync settings...")
pipeline = [
{
'$match': {
'ERLC.weather': {'$exists': True},
'$or': [
{'ERLC.weather.sync_time': True},
{'ERLC.weather.sync_weather': True}
],
'ERLC.weather.location': {'$exists': True, '$ne': ''}
}
},
{
'$lookup': {
'from': 'server_keys',
'localField': '_id',
'foreignField': '_id',
'as': 'server_key'
}
},
{
'$match': {
'server_key': {'$ne': []}
}
}
]

weather_service_url = config('WEATHER_SERVICE_URL')
logging.info(f"Using weather service URL: {weather_service_url}")

server_count = await bot.settings.db.count_documents({
'ERLC.weather': {'$exists': True},
'$or': [
{'ERLC.weather.sync_time': True},
{'ERLC.weather.sync_weather': True}
],
'ERLC.weather.location': {'$exists': True, '$ne': ''}
})
logging.info(f"Found {server_count} servers with weather sync enabled")

processed = 0
async with aiohttp.ClientSession() as session:
async for guild_data in bot.settings.db.aggregate(pipeline):
processed += 1
guild_id = guild_data['_id']
weather_settings = guild_data['ERLC']['weather']
location = weather_settings['location']

logging.info(f"Processing guild {guild_id} ({processed}/{server_count})")
logging.info(f"Location: {location}")
logging.info(f"Settings: sync_weather={weather_settings.get('sync_weather')}, sync_time={weather_settings.get('sync_time')}")

try:
# Fetch weather data
logging.info(f"Fetching weather data for {location}...")
async with session.get(f'{weather_service_url}/?location={location}') as resp:
if resp.status != 200:
logging.error(f"Failed to fetch weather data for {location}: Status {resp.status}")
continue
weather_data = await resp.json()
logging.info(f"Weather data received: {weather_data}")

# Execute weather/time commands based on settings
if weather_settings.get('sync_weather'):
try:
logging.info(f"Setting weather to {weather_data['weatherType']} for guild {guild_id}")
await bot.prc_api.run_command(guild_id, f":weather {weather_data['weatherType']}")
logging.info(f"Successfully set weather for guild {guild_id}")
except ResponseFailure as e:
logging.error(f"Failed to sync weather for guild {guild_id}: {str(e)}")

if weather_settings.get('sync_time'):
try:
logging.info(f"Setting time to {weather_data['time']} for guild {guild_id}")
await bot.prc_api.run_command(guild_id, f":time {weather_data['time']}")
logging.info(f"Successfully set time for guild {guild_id}")
except ResponseFailure as e:
logging.error(f"Failed to sync time for guild {guild_id}: {str(e)}")

except Exception as e:
logging.error(f"Error syncing weather for guild {guild_id}: {str(e)}", exc_info=True)

logging.info(f"Weather sync task completed. Processed {processed}/{server_count} servers")

except Exception as e:
logging.error(f"Critical error in weather sync task: {str(e)}", exc_info=True)

0 comments on commit 9a0eb28

Please sign in to comment.