2010-07-28 23:47:29 -05:00
|
|
|
# Countdown - track and display time until events
|
|
|
|
# Copyright (C) 2010 Brian S. Stephan
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2010-08-01 11:55:14 -05:00
|
|
|
from ConfigParser import NoOptionError, NoSectionError
|
2010-07-29 22:43:57 -05:00
|
|
|
from datetime import datetime
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2010-07-29 22:43:57 -05:00
|
|
|
from dateutil.parser import *
|
|
|
|
from dateutil.relativedelta import *
|
|
|
|
from dateutil.tz import *
|
2010-07-30 18:34:10 -05:00
|
|
|
from extlib import irclib
|
2010-07-29 00:18:20 -05:00
|
|
|
|
|
|
|
from Module import Module
|
|
|
|
|
2010-07-29 00:04:01 -05:00
|
|
|
# Class that adds a countdown item to the bot
|
|
|
|
|
2010-07-27 20:35:01 -05:00
|
|
|
class Countdown(Module):
|
|
|
|
|
|
|
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
|
|
|
whats = what.split(' ')
|
|
|
|
if whats[0] == 'countdown' and len(whats) >= 2:
|
|
|
|
if whats[1] == 'add' and len(whats) >= 4:
|
|
|
|
item = whats[2]
|
|
|
|
target = parse(' '.join(whats[3:]), default=datetime.now().replace(tzinfo=tzlocal()))
|
2010-08-01 11:41:26 -05:00
|
|
|
if not self.config.has_section(self.__class__.__name__):
|
|
|
|
self.config.add_section(self.__class__.__name__)
|
2010-07-27 20:35:01 -05:00
|
|
|
|
2010-08-01 11:41:26 -05:00
|
|
|
self.config.set(self.__class__.__name__, item, target.astimezone(tzutc()).isoformat())
|
2010-07-27 20:35:01 -05:00
|
|
|
replystr = 'added countdown item ' + whats[2]
|
2010-07-30 00:34:57 -05:00
|
|
|
return self.reply(connection, replypath, replystr)
|
2010-07-27 20:35:01 -05:00
|
|
|
elif whats[1] == 'remove':
|
|
|
|
try:
|
2010-08-01 11:41:26 -05:00
|
|
|
if self.config.remove_option(self.__class__.__name__, whats[2]):
|
2010-07-27 20:35:01 -05:00
|
|
|
replystr = 'removed countdown item ' + whats[2]
|
2010-07-30 00:34:57 -05:00
|
|
|
return self.reply(connection, replypath, replystr)
|
2010-07-27 20:35:01 -05:00
|
|
|
except NoSectionError: pass
|
|
|
|
elif whats[1] == 'list':
|
|
|
|
try:
|
2010-08-01 11:41:26 -05:00
|
|
|
cdlist = self.config.options(self.__class__.__name__)
|
2010-08-01 12:13:38 -05:00
|
|
|
self.remove_metaoptions(cdlist)
|
2010-07-27 20:35:01 -05:00
|
|
|
cdlist.sort()
|
|
|
|
liststr = ', '.join(cdlist)
|
2010-07-30 00:34:57 -05:00
|
|
|
return self.reply(connection, replypath, liststr)
|
2010-07-27 20:35:01 -05:00
|
|
|
except NoSectionError: pass
|
|
|
|
else:
|
|
|
|
try:
|
2010-08-01 11:41:26 -05:00
|
|
|
timestr = self.config.get(self.__class__.__name__, whats[1])
|
2010-07-27 20:35:01 -05:00
|
|
|
time = parse(timestr)
|
|
|
|
rdelta = relativedelta(time, datetime.now().replace(tzinfo=tzlocal()))
|
|
|
|
relstr = whats[1] + ' will occur in '
|
|
|
|
if rdelta.years != 0:
|
|
|
|
relstr += str(rdelta.years) + ' years '
|
2010-09-08 21:39:56 -05:00
|
|
|
if rdelta.years > 1:
|
|
|
|
relstr += 's'
|
|
|
|
relstr += ', '
|
2010-07-27 20:35:01 -05:00
|
|
|
if rdelta.months != 0:
|
2010-09-08 21:39:56 -05:00
|
|
|
relstr += str(rdelta.months) + ' month'
|
|
|
|
if rdelta.months > 1:
|
|
|
|
relstr += 's'
|
|
|
|
relstr += ', '
|
2010-07-27 20:35:01 -05:00
|
|
|
if rdelta.days != 0:
|
2010-09-08 21:39:56 -05:00
|
|
|
relstr += str(rdelta.days) + ' day'
|
|
|
|
if rdelta.days > 1:
|
|
|
|
relstr += 's'
|
|
|
|
relstr += ', '
|
2010-07-27 20:35:01 -05:00
|
|
|
if rdelta.hours != 0:
|
2010-09-08 21:39:56 -05:00
|
|
|
relstr += str(rdelta.hours) + ' hour'
|
|
|
|
if rdelta.hours > 1:
|
|
|
|
relstr += 's'
|
|
|
|
relstr += ', '
|
2010-07-27 20:35:01 -05:00
|
|
|
if rdelta.minutes != 0:
|
2010-09-08 21:39:56 -05:00
|
|
|
relstr += str(rdelta.minutes) + ' minute'
|
|
|
|
if rdelta.minutes > 1:
|
|
|
|
relstr += 's'
|
|
|
|
relstr += ', '
|
2010-07-27 20:35:01 -05:00
|
|
|
if rdelta.seconds != 0:
|
2010-09-08 21:39:56 -05:00
|
|
|
relstr += str(rdelta.seconds) + ' second'
|
|
|
|
if rdelta.seconds > 1:
|
|
|
|
relstr += 's'
|
|
|
|
relstr += ', '
|
|
|
|
return self.reply(connection, replypath, relstr[0:-2])
|
2010-07-27 20:35:01 -05:00
|
|
|
except NoOptionError: pass
|
|
|
|
|
2010-07-28 23:48:47 -05:00
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
2010-07-28 00:11:58 -05:00
|
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|