<commit_before>import logging
import sys
import os

from time import sleep
import irc.bot
import irc.strings
from datetime import datetime, timezone, timedelta
import threading

import xml.etree.ElementTree


class GrenouilleIrcBot(irc.bot.SingleServerIRCBot):
    """The module of the bot responsible for the Twitch (IRC) chat.
    Listen to all pub messages and respond accordingly.

    Attributes
        grenouille_bot - The main class the module is linked to.

        sanitizer - thread ran every 3 minutes to check if the bot is still alive
        commands - list of all commands supported by the bot
        who_data - streamer names displayed by who
    """

    def __init__(self, grenouille_bot):
        self.grenouille_bot = grenouille_bot

        channel = self.grenouille_bot.config['DEFAULT']['channel']
        nickname = self.grenouille_bot.config['DEFAULT']['nickname']
        server = 'irc.chat.twitch.tv'
        password = self.grenouille_bot.config['DEFAULT']['token']
        port = 6667

        self.who_data = 'Aucune info sur le streamer actuel.'
        self.commands = {
            'grenouille': self.grenouille,
            'next': self.next,
            'now': self.now,
            'who': self.who,
            'youtube': self.youtube,
            'twitter': self.twitter
        }

        irc.bot.SingleServerIRCBot.__init__(self, [(server, port, password)], nickname, nickname)
        self.channel = channel
        self.sanitizer = threading.Timer(60, self.sanitize).start()
        self.last_ping = datetime.utcnow()

        self.twitters = xml.etree.ElementTree.parse(os.path.join(os.path.dirname(__file__), 'twitters.xml')).getroot()

    def on_welcome(self, connection, e):
        """Called when the bot is connected to the IRC server.
        """
        connection.join(self.channel)
        connection.set_rate_limit(0.5)
        connection.send_raw('CAP REQ :twitch.tv/commands')
        connection.send_raw('CAP REQ :twitch.tv/tags')
        connection.privmsg(self.channel, "Je suis la !grenouille pour vous servir.")
        logging.info('Connected to channel.')

    def sanitize(self):
        """Check that IRC twitch didn't kick us.
        If that's the case, we reconnect.
        """
        if datetime.utcnow() - self.last_ping > timedelta(minutes=7):
            self.last_ping = datetime.utcnow()
            logging.warning('Sanitizer detected lost connection. Reconnecting.')
            self.connection.disconnect()
            sleep(10)
            self.connection.reconnect()
        self.sanitizer = threading.Timer(60, self.sanitize).start()

    def on_ping(self, connection, e):
        """Save last ping for sanitizer.
        """
        self.last_ping = datetime.utcnow()

    def on_pubmsg(self, connection, e):
        """Called for every public message.
        Extract command, call it with admin info.
        """
        message = e.arguments[0]
        sender = e.source.nick
        tags = {key_value["key"]: key_value["value"] for key_value in e.tags}
        is_admin = False
        if 'user-type' in tags:
            is_admin = bool(tags['user-type'])

        if not message[0] == '!':<commit_msg>Remove entry message to avoid reconnect spam.<commit_after>import logging
import sys
import os

from time import sleep
import irc.bot
import irc.strings
from datetime import datetime, timezone, timedelta
import threading

import xml.etree.ElementTree


class GrenouilleIrcBot(irc.bot.SingleServerIRCBot):
    """The module of the bot responsible for the Twitch (IRC) chat.
    Listen to all pub messages and respond accordingly.

    Attributes
        grenouille_bot - The main class the module is linked to.

        sanitizer - thread ran every 3 minutes to check if the bot is still alive
        commands - list of all commands supported by the bot
        who_data - streamer names displayed by who
    """

    def __init__(self, grenouille_bot):
        self.grenouille_bot = grenouille_bot

        channel = self.grenouille_bot.config['DEFAULT']['channel']
        nickname = self.grenouille_bot.config['DEFAULT']['nickname']
        server = 'irc.chat.twitch.tv'
        password = self.grenouille_bot.config['DEFAULT']['token']
        port = 6667

        self.who_data = 'Aucune info sur le streamer actuel.'
        self.commands = {
            'grenouille': self.grenouille,
            'next': self.next,
            'now': self.now,
            'who': self.who,
            'youtube': self.youtube,
            'twitter': self.twitter
        }

        irc.bot.SingleServerIRCBot.__init__(self, [(server, port, password)], nickname, nickname)
        self.channel = channel
        self.sanitizer = threading.Timer(60, self.sanitize).start()
        self.last_ping = datetime.utcnow()

        self.twitters = xml.etree.ElementTree.parse(os.path.join(os.path.dirname(__file__), 'twitters.xml')).getroot()

    def on_welcome(self, connection, e):
        """Called when the bot is connected to the IRC server.
        """
        connection.join(self.channel)
        connection.set_rate_limit(0.5)
        connection.send_raw('CAP REQ :twitch.tv/commands')
        connection.send_raw('CAP REQ :twitch.tv/tags')
        logging.info('Connected to channel.')

    def sanitize(self):
        """Check that IRC twitch didn't kick us.
        If that's the case, we reconnect.
        """
        if datetime.utcnow() - self.last_ping > timedelta(minutes=7):
            self.last_ping = datetime.utcnow()
            logging.warning('Sanitizer detected lost connection. Reconnecting.')
            self.connection.disconnect()
            sleep(10)
            self.connection.reconnect()
        self.sanitizer = threading.Timer(60, self.sanitize).start()

    def on_ping(self, connection, e):
        """Save last ping for sanitizer.
        """
        self.last_ping = datetime.utcnow()

    def on_pubmsg(self, connection, e):
        """Called for every public message.
        Extract command, call it with admin info.
        """
        message = e.arguments[0]
        sender = e.source.nick
        tags = {key_value["key"]: key_value["value"] for key_value in e.tags}
        is_admin = False
        if 'user-type' in tags:
            is_admin = bool(tags['user-type'])

        if not message[0] == '!':
{'fraction_non_alphanumeric': 0.10775793964291357, 'fraction_numerical': 0.0050560910096381735, 'mean_word_length': 2.4609075997813012, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4790782', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1823, 'n_words': 496}