<commit_before>import socket
import string
import os
from threading import Thread
from messager import Messager

class ReadOnlyIRCBot(object):
    HOST = 'irc.twitch.tv'
    PORT = 6667
    
    def __init__(self, channel, nick=None, oauth_token=None):
        self.channel = channel
        self.readbuffer = ""
        if nick is None:
            nick = channel 
        if oauth_token is None:
            oauth_token = os.getenv('TWITCH_KEY')
        self.oauth_token = oauth_token
        self.nick = nick

        self.socket = socket.socket()
        self.socket.connect((self.HOST, self.PORT))

        self.connected = False
        self._messager = Messager()
        self.chat_signal = self._messager.chat_signal

        self._thread = Thread(target=self.run)
        self._thread.setDaemon(True)
        self._thread.start()

    def _connect_to_server_helper(self):
        self._send_helper('PASS oauth:{}'.format(self.oauth_token))
        self._send_helper('NICK {}'.format(self.nick))

    def _send_helper(self, message):
        message += '\r\n'
        self.socket.send(bytes(message, 'UTF-8'))

    def run(self):
        self._connect_to_server_helper()
        while True:
            self.readbuffer = self.readbuffer + \
                    self.socket.recv(1024).decode('UTF-8')

            temp = str.split(self.readbuffer, '\n')
            self.readbuffer = temp.pop()
            print('length of temp: ', len(temp))

            for line in temp:
                line = line.rstrip()
                # TODO: switch back to using just `split()`
                line = line.split(':', 2)

                if line[0] == "PING":
                    print('ping!')
                    self.socket.send(bytes("PONG {}".format(line[1]), 
                                           "UTF-8"))
                    break
                # TODO: Just parse `PRIVMSG` 
                # NOTE: If use `split()` this has extra space on the end!!!
                elif line[1] == 'tmi.twitch.tv 376 {} '.format(self.nick):
                    self.connected = True
                    self._send_helper('JOIN #{}'.format(self.channel))
                    break
                test_line = line[1].split()
                if len(test_line) > 0:
                    if test_line[1] == 'PRIVMSG':
                        if len(line) > 1:
                            message = line[2]
                        else:
                            message = 'PLACEHOLDER'
                        self._messager.recieve_chat_data(test_line[0], message)
                for index, i in enumerate(line):
                    print(line[index])

if __name__ == '__main__':
    irc_bot = ReadOnlyIRCBot('beohoff')

<commit_msg>Cleaned up the irc client slightly. Untested<commit_after>import socket
import sys
import os
from threading import Thread
from messager import Messager
from PyQt5 import QtCore

class ReadOnlyIRCBot(QtCore.QObject):
    HOST = 'irc.twitch.tv'
    PORT = 6667
    chat_signal = QtCore.pyqtSignal(str, str) 

    def __init__(self, channel, nick=None, oauth_token=None, parent=None):
        super(ReadOnlyIRCBot, self).__init__(parent)
        if nick is None:
            nick = channel 
        if oauth_token is None:
            oauth_token = os.getenv('TWITCH_KEY')
        self.nick = nick
        self.oauth_token = oauth_token
        self.channel = channel

        # NOTE: If use `split()` in run this has extra space on the end!!!
        self.connect_message = 'tmi.twitch.tv 376 {} '.format(self.nick)
        self.socket = socket.socket()
        self.readbuffer = ""

        self.socket.connect((self.HOST, self.PORT))

        self.connected = False

        self._thread = Thread(target=self.run)
        self._thread.setDaemon(True)
        self._thread.start()

    def _connect_to_server_helper(self):
        self._send_helper('PASS oauth:{}'.format(self.oauth_token))
        self._send_helper('NICK {}'.format(self.nick))

    def _send_helper(self, message):
        message += '\r\n'
        self.socket.send(bytes(message, 'UTF-8'))

    def run(self):
        self._connect_to_server_helper()
        while True:
            self.readbuffer += self.socket.recv(1024).decode('UTF-8')

            temp = self.readbuffer.split('\n')
            self.readbuffer = temp.pop()

            # For DEBUGGING
            #print('length of temp: ', len(temp))
            for line in temp:
                line = line.rstrip()

                # TODO: switch back to using just `split()`
                line = line.split(':', 2)
                
                # Handles PING/PONG of server
                if line[0] == "PING":
                    # TODO: verify that this works other places than just in my head
                    self._send_helper('PONG {}'.format(line[1]))
                    break
                # Handles the connection method
                elif line[1] == self.connect_message:
                    self.connected = True
                    self._send_helper('JOIN #{}'.format(self.channel))
                    break

                # FIXME: Is there an option where there isn't an item in index of 1?
                # code will break if so...
                try:
                    test_line = line[1].split()
                    if len(test_line) > 0:
                        if test_line[1] == 'PRIVMSG':
                            if len(line) > 1:
                                message = line[2]
                            else:
                                message = 'PLACEHOLDER'
                            self.chat_signal.emit(test_line[0], message)
                            break
                        
                except:
                    print("Unexpected error:", sys.exc_info()[0])
                    print('Line: ', line[1])
                    print(test_line)

                for index, i in enumerate(line):
                    print(line[index])

if __name__ == '__main__':
    irc_bot = ReadOnlyIRCBot('beohoff', nick='beohoff')
    # FIXME: Add in a loop here or something
{'fraction_non_alphanumeric': 0.10801507949516473, 'fraction_numerical': 0.008359285363055236, 'mean_word_length': 1.5351059410054009, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5259664', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1654, 'n_words': 399}