|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Copyright (C) 2013, Cameron White |
| 4 | + |
| 5 | +import logging |
| 6 | +import feedparser |
| 7 | +from kitnirc.modular import Module |
| 8 | +from botparse import BotParse |
| 9 | +import datetime |
| 10 | +from itertools import takewhile |
| 11 | +import re |
| 12 | + |
| 13 | +_log = logging.getLogger(__name__) |
| 14 | + |
| 15 | +parser = BotParse() |
| 16 | +command_today = parser.add_command('!today') |
| 17 | +command_today.add_argument('--limit', type=int) |
| 18 | + |
| 19 | +class CalagatorBot(Module): |
| 20 | + |
| 21 | + @Module.handle("PRIVMSG") |
| 22 | + def messages(self, client, actor, recipient, message): |
| 23 | + |
| 24 | + self.client = client |
| 25 | + self.actor = actor |
| 26 | + self.recipient = recipient |
| 27 | + self.message = message |
| 28 | + |
| 29 | + config = self.controller.config |
| 30 | + |
| 31 | + # Only pay attention if addressed directly in channels |
| 32 | + try: |
| 33 | + self.args = parser.parse_args(self.message.split()) |
| 34 | + except (NameError, TypeError): |
| 35 | + _log.debug("message not reconized %r", self.message) |
| 36 | + return |
| 37 | + |
| 38 | + # Log a message to the INFO log level - see here for more details: |
| 39 | + # http://docs.python.org/2/library/logging.html |
| 40 | + _log.info("Responding to %r in %r", self.actor, self.recipient) |
| 41 | + |
| 42 | + if self.args.command == "!today": |
| 43 | + |
| 44 | + if self.args.help: |
| 45 | + messages = command_today.format_help().split('\n') |
| 46 | + else: |
| 47 | + def today_sort(entries): |
| 48 | + entries = list(takewhile( |
| 49 | + lambda x: |
| 50 | + get_start_time(x).date() <= datetime.date.today(), |
| 51 | + entries, |
| 52 | + )) |
| 53 | + entries = filter( |
| 54 | + lambda x: |
| 55 | + get_start_time(x).date() == datetime.date.today(), |
| 56 | + entries, |
| 57 | + ) |
| 58 | + return entries |
| 59 | + messages = self.get_event_messages(today_sort) |
| 60 | + |
| 61 | + elif self.args.command == "!help": |
| 62 | + messages = parser.format_help().split('\n') |
| 63 | + |
| 64 | + # send messages |
| 65 | + for message in messages: |
| 66 | + self.client.reply(self.recipient, self.actor, message) |
| 67 | + |
| 68 | + # Stop any other modules from handling this message. |
| 69 | + return True |
| 70 | + |
| 71 | + def get_event_messages(self, func=None): |
| 72 | + |
| 73 | + config = self.controller.config |
| 74 | + |
| 75 | + if self.args.limit: |
| 76 | + limit = self.args.limit |
| 77 | + elif config.has_option("calagatorbot", "limit"): |
| 78 | + try: |
| 79 | + limit = int(config.get("calagatorbot", "limit")) |
| 80 | + except TypeError: |
| 81 | + limit = None |
| 82 | + else: |
| 83 | + limit = None |
| 84 | + |
| 85 | + feeds = feedparser.parse( |
| 86 | + 'http://calagator.org/events.atom' |
| 87 | + ) |
| 88 | + entries = feeds.entries |
| 89 | + |
| 90 | + if func: |
| 91 | + entries = func(entries) |
| 92 | + |
| 93 | + if entries and limit >= 0: |
| 94 | + entries = entries[:limit] |
| 95 | + |
| 96 | + messages = [] |
| 97 | + for entry in entries: |
| 98 | + |
| 99 | + start_time = get_start_time(entry) |
| 100 | + end_time = get_end_time(entry) |
| 101 | + |
| 102 | + message = '{} - {:%a, %b %d} from {:%H:%M} to {:%H:%M} - {}'.format( |
| 103 | + entry.title, |
| 104 | + get_start_time(entry), |
| 105 | + get_start_time(entry), |
| 106 | + get_end_time(entry), |
| 107 | + entry.link, |
| 108 | + ) |
| 109 | + |
| 110 | + messages.append(message) |
| 111 | + return messages |
| 112 | + |
| 113 | +def get_start_time(entry): |
| 114 | + start_time = re.sub(r':[0-9][0-9]-[0-9][0-9]:[0-9][0-9]', '', entry.start_time) |
| 115 | + return datetime.datetime.strptime( |
| 116 | + start_time, |
| 117 | + r'%Y-%m-%dT%H:%M', |
| 118 | + ) |
| 119 | + |
| 120 | +def get_end_time(entry): |
| 121 | + end_time = re.sub(r':[0-9][0-9]-[0-9][0-9]:[0-9][0-9]', '', entry.end_time) |
| 122 | + return datetime.datetime.strptime( |
| 123 | + end_time, |
| 124 | + r'%Y-%m-%dT%H:%M', |
| 125 | + ) |
| 126 | + |
| 127 | +module = CalagatorBot |
0 commit comments