commit e9aaaaf3ff329e79c12776ac49a8c854233c0d03 Author: Nell Date: Wed Feb 14 22:06:20 2024 +0100 Init diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..565f066 --- /dev/null +++ b/.env.dist @@ -0,0 +1,2 @@ +DISCORD_TOKEN=none +CHANNEL_ID=none \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b2f5e3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +.venv/ +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b01c613 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.12-slim + +ENV PYTHONUNBUFFERED 1 + +WORKDIR /app +RUN pip install --upgrade pip + +COPY ./requirements.txt ./requirements.txt +RUN pip install -r requirements.txt + +COPY . . + +CMD bash -c "python main.py" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7eb10f7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3' + +services: + app: + build: + context: . + env_file: + - .env + restart: unless-stopped \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..8180a93 --- /dev/null +++ b/main.py @@ -0,0 +1,146 @@ +from os import getenv + +import aiohttp +from dotenv import load_dotenv +import discord +from discord.ext import tasks +import asyncio +import datetime +import pytz + +load_dotenv() +TOKEN = getenv('DISCORD_TOKEN') +CHANNEL_ID = int(getenv('CHANNEL_ID')) + +TEMPO_API_BASE = "https://www.api-couleur-tempo.fr/api" +TEMPO_API_TODAY = f"{TEMPO_API_BASE}/jourTempo/today" +TEMPO_API_TOMORROW = f"{TEMPO_API_BASE}/jourTempo/tomorrow" + +map_code_jour = { + 0: { + "title": "Non déterminé", + "icon": "⚫" + }, + 1: { + "title": "Jour Bleu", + "icon": "🔵" + }, + 2: { + "title": "Jour Blanc", + "icon": "⚪", + }, + 3: { + "title": "Jour Rouge (TRÈS CHÈRE)", + "icon": "🔴" + } +} + +paris_timezone = pytz.timezone("Europe/Paris") +# fonctionne pas correctement avec les timezone +# today_hour = datetime.time(hour=0, minute=1, tzinfo=paris_timezone) +# tomorrow_hour = datetime.time(hour=12, minute=22, tzinfo=paris_timezone) + + + +async def fetch_today(): + async with aiohttp.ClientSession() as session: + async with session.get(TEMPO_API_TODAY) as response: + return await response.json() + + +async def fetch_tomorrow(): + async with aiohttp.ClientSession() as session: + async with session.get(TEMPO_API_TOMORROW) as response: + return await response.json() + + +def format_today(data): + date_day = data["dateJour"] + code_day = data["codeJour"] + + return f"!tempo_today - {map_code_jour[code_day]['icon']} {map_code_jour[code_day]['title']} - {date_day}" + + +def format_tomorrow(data): + date_day = data["dateJour"] + code_day = data["codeJour"] + + return f"!tempo_tomorrow - {map_code_jour[code_day]['icon']} {map_code_jour[code_day]['title']} - {date_day}" + + +class MyClient(discord.Client): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.sent_messages = { + "today": None, + "tomorrow": None + } + + self.channel = None + + async def setup_hook(self) -> None: + print("Setting up") + self.background_update_today.start() + self.background_update_tomorrow.start() + + + async def on_ready(self): + print(f'Logged in as {self.user}') + self.channel = self.get_channel(CHANNEL_ID) + + async def on_message(self, message: discord.Message) -> None: + # print(f"Message from {message.author}: {message}") + match message.content: + case "?tempo_today": + await self._send_today(message) + case "?tempo_tomorrow": + await self._send_tomorrow(message) + + async def _send_today(self, response_to: discord.Message=None): + data = await fetch_today() + message = format_today(data) + if response_to: + await response_to.reply(message) + else: + await self.channel.send(message) + + async def _send_tomorrow(self, response_to: discord.Message=None): + data = await fetch_tomorrow() + message = format_tomorrow(data) + if response_to: + await response_to.reply(message) + else: + await self.channel.send(message) + + @tasks.loop(seconds=60) + async def background_update_today(self): + now = datetime.datetime.now(tz=paris_timezone).time() + print(now.hour, now.minute) + if now.hour == 0 and now.minute == 0: + async for message in self.channel.history(): + content = message.content + if message.author.id == self.user.id: + if content.startswith("!tempo_today") or content.startswith("!tempo_tomorrow"): + await message.delete() + + await self._send_today() + + @tasks.loop(seconds=60) + async def background_update_tomorrow(self): + now = datetime.datetime.now(tz=paris_timezone).time() + if now.hour == 12 and now.minute == 0: + async for message in self.channel.history(): + content = message.content + if message.author.id == self.user.id: + if content.startswith("!tempo_tomorrow"): + await message.delete() + + await self._send_tomorrow() + + +intents = discord.Intents.default() +intents.message_content = True + +client = MyClient(intents=intents) +client.run(TOKEN) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5c76797 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +aiohttp==3.9.3 +aiosignal==1.3.1 +attrs==23.2.0 +discord.py==2.3.2 +frozenlist==1.4.1 +idna==3.6 +multidict==6.0.5 +python-dotenv==1.0.1 +pytz==2024.1 +yarl==1.9.4 \ No newline at end of file