65 lines
2.8 KiB
Python
65 lines
2.8 KiB
Python
import aiohttp
|
|
import disnake
|
|
from disnake.ext import commands, tasks
|
|
from loguru import logger
|
|
import httpx
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
class Shk_tasks(commands.Cog):
|
|
def __init__(self,bot):
|
|
self.bot = bot
|
|
self.getHouseData.start()
|
|
|
|
def cog_unload(self):
|
|
logger.info(f"{__name__} Unloaded")
|
|
self.getHouseData.cancel()
|
|
|
|
@tasks.loop(hours=2)
|
|
async def getHouseData(self) -> None:
|
|
#Collect all worlds we have access too.
|
|
await self.bot.wait_until_ready()
|
|
logger.info("Starting House Data Update")
|
|
|
|
filter_query = {"worlds": {"$exists": True}}
|
|
documents = await self.bot.database.servers.find(filter_query).to_list(None)
|
|
|
|
# Combine dictionaries into a collated list of worlds
|
|
collated_worlds = {}
|
|
for doc in documents:
|
|
worlds = doc.get("worlds")
|
|
collated_worlds.update(worlds)
|
|
upsert_operations = []
|
|
world:str
|
|
try:
|
|
async with aiohttp.ClientSession() as session:
|
|
for world,key in collated_worlds.items():
|
|
if key is None: continue
|
|
logger.info(f"Updating house info for {world}")
|
|
for house in range(1,21):
|
|
try:
|
|
async with session.get(f'https://shk.azure-api.net/shkinfo/v1/HouseActivity?world={world}&house={house}&Key={key}&subscription-key=ff2e578e119348ea8b48a2acd2f5a48d',timeout=20) as houseActivity:
|
|
for user in await houseActivity.json():
|
|
filter_query = {
|
|
"in_game_name": user["Username"],
|
|
"world": world,
|
|
"house": house
|
|
}
|
|
update_query = {
|
|
"$set": {
|
|
"timestamp": datetime.now()
|
|
}
|
|
}
|
|
await self.bot.database.players.update_one({"in_game_name": user["Username"]},{"$set": {"in_game_name": user["Username"]}},upsert=True)
|
|
await self.bot.database.house_history.update_one(filter_query, update_query, upsert=True)
|
|
except aiohttp.ServerTimeoutError:
|
|
logger.exception(f"Timeout when trying to fetch data for World {world} and house {house}")
|
|
logger.info(f"Finished {world} update")
|
|
except Exception as e:
|
|
logger.exception("Scan failed due to exception")
|
|
logger.info("Finished House Data update")
|
|
|
|
def setup(bot):
|
|
logger.info(f"{__name__} Loaded")
|
|
bot.add_cog(Shk_tasks(bot)) |