This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Bytespeicher/plugins/dates
Added dates plugin. Closes #14
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |