Skip to content

Commit

Permalink
Merge pull request #5 from PYCONDE/dr-automatically-trigger-api-refresh
Browse files Browse the repository at this point in the history
refresh api
  • Loading branch information
theopinard authored Apr 15, 2024
2 parents d321ec8 + 3dd2b2f commit 6f74f61
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
3 changes: 2 additions & 1 deletion EuroPythonBot/cogs/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ async def on_ready(self):
reg_channel = self.bot.get_channel(config.REG_CHANNEL_ID)

await reg_channel.purge()
await order_ins.fetch_data()
# start the async fetch_data task with will be triggered automatically using discord tasks
order_ins.fetch_data.start()
order_ins.load_registered()

embed = discord.Embed(
Expand Down
15 changes: 2 additions & 13 deletions EuroPythonBot/helpers/tito_connector.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import logging

# import os
from datetime import datetime
from http import HTTPStatus
from pathlib import Path
from time import time

import aiofiles
import aiohttp
from discord.ext import tasks
from dotenv import load_dotenv

from configuration import Config, Singleton
Expand All @@ -30,9 +28,6 @@ def __init__(self):

self.id_to_name = None
self.orders = {}
# TODO: fetch data every 5 minutes, triggered when validating tickets and checking
# last_fetch
self.last_fetch = None

self.registered_file = getattr(self.config, "REGISTERED_LOG_FILE", "./registered_log.txt")
self.REGISTERED_SET = set()
Expand All @@ -46,16 +41,14 @@ def load_registered(self):
except Exception:
_logger.exception("Cannot load registered data, starting from scratch. Error:")

@tasks.loop(minutes=10.0)
async def fetch_data(self) -> None:
"""Run refresh_all route from API that reloads ticket data from Tito."""

_logger.info("Refresh tickets from Tito")
time_start = time()
await self._update_tito(f"{self.config.TITO_BASE_URL}/tickets/refresh_all")
_logger.info("Updated tickets from Tito in %r seconds", time() - time_start)

self.last_fetch = datetime.now()

async def _update_tito(self, url) -> bool:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self.HEADERS) as response:
Expand All @@ -66,9 +59,6 @@ async def _update_tito(self, url) -> bool:

async def get_ticket_type(self, order: str, full_name: str) -> dict | None:
"""With user input `order` and `full_name`, check for their ticket type"""

# return "Personal"

key = f"{order}-{sanitize_string(input_string=full_name)}"
self.validate_key(key)
data = None
Expand Down Expand Up @@ -101,7 +91,6 @@ async def get_ticket_type(self, order: str, full_name: str) -> dict | None:

async def get_roles(self, name: str, order: str) -> list[int]:
roles: list[int] = []

data = await self.get_ticket_type(full_name=name, order=order)

if data:
Expand Down

0 comments on commit 6f74f61

Please sign in to comment.