Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #19 from Bytespeicher/plugins/dates
Browse files Browse the repository at this point in the history
Added dates plugin. Closes #14
  • Loading branch information
mkzero committed May 11, 2014
2 parents b0e97d7 + 9f44961 commit acab27a
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ commands via the registerCommand function.
* urllib
* json
* re
* dates
* icalendar
* datetime
* pytz
* urllib
* messagelogger
* time
* shorturl
Expand Down
4 changes: 4 additions & 0 deletions bytebot_config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
Expand Down
69 changes: 69 additions & 0 deletions plugins/dates.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit acab27a

Please sign in to comment.