<commit_before># -*- test-case-name: vumi.workers.irc.tests.test_workers -*-

import re
from datetime import datetime

from twisted.internet.defer import inlineCallbacks
from twisted.python import log

from vumi.service import Worker
from vumi.message import Message


class IRCWorker(Worker):

    @inlineCallbacks
    def startWorker(self):
        log.msg("Starting IRC worker.")
        inbound = self.config.get('inbound', 'irc.inbound')
        outbound = self.config.get('outbound', 'irc.outbound')
        self.publisher = yield self.publish_to(outbound)
        self.consume(inbound, self.consume_message,
                     '%s.%s' % (inbound, self.name))
        self.worker_setup()
        log.msg("Started service")

    def _publish_message(self, **kwargs):
        timestamp = datetime.utcnow()

        payload = {
            "nickname": kwargs.get('nickname', 'system'),
            "server": kwargs.get('server', 'unknown'),
            "channel": kwargs.get('channel', 'unknown'),
            "message_type": kwargs.get('message_type', 'message'),
            "message_content": kwargs.get('msg', ''),
            "timestamp": timestamp.isoformat()
        }

        return self.publisher.publish_message(Message(
                recipient=self.name, **payload))

    def consume_message(self, message):
        log.msg("Consumed message %s" % message)
        data = self.process_message(message.payload)
        if data:
            self.publisher.publish_message(Message(recipient=self.name,
                                                   message=data))

    def process_message(self, data):
        return None

    def stopWorker(self):
        log.msg("Stopping IRC worker.")


class MessageLogger(IRCWorker):
    name = 'message_logger'

    def worker_setup(self):
        self.log_server = self.config.get('log_server', 'http://example.com/')

    def log(self, **kwargs):
        """Write a message to the file."""
        # utils.post_data_to_url(self.log_server, json.dumps(payload),
        #                        'application/json')<commit_msg>Uncomment and fix posting to ircarchive webapp.<commit_after># -*- test-case-name: vumi.workers.irc.tests.test_workers -*-

import re
import json
from datetime import datetime

from twisted.internet.defer import inlineCallbacks
from twisted.python import log

from vumi.service import Worker
from vumi.message import Message
from vumi.webapp.api.utils import post_data_to_url


class IRCWorker(Worker):

    @inlineCallbacks
    def startWorker(self):
        log.msg("Starting IRC worker.")
        inbound = self.config.get('inbound', 'irc.inbound')
        outbound = self.config.get('outbound', 'irc.outbound')
        self.publisher = yield self.publish_to(outbound)
        self.consume(inbound, self.consume_message,
                     '%s.%s' % (inbound, self.name))
        self.worker_setup()
        log.msg("Started service")

    def _publish_message(self, **kwargs):
        timestamp = datetime.utcnow()

        payload = {
            "nickname": kwargs.get('nickname', 'system'),
            "server": kwargs.get('server', 'unknown'),
            "channel": kwargs.get('channel', 'unknown'),
            "message_type": kwargs.get('message_type', 'message'),
            "message_content": kwargs.get('msg', ''),
            "timestamp": timestamp.isoformat()
        }

        return self.publisher.publish_message(Message(
                recipient=self.name, **payload))

    def consume_message(self, message):
        log.msg("Consumed message %s" % message)
        data = self.process_message(message.payload)
        if data:
            self.publisher.publish_message(Message(recipient=self.name,
                                                   message=data))

    def process_message(self, data):
        return None

    def stopWorker(self):
        log.msg("Stopping IRC worker.")


class MessageLogger(IRCWorker):
    name = 'message_logger'

    def worker_setup(self):
        self.log_server = self.config.get('log_server', 'http://example.com/')

    def log(self, **kwargs):
        """Write a message to the file."""
        post_data_to_url(self.log_server, json.dumps(kwargs),
                         'application/json')
{'fraction_non_alphanumeric': 0.12623413258110014, 'fraction_numerical': 0.0, 'mean_word_length': 2.4991776315789473, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, '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': '1048357', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1186, 'n_words': 251}