1
0
Fork 0
nemubot/modules/birthday.py

135 lines
4.7 KiB
Python
Raw Normal View History

2014-08-27 23:39:31 +00:00
"""People birthdays and ages"""
2015-10-05 21:54:38 +00:00
# PYTHON STUFFS #######################################################
import re
import sys
2014-10-09 05:30:04 +00:00
from datetime import date, datetime
2015-02-11 17:12:39 +00:00
from nemubot import context
from nemubot.exception import IMException
from nemubot.hooks import hook
from nemubot.tools.countdown import countdown_format
from nemubot.tools.date import extractDate
from nemubot.tools.xmlparser.node import ModuleState
from nemubot.module.more import Response
2014-11-13 01:51:49 +00:00
2015-10-05 21:54:38 +00:00
# LOADING #############################################################
def load(context):
2015-02-11 17:12:39 +00:00
context.data.setIndex("name", "birthday")
2015-10-05 21:54:38 +00:00
# MODULE CORE #########################################################
2012-07-23 10:07:26 +00:00
def findName(msg):
2015-07-10 21:09:54 +00:00
if (not len(msg.args) or msg.args[0].lower() == "moi" or
msg.args[0].lower() == "me"):
2017-07-18 04:32:48 +00:00
name = msg.frm.lower()
2012-11-04 03:39:54 +00:00
else:
2015-07-10 21:09:54 +00:00
name = msg.args[0].lower()
2012-07-23 10:07:26 +00:00
2012-11-04 03:39:54 +00:00
matches = []
2015-02-11 17:12:39 +00:00
if name in context.data.index:
2012-11-04 03:39:54 +00:00
matches.append(name)
else:
2015-02-11 17:12:39 +00:00
for k in context.data.index.keys():
2014-11-13 01:51:49 +00:00
if k.find(name) == 0:
matches.append(k)
2012-11-04 03:39:54 +00:00
return (matches, name)
2012-07-23 10:07:26 +00:00
2015-10-05 21:54:38 +00:00
# MODULE INTERFACE ####################################################
## Commands
2015-11-02 19:19:12 +00:00
@hook.command("anniv",
2015-10-05 21:54:38 +00:00
help="gives the remaining time before the anniversary of known people",
help_usage={
None: "Calculate the time remaining before your birthday",
"WHO": "Calculate the time remaining before WHO's birthday",
})
def cmd_anniv(msg):
(matches, name) = findName(msg)
if len(matches) == 1:
name = matches[0]
2015-02-11 17:12:39 +00:00
tyd = context.data.index[name].getDate("born")
tyd = datetime(date.today().year, tyd.month, tyd.day)
if (tyd.day == datetime.today().day and
tyd.month == datetime.today().month):
return Response(countdown_format(
2015-02-11 17:12:39 +00:00
context.data.index[name].getDate("born"), "",
2014-11-13 01:51:49 +00:00
"C'est aujourd'hui l'anniversaire de %s !"
" Il a %s. Joyeux anniversaire :)" % (name, "%s")),
msg.channel)
else:
if tyd < datetime.today():
tyd = datetime(date.today().year + 1, tyd.month, tyd.day)
return Response(countdown_format(tyd,
"Il reste %s avant l'anniversaire de %s !" % ("%s",
name), ""),
msg.channel)
else:
return Response("désolé, je ne connais pas la date d'anniversaire"
" de %s. Quand est-il né ?" % name,
2017-07-18 04:32:48 +00:00
msg.channel, msg.frm)
2014-11-13 01:51:49 +00:00
2015-11-02 19:19:12 +00:00
@hook.command("age",
2015-10-05 21:54:38 +00:00
help="Calculate age of known people",
help_usage={
None: "Calculate your age",
"WHO": "Calculate the age of WHO"
})
def cmd_age(msg):
(matches, name) = findName(msg)
if len(matches) == 1:
name = matches[0]
2015-02-11 17:12:39 +00:00
d = context.data.index[name].getDate("born")
return Response(countdown_format(d,
"%s va naître dans %s." % (name, "%s"),
"%s a %s." % (name, "%s")),
msg.channel)
else:
return Response("désolé, je ne connais pas l'âge de %s."
2017-07-18 04:32:48 +00:00
" Quand est-il né ?" % name, msg.channel, msg.frm)
return True
2014-11-13 01:51:49 +00:00
2015-10-05 21:54:38 +00:00
## Input parsing
2015-11-02 19:19:12 +00:00
@hook.ask()
def parseask(msg):
2017-07-18 04:48:15 +00:00
res = re.match(r"^(\S+)\s*('s|suis|est|is|was|were)?\s+(birthday|geburtstag|née? |nee? le|born on).*$", msg.message, re.I)
2014-08-21 13:16:25 +00:00
if res is not None:
2012-11-04 03:39:54 +00:00
try:
2017-07-18 04:48:15 +00:00
extDate = extractDate(msg.message)
2012-11-04 03:39:54 +00:00
if extDate is None or extDate.year > datetime.now().year:
return Response("la date de naissance ne paraît pas valide...",
2012-11-04 03:39:54 +00:00
msg.channel,
2017-07-18 04:32:48 +00:00
msg.frm)
2012-11-04 03:39:54 +00:00
else:
2014-08-21 13:16:25 +00:00
nick = res.group(1)
if nick == "my" or nick == "I" or nick == "i" or nick == "je" or nick == "mon" or nick == "ma":
2017-07-18 04:32:48 +00:00
nick = msg.frm
2015-02-11 17:12:39 +00:00
if nick.lower() in context.data.index:
context.data.index[nick.lower()]["born"] = extDate
2012-11-04 03:39:54 +00:00
else:
ms = ModuleState("birthday")
2014-08-21 13:16:25 +00:00
ms.setAttribute("name", nick.lower())
2012-11-04 03:39:54 +00:00
ms.setAttribute("born", extDate)
2015-02-11 17:12:39 +00:00
context.data.addChild(ms)
context.save()
return Response("ok, c'est noté, %s est né le %s"
2014-08-21 13:16:25 +00:00
% (nick, extDate.strftime("%A %d %B %Y à %H:%M")),
2012-11-04 03:39:54 +00:00
msg.channel,
2017-07-18 04:32:48 +00:00
msg.frm)
2012-11-04 03:39:54 +00:00
except:
raise IMException("la date de naissance ne paraît pas valide.")