<commit_before>#!/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, timezone
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

            fmt = "%d.%m.%Y %H:%M"
            timezoneEF = timezone('Europe/Berlin')

            #convert utc datetime to local timezone
            dt_utc = ev.get('dtstart').dt
            dt_local = dt_utc.astimezone(timezoneEF)
            dt_str = dt_local.strftime(fmt)

            #encode unicode string in utf8
            ucode_event_str = ev.get('summary')
            utf8_event_str = ucode_event_str.encode("utf-8")

            irc.msg(channel, "  %s - %s" % 
                        (dt_str,
                        utf8_event_str)
                   )

        if found == 0:
            irc.msg(channel, "No dates during the next week")

        f.close()<commit_msg>Fixed sorting for events. Closes #30<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import time

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, timezone
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
        data  = []

        for ev in cal.walk('VEVENT'):
            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

            fmt = "%d.%m.%Y %H:%M"
            timezoneEF = timezone('Europe/Berlin')

            #convert utc datetime to local timezone
            dt_utc = ev.get('dtstart').dt
            dt_local = dt_utc.astimezone(timezoneEF)
            dt_str = dt_local.strftime(fmt)

            #encode unicode string in utf8
            ucode_event_str = ev.get('summary')
            utf8_event_str = ucode_event_str.encode("utf-8")

            data.append({
                'datetime': dt_str,
                'info': utf8_event_str,
            })

        data = sorted(data,
                      key=lambda k: time.mktime(datetime.strptime(k['datetime'],
                                                                  "%d.%m.%Y %H:%M").timetuple()
                                               )
                     )


        for ev in data:
            irc.msg(channel, "  %s - %s" % 
                        (ev['datetime'],
                        ev['info'])
                   )

        if found == 0:
            irc.msg(channel, "No dates during the next week")

        f.close()
{'fraction_non_alphanumeric': 0.10194827007054082, 'fraction_numerical': 0.005710446758481693, 'mean_word_length': 1.731651376146789, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6296001', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1649, 'n_words': 483}