diff --git a/README.md b/README.md index 10641f0..d13b847 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,11 @@ commands via the registerCommand function. * urllib * json * re +* dates + * icalendar + * datetime + * pytz + * urllib * messagelogger * time * shorturl diff --git a/bytebot_config.py.example b/bytebot_config.py.example index e456ca3..3c44190 100644 --- a/bytebot_config.py.example +++ b/bytebot_config.py.example @@ -27,9 +27,13 @@ BYTEBOT_PLUGINS= [ 'spacestatus', 'ircquestions', 'shorturl', + 'dates', ] BYTEBOT_PLUGIN_CONFIG = { + 'dates': { + 'url': 'http://www.google.com/calendar/ical/2eskb61g20prl65k2qd01uktis%40group.calendar.google.com/public/basic.ics', + }, 'messagelogger': { 'file': '/tmp/irc.log' }, diff --git a/plugins/dates.py b/plugins/dates.py new file mode 100644 index 0000000..80df7d0 --- /dev/null +++ b/plugins/dates.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +from plugins.plugin import Plugin +from bytebot_config import BYTEBOT_PLUGIN_CONFIG +from icalendar import Calendar, Event +from icalendar.prop import vDDDTypes +from datetime import datetime, timedelta +from pytz import utc +from urllib import urlopen + +class dates(Plugin): + def __init__(self): + try: + BYTEBOT_PLUGIN_CONFIG['dates'].keys() + except Exception: + raise Exception('ERROR: Plugin "dates" is missing configuration!') + + pass + + def registerCommand(self, irc): + """Registers the '!dates' command to the global command list + + irc: An instance of the bytebot. Will be passed by the plugin loader + """ + + irc.registerCommand('!dates', 'Shows the next planned dates') + + def onPrivmsg(self, irc, msg, channel, user): + """Looks for a '!dates' command in messages posted to the channel and + returns a list of dates within the next week. + + irc: An instance of the bytebot. Will be passed by the plugin loader + msg: The msg sent to the channel + channel: The channels name + user: The user who sent the message + """ + + if msg.find('!dates') == -1: + return + + f = urlopen(BYTEBOT_PLUGIN_CONFIG['dates']['url']) + cal = Calendar.from_ical(f.read()) + now = datetime.now(utc).replace(hour=0, minute=0, second=0, microsecond=0) + nweek = now + timedelta(weeks=1) + found = 0 + + for ev in cal.walk(): + if ev.name != 'VEVENT': + continue + + if len(str(vDDDTypes.from_ical(ev.get('dtstart'))).split(' ')) > 1: + start = vDDDTypes.from_ical(ev.get('dtstart')).replace(tzinfo=utc) + if start < now or start > nweek: continue + else: + start = vDDDTypes.from_ical(ev.get('dtstart')) + if start < now.date() or start > nweek.date(): continue + + found += 1 + + irc.msg(channel, " %s - %s" % + (str(ev.get('dtstart').dt), + str(ev.get('summary'))) + ) + + if found == 0: + irc.msg(channel, "No dates during the next week") + + f.close()