~robin_jadoul/organizers-bot

5d52660acef1a0a40186fb936bb3d2aceb320dc8 — LucidBrot 2 years ago 7ddfa0a
Replace datetime with dateutil for isostring parsing of datetime.

I tested my changes, had a few typos but now everything seems to run solidly. However, I got this
```
RROR:discord_slash:An exception has occurred while executing command `chal`:
Traceback (most recent call last):
  File "/root/.cache/pypoetry/virtualenvs/organizers-bot-9TtSrW0h-py3.9/lib/python3.9/site-packages/discord_slash/client.py", line 872, in invoke_command
    await func.invoke(ctx, args)
  File "/root/.cache/pypoetry/virtualenvs/organizers-bot-9TtSrW0h-py3.9/lib/python3.9/site-packages/discord_slash/model.py", line 168, in invoke
    return await coro
  File "/app/organizers_bot/bot.py", line 31, in wrapper
    return await f(ctx, *args, **kw)
  File "/app/organizers_bot/bot.py", line 88, in create_challenge_channel
    await ctfnote.add_task(ctx, created, challenge, category, solved_prefix = "✓-", ctfid = ctfid)
  File "/app/organizers_bot/ctfnote.py", line 592, in add_task
    current_ctf = await refresh_ctf(ctx, ctfid = ctfid)
  File "/app/organizers_bot/ctfnote.py", line 516, in refresh_ctf
    current_ctfs = await ctfnote.getActiveCtfs()
  File "/app/organizers_bot/ctfnote.py", line 438, in getActiveCtfs
    ctfs = list(filter(lambda ctf:
  File "/app/organizers_bot/ctfnote.py", line 439, in <lambda>
    datetime.fromisoformat(ctf["startTime"]) < now and
ValueError: Invalid isoformat string: '2022-07-04T17:44:11.17+00:00'

```
and I'm not sure why that would happen. Because the string looks to me like it matches the format they specify in https://docs.python.org/3.9/library/datetime.html#datetime.datetime.fromisoformat

But the docs say
> A more full-featured ISO 8601 parser, dateutil.parser.isoparse is available in the third-party package dateutil.
So I'm replacing that code with this.
1 files changed, 5 insertions(+), 4 deletions(-)

M organizers_bot/ctfnote.py
M organizers_bot/ctfnote.py => organizers_bot/ctfnote.py +5 -4
@@ 11,7 11,8 @@ websockets_logger.setLevel(logging.WARNING)

from string import ascii_letters, digits
from random import choice, randrange
from datetime import datetime, timezone
from datetime import datetime
import dateutil # parser, tz
import logging
import asyncio
from . import queries


@@ 434,10 435,10 @@ class CTFNote:

    async def getActiveCtfs(self):
        ctfs = await self.getIncomingCtfs()
        now = datetime.now(timezone.utc)
        now = datetime.now(dateutil.tz.UTC)
        ctfs = list(filter(lambda ctf: 
            datetime.fromisoformat(ctf["startTime"]) < now and
            datetime.fromisoformat(ctf["endTime"]) > now, ctfs))
            dateutil.parser.isoparse(ctf["startTime"]) < now and
            dateutil.parser.isoparse(ctf["endTime"]) > now, ctfs))

        # This is a list. Use an element like this:
        # return CTF(self.client, ctfs[0]) if ctfs else None