Fixed a horrible bug in date deduction
Cleaned the code up Prettied up the format for task declaration Added a /help command
This commit is contained in:
parent
8d14a55ea9
commit
5a62611267
3 changed files with 66 additions and 41 deletions
4
chat.py
4
chat.py
|
@ -1,4 +1,5 @@
|
||||||
from task import *
|
from task import *
|
||||||
|
|
||||||
class Chat(object):
|
class Chat(object):
|
||||||
def __init__(self, ident, name):
|
def __init__(self, ident, name):
|
||||||
self.id = ident
|
self.id = ident
|
||||||
|
@ -8,7 +9,8 @@ class Chat(object):
|
||||||
def addTask(self, name, repeat=0, hour=0, day=0, month=0, year=0):
|
def addTask(self, name, repeat=0, hour=0, day=0, month=0, year=0):
|
||||||
t = Task(name, repeat, hour, day, month, year)
|
t = Task(name, repeat, hour, day, month, year)
|
||||||
self.tasks.append(t)
|
self.tasks.append(t)
|
||||||
print('task ' + self.tasks[-1].name + ' added')
|
print('new task ' + t.name + ' added:')
|
||||||
|
print(str(t) + '\n')
|
||||||
return t
|
return t
|
||||||
|
|
||||||
def getTasks(self, y=0, d=0, m=0, y2=0, d2=0, m2=0):
|
def getTasks(self, y=0, d=0, m=0, y2=0, d2=0, m2=0):
|
||||||
|
|
50
main.py
50
main.py
|
@ -27,7 +27,15 @@ def error(bot, update, error):
|
||||||
logger.warn('Update "%s" caused error "%s"' % (update, error))
|
logger.warn('Update "%s" caused error "%s"' % (update, error))
|
||||||
|
|
||||||
def start(bot, update):
|
def start(bot, update):
|
||||||
update.message.reply_text('Hello, World!!')
|
update.message.reply_text('Hello there!')
|
||||||
|
|
||||||
|
def help(bot, update):
|
||||||
|
message = 'The task must be stated like this:\n'
|
||||||
|
message += '/new repeat hour day/month/year\n\n'
|
||||||
|
message += 'where __repeat__ is how many days are between each repetition, '
|
||||||
|
message += '__year__ is optional, __month__ is optional, and __day__ is '
|
||||||
|
message += 'optional if month is omitted (but the slashes aren\'t)'
|
||||||
|
update.message.reply_text(message)
|
||||||
|
|
||||||
def addTask(bot, update, args):
|
def addTask(bot, update, args):
|
||||||
global chats
|
global chats
|
||||||
|
@ -38,23 +46,27 @@ def addTask(bot, update,args):
|
||||||
chat = Chat(tchat.id, title)
|
chat = Chat(tchat.id, title)
|
||||||
else:
|
else:
|
||||||
chat = chats[ident]
|
chat = chats[ident]
|
||||||
try:
|
|
||||||
if len(args) > 5:
|
# Args: name repeat hour day/month/year
|
||||||
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]),int(args[4]),int(args[5]))
|
if len(args) > 2:
|
||||||
elif len(args) > 4:
|
args[2] = args[2].strip('h')
|
||||||
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]),int(args[4]))
|
if len(args) > 3:
|
||||||
elif len(args) > 3:
|
date = args[3].split('/')
|
||||||
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]))
|
for i in range(len(date)):
|
||||||
elif len(args) > 2:
|
if date[i] == '':
|
||||||
t = chat.addTask(args[0],args[1],int(args[2]))
|
date[i] = '0'
|
||||||
|
t = chat.addTask(args[0],int(args[1]),int(args[2]),int(date[0]),int(date[1]),int(date[2]))
|
||||||
|
else:
|
||||||
|
t = chat.addTask(args[0],int(args[1]),int(args[2]))
|
||||||
elif len(args) > 1:
|
elif len(args) > 1:
|
||||||
t = chat.addTask(args[0],args[1])
|
t = chat.addTask(args[0],int(args[1]))
|
||||||
else:
|
else:
|
||||||
t = chat.addTask(args[0])
|
t = chat.addTask(args[0])
|
||||||
update.message.reply_text('Task ' + t.name + ' added')
|
update.message.reply_text('Task ' + t.name + ' added')
|
||||||
chats[ident] = chat
|
chats[ident] = chat
|
||||||
except:
|
|
||||||
update.message.reply_text('Wrong format')
|
# except:
|
||||||
|
# update.message.reply_text('Wrong format')
|
||||||
|
|
||||||
def getTasksToday(bot,update):
|
def getTasksToday(bot,update):
|
||||||
global chats
|
global chats
|
||||||
|
@ -65,15 +77,13 @@ def getTasksToday(bot,update):
|
||||||
chat = Chat(tchat.id, title)
|
chat = Chat(tchat.id, title)
|
||||||
else:
|
else:
|
||||||
chat = chats[ident]
|
chat = chats[ident]
|
||||||
update.message.reply_text('showing today tasks:')
|
update.message.reply_text('Today\'s tasks:')
|
||||||
dateNow = datetime.now()
|
dateNow = datetime.now()
|
||||||
tasks = chat.getTasks()
|
tasks = chat.getTasks()
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
update.message.reply_text('leiendo task')
|
update.message.reply_text(t)
|
||||||
dateTask = task.getDate()
|
|
||||||
update.message.reply_text(task.toString())
|
|
||||||
update.message.reply_text('thats all for today')
|
|
||||||
chats[ident] = chat
|
chats[ident] = chat
|
||||||
|
|
||||||
def stop(bot, update):
|
def stop(bot, update):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -91,10 +101,10 @@ def main():
|
||||||
|
|
||||||
# on different commands - answer in Telegram
|
# on different commands - answer in Telegram
|
||||||
dp.add_handler(CommandHandler("start", start))
|
dp.add_handler(CommandHandler("start", start))
|
||||||
dp.add_handler(CommandHandler("tasksToday", getTasksToday))
|
dp.add_handler(CommandHandler("today", getTasksToday))
|
||||||
dp.add_handler(CommandHandler("new", addTask, pass_args=True))
|
dp.add_handler(CommandHandler("new", addTask, pass_args=True))
|
||||||
#dp.add_handler(CommandHandler("about", about))
|
#dp.add_handler(CommandHandler("about", about))
|
||||||
#dp.add_handler(CommandHandler("help", help))
|
dp.add_handler(CommandHandler("help", help))
|
||||||
dp.add_handler(CommandHandler("stop", stop))
|
dp.add_handler(CommandHandler("stop", stop))
|
||||||
|
|
||||||
# log all errors
|
# log all errors
|
||||||
|
|
37
task.py
37
task.py
|
@ -9,21 +9,32 @@ class Task(object):
|
||||||
self.setDate(hour, day, month, year)
|
self.setDate(hour, day, month, year)
|
||||||
|
|
||||||
def setDate(self, h, d, m, y):
|
def setDate(self, h, d, m, y):
|
||||||
date = datetime.now()
|
now = datetime.now()
|
||||||
date.replace(year=max(date.year, y))
|
now = datetime(now.year, now.month, now.day, now.hour)
|
||||||
|
date = datetime(now.year, now.month, now.day, now.hour)
|
||||||
|
fixed = (y > 0 and m > 0 and d > 0)
|
||||||
|
if y > 0:
|
||||||
|
date = date.replace(year=y)
|
||||||
|
|
||||||
if m > 0 and datetime.now() > datetime(date.year, m, date.day):
|
if m > 0:
|
||||||
date.replace(year=(date.year+1))
|
while now >= datetime(date.year, m, date.day, date.hour) and not fixed:
|
||||||
date.replace(month=max(date.month, m))
|
date = date.replace(year=(date.year+1))
|
||||||
|
date = date.replace(month=m)
|
||||||
|
|
||||||
if d > 0 and datetime.now() > datetime(date.year, date.month, d):
|
if d > 0:
|
||||||
|
if now >= datetime(date.year, date.month, d, date.hour) and not fixed:
|
||||||
_, delta = monthrange(date.year, date.month)
|
_, delta = monthrange(date.year, date.month)
|
||||||
date += timedelta(delta) # Days of date.month
|
date += timedelta(delta) # Days of date.month
|
||||||
date.replace(day=max(date.day, d))
|
date = date.replace(day=d)
|
||||||
|
|
||||||
if h > 0 and datetime.now() > datetime(date.year, date.month, date.day, h):
|
if h > 0:
|
||||||
|
if now >= datetime(date.year, date.month, date.day, h) and not fixed:
|
||||||
date += timedelta(1)
|
date += timedelta(1)
|
||||||
self.date = datetime(date.year, date.month, date.day, max(date.hour, h))
|
date = datetime(date.year, date.month, date.day, h)
|
||||||
|
|
||||||
|
while now >= date and self.repeat > 0:
|
||||||
|
date += timedelta(self.repeat)
|
||||||
|
self.date = date
|
||||||
|
|
||||||
def getDate(self):
|
def getDate(self):
|
||||||
return self.date
|
return self.date
|
||||||
|
@ -32,7 +43,9 @@ class Task(object):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
s = "Task " self.name + ":\n"
|
s = "Task " + self.name + ":\n"
|
||||||
s += "Date: " + " " + str(self.date.day) + "/" + str(self.date.year) + "/" + str(self.date.month) + "\n"
|
s += "Date: " + " " + str(self.date.day) + "/" + str(self.date.month) + "/" + str(self.date.year)
|
||||||
|
s += " at " + str(self.date.hour) + ":00\n"
|
||||||
if self.repeat > 0:
|
if self.repeat > 0:
|
||||||
s += "Repeats every " + self.repeat + " days."
|
s += "Repeats every " + str(self.repeat) + " days."
|
||||||
|
return s
|
||||||
|
|
Loading…
Reference in a new issue