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:
vylion 2017-10-14 23:31:23 +02:00
parent 8d14a55ea9
commit 5a62611267
3 changed files with 66 additions and 41 deletions

View file

@ -1,4 +1,5 @@
from task import *
class Chat(object):
def __init__(self, ident, name):
self.id = ident
@ -8,7 +9,8 @@ class Chat(object):
def addTask(self, name, repeat=0, hour=0, day=0, month=0, year=0):
t = Task(name, repeat, hour, day, month, year)
self.tasks.append(t)
print('task ' + self.tasks[-1].name + ' added')
print('new task ' + t.name + ' added:')
print(str(t) + '\n')
return t
def getTasks(self, y=0, d=0, m=0, y2=0, d2=0, m2=0):

60
main.py
View file

@ -27,9 +27,17 @@ def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def start(bot, update):
update.message.reply_text('Hello, World!!')
update.message.reply_text('Hello there!')
def addTask(bot, update,args):
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):
global chats
tchat = update.message.chat
ident = str(tchat.id)
@ -38,23 +46,27 @@ def addTask(bot, update,args):
chat = Chat(tchat.id, title)
else:
chat = chats[ident]
try:
if len(args) > 5:
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]),int(args[4]),int(args[5]))
elif len(args) > 4:
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]),int(args[4]))
elif len(args) > 3:
t = chat.addTask(args[0],args[1],int(args[2]),int(args[3]))
elif len(args) > 2:
t = chat.addTask(args[0],args[1],int(args[2]))
elif len(args) > 1:
t = chat.addTask(args[0],args[1])
# Args: name repeat hour day/month/year
if len(args) > 2:
args[2] = args[2].strip('h')
if len(args) > 3:
date = args[3].split('/')
for i in range(len(date)):
if date[i] == '':
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])
update.message.reply_text('Task ' + t.name + ' added')
chats[ident] = chat
except:
update.message.reply_text('Wrong format')
t = chat.addTask(args[0],int(args[1]),int(args[2]))
elif len(args) > 1:
t = chat.addTask(args[0],int(args[1]))
else:
t = chat.addTask(args[0])
update.message.reply_text('Task ' + t.name + ' added')
chats[ident] = chat
# except:
# update.message.reply_text('Wrong format')
def getTasksToday(bot,update):
global chats
@ -65,15 +77,13 @@ def getTasksToday(bot,update):
chat = Chat(tchat.id, title)
else:
chat = chats[ident]
update.message.reply_text('showing today tasks:')
update.message.reply_text('Today\'s tasks:')
dateNow = datetime.now()
tasks = chat.getTasks()
for task in tasks:
update.message.reply_text('leiendo task')
dateTask = task.getDate()
update.message.reply_text(task.toString())
update.message.reply_text('thats all for today')
update.message.reply_text(t)
chats[ident] = chat
def stop(bot, update):
pass
@ -91,10 +101,10 @@ def main():
# on different commands - answer in Telegram
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("about", about))
#dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("stop", stop))
# log all errors

43
task.py
View file

@ -9,21 +9,32 @@ class Task(object):
self.setDate(hour, day, month, year)
def setDate(self, h, d, m, y):
date = datetime.now()
date.replace(year=max(date.year, y))
now = datetime.now()
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):
date.replace(year=(date.year+1))
date.replace(month=max(date.month, m))
if m > 0:
while now >= datetime(date.year, m, date.day, date.hour) and not fixed:
date = date.replace(year=(date.year+1))
date = date.replace(month=m)
if d > 0 and datetime.now() > datetime(date.year, date.month, d):
_, delta = monthrange(date.year, date.month)
date += timedelta(delta) # Days of date.month
date.replace(day=max(date.day, d))
if d > 0:
if now >= datetime(date.year, date.month, d, date.hour) and not fixed:
_, delta = monthrange(date.year, date.month)
date += timedelta(delta) # Days of date.month
date = date.replace(day=d)
if h > 0 and datetime.now() > datetime(date.year, date.month, date.day, h):
date += timedelta(1)
self.date = datetime(date.year, date.month, date.day, max(date.hour, h))
if h > 0:
if now >= datetime(date.year, date.month, date.day, h) and not fixed:
date += timedelta(1)
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):
return self.date
@ -32,7 +43,9 @@ class Task(object):
return self.name
def __str__(self):
s = "Task " self.name + ":\n"
s += "Date: " + " " + str(self.date.day) + "/" + str(self.date.year) + "/" + str(self.date.month) + "\n"
s = "Task " + self.name + ":\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:
s += "Repeats every " + self.repeat + " days."
s += "Repeats every " + str(self.repeat) + " days."
return s