extractDate function is now in a separate Python module
This commit is contained in:
parent
04bde60482
commit
ee14682c4f
4 changed files with 59 additions and 70 deletions
68
message.py
68
message.py
|
|
@ -127,71 +127,3 @@ class Message:
|
||||||
#TODO: use encoding from config file
|
#TODO: use encoding from config file
|
||||||
s = s.decode('utf-8', 'replace')
|
s = s.decode('utf-8', 'replace')
|
||||||
return s
|
return s
|
||||||
|
|
||||||
##############################
|
|
||||||
# #
|
|
||||||
# Extraction/Format text #
|
|
||||||
# #
|
|
||||||
##############################
|
|
||||||
|
|
||||||
def extractDate (self):
|
|
||||||
"""Parse a message to extract a time and date"""
|
|
||||||
msgl = self.content.lower ()
|
|
||||||
result = re.match("^[^0-9]+(([0-9]{1,4})[^0-9]+([0-9]{1,2}|janvier|january|fevrier|février|february|mars|march|avril|april|mai|maï|may|juin|juni|juillet|july|jully|august|aout|août|septembre|september|october|octobre|oktober|novembre|november|decembre|décembre|december)([^0-9]+([0-9]{1,4}))?)[^0-9]+(([0-9]{1,2})[^0-9]*[h':]([^0-9]*([0-9]{1,2})([^0-9]*[m\":][^0-9]*([0-9]{1,2}))?)?)?.*$", msgl + " TXT")
|
|
||||||
if result is not None:
|
|
||||||
day = result.group(2)
|
|
||||||
if len(day) == 4:
|
|
||||||
year = day
|
|
||||||
day = 0
|
|
||||||
month = result.group(3)
|
|
||||||
if month == "janvier" or month == "january" or month == "januar":
|
|
||||||
month = 1
|
|
||||||
elif month == "fevrier" or month == "février" or month == "february":
|
|
||||||
month = 2
|
|
||||||
elif month == "mars" or month == "march":
|
|
||||||
month = 3
|
|
||||||
elif month == "avril" or month == "april":
|
|
||||||
month = 4
|
|
||||||
elif month == "mai" or month == "may" or month == "maï":
|
|
||||||
month = 5
|
|
||||||
elif month == "juin" or month == "juni" or month == "junni":
|
|
||||||
month = 6
|
|
||||||
elif month == "juillet" or month == "jully" or month == "july":
|
|
||||||
month = 7
|
|
||||||
elif month == "aout" or month == "août" or month == "august":
|
|
||||||
month = 8
|
|
||||||
elif month == "september" or month == "septembre":
|
|
||||||
month = 9
|
|
||||||
elif month == "october" or month == "october" or month == "oktober":
|
|
||||||
month = 10
|
|
||||||
elif month == "november" or month == "novembre":
|
|
||||||
month = 11
|
|
||||||
elif month == "december" or month == "decembre" or month == "décembre":
|
|
||||||
month = 12
|
|
||||||
|
|
||||||
if day == 0:
|
|
||||||
day = result.group(5)
|
|
||||||
else:
|
|
||||||
year = result.group(5)
|
|
||||||
|
|
||||||
hour = result.group(7)
|
|
||||||
minute = result.group(9)
|
|
||||||
second = result.group(11)
|
|
||||||
|
|
||||||
if year == None:
|
|
||||||
year = date.today().year
|
|
||||||
if hour == None:
|
|
||||||
hour = 0
|
|
||||||
if minute == None:
|
|
||||||
minute = 0
|
|
||||||
if second == None:
|
|
||||||
second = 1
|
|
||||||
else:
|
|
||||||
second = int (second) + 1
|
|
||||||
if second > 59:
|
|
||||||
minute = int (minute) + 1
|
|
||||||
second = 0
|
|
||||||
|
|
||||||
return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ from datetime import date
|
||||||
|
|
||||||
from hooks import hook
|
from hooks import hook
|
||||||
from tools.countdown import countdown_format
|
from tools.countdown import countdown_format
|
||||||
|
from tools.date import extractDate
|
||||||
from xmlparser.node import ModuleState
|
from xmlparser.node import ModuleState
|
||||||
|
|
||||||
nemubotversion = 3.4
|
nemubotversion = 3.4
|
||||||
|
|
@ -88,7 +89,7 @@ def parseask(msg):
|
||||||
res = re.match(r"^(\S+)\s*('s|suis|est|is|was|were)?\s+(birthday|geburtstag|née? |nee? le|born on).*$", msg.text, re.I)
|
res = re.match(r"^(\S+)\s*('s|suis|est|is|was|were)?\s+(birthday|geburtstag|née? |nee? le|born on).*$", msg.text, re.I)
|
||||||
if res is not None:
|
if res is not None:
|
||||||
try:
|
try:
|
||||||
extDate = msg.extractDate()
|
extDate = extractDate(msg.text)
|
||||||
if extDate is None or extDate.year > datetime.now().year:
|
if extDate is None or extDate.year > datetime.now().year:
|
||||||
return Response(msg.sender,
|
return Response(msg.sender,
|
||||||
"la date de naissance ne paraît pas valide...",
|
"la date de naissance ne paraît pas valide...",
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ nemubotversion = 3.4
|
||||||
|
|
||||||
from event import ModuleEvent
|
from event import ModuleEvent
|
||||||
from hooks import Hook, hook
|
from hooks import Hook, hook
|
||||||
|
from tools.date import extractDate
|
||||||
from tools.countdown import countdown_format, countdown
|
from tools.countdown import countdown_format, countdown
|
||||||
|
|
||||||
def help_full ():
|
def help_full ():
|
||||||
|
|
@ -198,7 +199,7 @@ def parseask(msg):
|
||||||
|
|
||||||
texts = re.match("^[^\"]*(avant|après|apres|before|after)?[^\"]*\"([^\"]+)\"[^\"]*((avant|après|apres|before|after)?.*\"([^\"]+)\".*)?$", msg.text, re.I)
|
texts = re.match("^[^\"]*(avant|après|apres|before|after)?[^\"]*\"([^\"]+)\"[^\"]*((avant|après|apres|before|after)?.*\"([^\"]+)\".*)?$", msg.text, re.I)
|
||||||
if texts is not None and texts.group(3) is not None:
|
if texts is not None and texts.group(3) is not None:
|
||||||
extDate = msg.extractDate()
|
extDate = extractDate(msg.text)
|
||||||
if extDate is None or extDate == "":
|
if extDate is None or extDate == "":
|
||||||
raise IRCException("la date de l'événement est invalide !")
|
raise IRCException("la date de l'événement est invalide !")
|
||||||
|
|
||||||
|
|
|
||||||
55
tools/date.py
Normal file
55
tools/date.py
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
# Extraction/Format text
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
import re
|
||||||
|
|
||||||
|
xtrdt = re.compile(r'''^.*? (?P<day>[0-9]{1,4}) .+?
|
||||||
|
(?P<month>[0-9]{1,2}|janvier|january|fevrier|février|february|mars|march|avril|april|mai|maï|may|juin|juni|juillet|july|jully|august|aout|août|septembre|september|october|octobre|oktober|novembre|november|decembre|décembre|december)
|
||||||
|
(?:.+?(?P<year>[0-9]{1,4}))? [^0-9]+
|
||||||
|
(?:(?P<hour>[0-9]{1,2})[^0-9]*[h':]
|
||||||
|
(?:[^0-9]*(?P<minute>[0-9]{1,2})
|
||||||
|
(?:[^0-9]*[m\":][^0-9]*(?P<second>[0-9]{1,2}))?)?)?.*?
|
||||||
|
$''', re.X)
|
||||||
|
|
||||||
|
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")
|
||||||
|
if month == "janvier" or month == "january" or month == "januar": month = 1
|
||||||
|
elif month == "fevrier" or month == "février" or month == "february": month = 2
|
||||||
|
elif month == "mars" or month == "march": month = 3
|
||||||
|
elif month == "avril" or month == "april": month = 4
|
||||||
|
elif month == "mai" or month == "may" or month == "maï": month = 5
|
||||||
|
elif month == "juin" or month == "juni" or month == "junni": month = 6
|
||||||
|
elif month == "juillet" or month == "jully" or month == "july": month = 7
|
||||||
|
elif month == "aout" or month == "août" or month == "august": month = 8
|
||||||
|
elif month == "september" or month == "septembre": month = 9
|
||||||
|
elif month == "october" or month == "october" or month == "oktober": month = 10
|
||||||
|
elif month == "november" or month == "novembre": month = 11
|
||||||
|
elif month == "december" or month == "decembre" or month == "décembre": month = 12
|
||||||
|
|
||||||
|
year = result.group("year")
|
||||||
|
|
||||||
|
if len(day) == 4:
|
||||||
|
day, year = year, day
|
||||||
|
|
||||||
|
hour = result.group("hour")
|
||||||
|
minute = result.group("minute")
|
||||||
|
second = result.group("second")
|
||||||
|
|
||||||
|
if year is None: year = date.today().year
|
||||||
|
if hour is None: hour = 0
|
||||||
|
if minute is None: minute = 0
|
||||||
|
if second is None:
|
||||||
|
second = 1
|
||||||
|
else:
|
||||||
|
second = int(second) + 1
|
||||||
|
if second > 59:
|
||||||
|
minute = int(minute) + 1
|
||||||
|
second = 0
|
||||||
|
|
||||||
|
return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
|
||||||
|
else:
|
||||||
|
return None
|
||||||
Loading…
Add table
Add a link
Reference in a new issue