From 9157bb092eb439f1dc5137c48f4c86d8ec9ece68 Mon Sep 17 00:00:00 2001 From: CMDR furrycat Date: Fri, 7 Oct 2016 10:39:55 +0100 Subject: [PATCH] Parsing fixes when creating announcements. --- bot.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index e35c44c..8c79598 100755 --- a/bot.py +++ b/bot.py @@ -884,7 +884,13 @@ def show_announcement(message, id): @asyncio.coroutine def create_announcement(message, raw): # announcement create [params]: - parts = raw.split(':') + # We want to split by colon but don't want to count any which are part of dates. + m = re.match(r'(?:[^:]*\s+(?:at|from|to)\s+\d\d\d\d-?\d\d-?\d\d(?:T|\s+)\d\d:?\d\d:?\d\dZ?)+', raw) + if m is not None: + parts = raw[len(m.group(0)):].split(':') + parts[0] = m.group(0) + parts[0] + else: + parts = raw.split(':') params = parts[0] if len(parts) > 1: text = ':'.join(parts[1:]).strip() @@ -904,7 +910,10 @@ def create_announcement(message, raw): arg = args[i].lower() if i > len(args) - 1: break - param = args[i + 1] + try: + param = args[i + 1] + except IndexError: + param = None log.info('{}: {}={}'.format(i, arg, param)) ok = False @@ -917,9 +926,9 @@ def create_announcement(message, raw): ok = True elif arg in ['at', 'from', 'to']: k = 'end_date' if arg == 'to' else 'start_date' - m = re.match(r'(\d\d\d\d)-?(\d\d)-?(\d\d)[T\s](\d\d):?(\d\d):?(\d\d)Z?', param) + m = re.match(r'(\d\d\d\d)-?(\d\d)-?(\d\d)(?:T|\s+)(\d\d):?(\d\d):?(\d\d)Z?', param) if m is not None: - create[k] = int(datetime.datetime(*([int(n) for n in m.group()] + [0, pytz.UTC])).timestamp()) + create[k] = int(datetime.datetime(*([int(n) for n in m.groups()] + [0, pytz.UTC])).timestamp()) ok = True else: break -- 2.7.4