From cd1072f2aa51157112a9d210f7144cec5a762171 Mon Sep 17 00:00:00 2001 From: ngharo Date: Fri, 29 Jun 2018 23:37:52 -0500 Subject: WIP --- bot.py | 95 --------------------------------------------------------------- main.py | 41 --------------------------- shared.py | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 136 deletions(-) delete mode 100644 bot.py delete mode 100644 main.py create mode 100644 shared.py diff --git a/bot.py b/bot.py deleted file mode 100644 index 3a5e664..0000000 --- a/bot.py +++ /dev/null @@ -1,95 +0,0 @@ -import time -import threading -import irctk -import random - -class Bot(threading.Thread): - host = 'tolkien.freenode.net' - port = 6697 - ssl = True - password = None - - def __init__(self, channel, threads, queue, name, rate, per): - threading.Thread.__init__(self) - self.threads = threads - self.queue = queue - self.name = name - self.channel = channel - # following for rate limiting: - self.rate = rate - self.per = per - self.last_check = time.time() - self.allowance = rate - - self.log('starting up') - self.ready = False - - self.client = irctk.Client(nickname=name, ident=name, realname=name, password=Bot.password) - self.client.delegate = self - self.client.connect(Bot.host, Bot.port, use_tls=Bot.ssl) - - def irc_raw(self, client, line): - try: - self.log('IRC: ' + line) - except: - self.log('failed to log') - #pass - - def log(self, line): - print "{} [{}] {}".format(time.strftime('%H:%M'), self.name, line) - - def irc_registered(self, client): - self.log('joining ' + self.channel) - channel = client.add_channel(self.channel) - channel.join() - - def irc_channel_join(self, client, nick, channel): - if str(nick) == str(client.get_nickname()): - self.log('im ready :)') - self.ready = True - - def can_send(self): - current = time.time() - time_passed = current - self.last_check; - self.last_check = current - self.allowance += time_passed * (self.rate / self.per) - - if self.allowance > self.rate: - self.allowance = self.rate - - if self.allowance > 1.0: - self.allowance -= 1.0 - return True - - return False - - def run(self): - self.log('run() called') - - while True: - waiting = True - while waiting: - for thread in self.threads: - if not thread.ready: - waiting = True - break - else: - waiting = False - - time.sleep(random.random()) - - # start flushing the queue - while self.can_send(): - try: - line = self.queue.popleft() - self.client.send_line( - 'PRIVMSG ' + self.channel + ' :' + line - ) - time.sleep(0.25) - except IndexError: - # end of queue - self.log('done working') - self.client.quit('bye bye') - return - - time.sleep(0.1) diff --git a/main.py b/main.py deleted file mode 100644 index 2b8a902..0000000 --- a/main.py +++ /dev/null @@ -1,41 +0,0 @@ -import sys -import math -import collections -import zokket -from bot import Bot - -botname_prefix = 'audzx' -queue = collections.deque() -rate = 5.0 # messages -per = 8.0 # seconds -max_workers = 6 -channel = '#test123aszz' - -if len(sys.argv) == 2: - channel = sys.argv[1] - -if channel[0] != '#': - channel = '#' + channel - -linecount = 0 -for line in sys.stdin: - queue.append(line.strip()) - linecount += 1 - -workers = int(math.ceil(linecount / rate)) -if workers > max_workers: - workers = max_workers - -if workers == 0: - sys.exit(1) - -print 'starting {} worker threads'.format(workers) -threads = [] -for i in range(workers): - botname = botname_prefix + chr(97 + i) - threads.append(Bot(channel, threads, queue, botname, rate, per)) - -for bot in threads: - bot.start() - -zokket.DefaultRunloop.run() diff --git a/shared.py b/shared.py new file mode 100644 index 0000000..3fe6a4c --- /dev/null +++ b/shared.py @@ -0,0 +1,59 @@ +import sys +import queue +import pydle +import logging +import time +from lib import worker + +num_worker_threads = 2 +password = 'xxx' + +class ansibot(pydle.Client): + def on_connect(self): + self.join('#test99') + + def on_message(self, source, target, message): + if message != "!spam": + return + + global dispatched + global q + + """ + Here I thought I could have one client pickup and dispatch + work onto a queue. + + Clients then pick items off the queue send it along as a message to + the channel. + + What happens is the first client gobbles up the entire queue. I'm thinking + that client on_message callbacks are dispatched in serial. Am i right? + + This kinda thing works in testing using threading in place of ClientPool(). + Any pointers? + """ + if not dispatched: + dispatched = True + # generate some some data to push on the queue + [q.put(str(x)) for x in range(10)] + + # go to work on data + while not q.empty(): + part = q.get() + + self.message(source, part) + # give time for another client to grab items off the queue + # (theoretically) + time.sleep(0.1) + q.task_done() + +q = queue.Queue() +dispatched = False + +pool = pydle.ClientPool() +for i in range(num_worker_threads): + client = ansibot('zz' + str(i)) + pool.connect(client, 'irc.foo.bar', 7001, password=password, tls=True, tls_verify=False) + +# This will make sure all clients are treated in a fair way priority-wise. +pool.handle_forever() -- cgit v1.2.3