2014-11-02 02:02:44 +00:00
|
|
|
# Nemubot is a smart and modulable IM bot.
|
2015-01-03 19:17:46 +00:00
|
|
|
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
|
2014-11-02 02:02:44 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-09-08 00:25:26 +00:00
|
|
|
# Extraction/Format text
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2015-11-24 23:49:55 +00:00
|
|
|
month_binding = {
|
|
|
|
"janvier": 1, "january": 1, "januar": 1,
|
|
|
|
"fevrier": 2, "février": 2, "february": 2,
|
|
|
|
"march": 3, "mars": 3,
|
|
|
|
"avril": 4, "april": 4,
|
|
|
|
"mai": 5, "may": 5, "maï": 5,
|
|
|
|
"juin": 6, "juni": 6, "junni": 6,
|
|
|
|
"juillet": 7, "jully": 7, "july": 7,
|
|
|
|
"aout": 8, "août": 8, "august": 8,
|
|
|
|
"septembre": 9, "september": 9,
|
|
|
|
"october": 10, "oktober": 10, "octobre": 10,
|
|
|
|
"november": 11, "novembre": 11,
|
|
|
|
"decembre": 12, "décembre": 12, "december": 12,
|
|
|
|
}
|
|
|
|
|
2014-09-08 00:25:26 +00:00
|
|
|
xtrdt = re.compile(r'''^.*? (?P<day>[0-9]{1,4}) .+?
|
2015-11-24 23:49:55 +00:00
|
|
|
(?P<month>[0-9]{1,2}|"''' + "|".join(month_binding) + '''")
|
2014-12-02 06:25:12 +00:00
|
|
|
(?:.+?(?P<year>[0-9]{1,4}))? (?:[^0-9]+
|
2014-09-08 00:25:26 +00:00
|
|
|
(?:(?P<hour>[0-9]{1,2})[^0-9]*[h':]
|
|
|
|
(?:[^0-9]*(?P<minute>[0-9]{1,2})
|
2014-12-02 06:25:12 +00:00
|
|
|
(?:[^0-9]*[m\":][^0-9]*(?P<second>[0-9]{1,2}))?)?)?.*?)?
|
2014-09-08 00:25:26 +00:00
|
|
|
$''', re.X)
|
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
|
2014-09-08 00:25:26 +00:00
|
|
|
def extractDate(msg):
|
|
|
|
"""Parse a message to extract a time and date"""
|
|
|
|
result = xtrdt.match(msg.lower())
|
|
|
|
if result is not None:
|
|
|
|
day = result.group("day")
|
|
|
|
month = result.group("month")
|
2015-11-24 23:49:55 +00:00
|
|
|
|
|
|
|
if month in month_binding:
|
|
|
|
month = month_binding[month]
|
2014-09-08 00:25:26 +00:00
|
|
|
|
|
|
|
year = result.group("year")
|
|
|
|
|
|
|
|
if len(day) == 4:
|
|
|
|
day, year = year, day
|
|
|
|
|
|
|
|
hour = result.group("hour")
|
|
|
|
minute = result.group("minute")
|
|
|
|
second = result.group("second")
|
|
|
|
|
2014-11-09 13:11:54 +00:00
|
|
|
if year is None:
|
2015-02-21 12:51:40 +00:00
|
|
|
from datetime import date
|
2014-11-09 13:11:54 +00:00
|
|
|
year = date.today().year
|
|
|
|
if hour is None:
|
|
|
|
hour = 0
|
|
|
|
if minute is None:
|
|
|
|
minute = 0
|
2014-09-08 00:25:26 +00:00
|
|
|
if second is None:
|
|
|
|
second = 1
|
|
|
|
else:
|
|
|
|
second = int(second) + 1
|
|
|
|
if second > 59:
|
|
|
|
minute = int(minute) + 1
|
|
|
|
second = 0
|
|
|
|
|
2015-02-21 12:51:40 +00:00
|
|
|
from datetime import datetime
|
2014-11-09 13:11:54 +00:00
|
|
|
return datetime(int(year), int(month), int(day),
|
|
|
|
int(hour), int(minute), int(second))
|
2014-09-08 00:25:26 +00:00
|
|
|
else:
|
|
|
|
return None
|