Commit ffcd27e1 authored by Michael Murtaugh's avatar Michael Murtaugh

ircpipebot and friends

parent 8f4baa3f
#!/bin/bash
# ensure fifo
if [ ! -e botswaller.fifo ]; then mkfifo botswaller.fifo; fi
# start the pipeline
cat botswaller.fifo | \
python ircpipebot.py --nickname botswaller | \
tee botswaller/delay.fifo | \
python -u ircfilter.py --block beatrixbotter --block botswaller --block nickolatesla --block rachmanibot --message | \
python -u delay.py --delay 0.25 | \
python -u whooshbot.py --index ~/bots/bots.index wikipedia/fats.firstperson | \
python -u delay.py --receive botswaller/delay.fifo --delay 4.0 | \
cat > botswaller.fifo
# cleanup
if [ -e botswaller.fifo ]; then rm botswaller.fifo; fi
\ No newline at end of file
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser
from time import sleep
from thread import start_new_thread
import sys, random
p = ArgumentParser()
p.add_argument("--receive", default=None, help="receive control signal from fifo/file (default: none)")
p.add_argument("--delay", type=float, default=1.0, help="delay time in seconds, default: 1.0")
p.add_argument("--random", type=float, default=0.0, help="random time in seconds, default: 0")
p.add_argument("--verbose", default=False, action="store_true", help="print messages to stderr")
args = p.parse_args()
delay_count = 0
delay_amount = 0.0
def reset_delay_amount ():
global delay_amount
delay_amount = args.delay
if args.random:
delay_amount += args.random * random.random()
def delay():
if args.verbose: print ("delay", file=sys.stderr)
global delay_count
delay_count += 1
def listen (r):
with open(r) as f:
if args.verbose: print ("delay: listening for control signal...", file=sys.stderr)
while True:
line = f.readline()
if line == '':
break
# DELAY!
delay()
# listen for control signals
if args.receive:
start_new_thread(listen, (args.receive, ))
use_delay_count = True
else:
use_delay_count = False
while True:
line = sys.stdin.readline()
if line == '':
break
line = line.decode("utf-8")
if use_delay_count:
while delay_count > 0:
sleep(args.delay)
delay_count -= 1
else:
sleep(args.delay)
reset_delay_amount()
sys.stdout.write(line.encode("utf-8"))
sys.stdout.flush()
import re, sys
from argparse import ArgumentParser
msgpat = re.compile(ur"^\((.+)\) (.+?): (.*)$")
p = ArgumentParser()
p.add_argument("--block", default=[], action="append", help="nicknames to block")
p.add_argument("--message", default=False, action="store_true", help="output message only")
args = p.parse_args()
block = set()
for b in args.block:
block.add(b)
while True:
line = sys.stdin.readline()
if line == '':
break
line = line.decode("utf-8")
m = msgpat.search(line)
if m:
time, speaker, msg = m.groups()
show = True
if speaker in block:
show = False
if show:
if args.message:
sys.stdout.write(msg.encode("utf-8")+"\n")
else:
sys.stdout.write(line.encode("utf-8"))
sys.stdout.flush()
else:
sys.stdout.write(line)
sys.stdout.flush()
from __future__ import print_function
import irc.bot
import sys, time
from thread import start_new_thread
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
class Bot (irc.bot.SingleServerIRCBot):
def __init__(self, channel, nickname, server, input, port=6667, idle=False):
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.input = input
self.channel = channel
if idle:
start_new_thread(self.idle, ())
def on_welcome(self, c, e):
c.join(self.channel)
print ("join", file=sys.stderr)
start_new_thread(self.receive, (c,))
def idle(self):
while True:
tstr = time.strftime("%H:%M:%S", time.localtime())
out = u"({0}) {1}: {2}".format(tstr, "_system", "idle")
sys.stdout.write(out.encode("utf-8")+"\n")
sys.stdout.flush()
time.sleep(1)
def receive (self, c):
if self.input == sys.stdin:
# print ("reading from stdin", file=sys.stderr)
_in = sys.stdin
else:
# print ("reading from {0}".format(self.input), file=sys.stderr)
_in = open(self.input)
while True:
line = _in.readline()
if line == '':
break
line = line.rstrip().decode("utf-8")
if line:
# print ("read line: {0}".format(line), file=sys.stderr)
# CHUNK IT
for chunk in chunks(line, 400):
c.privmsg(self.channel, chunk)
def on_pubmsg(self, c, e):
# e.target, e.source, e.arguments, e.type
msg = e.arguments[0]
tstr = time.strftime("%H:%M:%S", time.localtime())
nick = e.source.split("!", 1)[0]
out = u"({0}) {1}: {2}".format(tstr, nick, msg)
sys.stdout.write(out.encode("utf-8")+"\n")
sys.stdout.flush()
# print ("pubmsg: {0}".format(msg), file=sys.stderr)
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--server', default='localhost', help='server hostname (default: localhost)')
parser.add_argument('--port', default=6667, type=int, help='server port (default: 6667)')
parser.add_argument('--channel', default='#botopera', help='channel to join (default: #botopera)')
parser.add_argument('--nickname', default='pipebot', help='bot nickname (default: pipebot)')
parser.add_argument('--input', default=sys.stdin, help='input')
parser.add_argument('--idle', default=False, action="store_true", help='output idle messages')
args = parser.parse_args()
bot = Bot(args.channel, args.nickname, args.server, args.input, port=args.port, idle=args.idle)
bot.start()
import sys
from subprocess import PIPE, Popen
from random import choice
import json
# nb: -u ensures that the subprocess runs with no input/output buffering
# (Observed to be significant, running python 2.7.3 from the bash)
## accept list of (named) pipes ... processes
## start each up...
## feed stdin to them...
## watch output,
## maintain a "spigot" that routes one at a time
from argparse import ArgumentParser
from thread import start_new_thread
p = ArgumentParser(description="Takes multiple inputs, and combines output")
p.add_argument("--input", action="append")
args = p.parse_args()
print args.input
# control protocol to select output...
class Reader ():
def __init__(self, pname):
self.pname = pname
def start (self):
start_new_thread(self.readloop, ())
def readloop (self):
with open(self.pname) as f:
while True:
line = f.readline()
if line == '':
break
print ("<{0}> read {1}".format(self.pname, line.strip()), file=sys.stderr)
def write (self, data):
if self.stdout == None:
self.stdout = open(self.pname, "w")
self.stdout.write(data)
def on_read (reader, data):
sys.stdout.write(data)
readers = []
active_reader = None
for i in args.input:
r = Reader(i, on_read)
readers.append(r)
if active_reader == None:
active_reader = r
r.start()
while True:
line = sys.stdin.readline()
if line == '':
break
for r in readers:
r.write(line)
# adapter to collect different input bundles and select on at random (after the fact?)
# def open_proc (p):
# return Popen(["python", "-u", p], stdout=PIPE, stdin=PIPE)
# procs = [open_proc(x) for x in mods]
# while True:
# # Read a line of input
# line = sys.stdin.readline()
# if line == '':
# sys.exit(0)
# responses = []
# for i, p in enumerate(procs):
# # Send the line to subprocesses
# p.stdin.write(line)
# p.stdin.flush()
# # Read the response
# r = p.stdout.readline().rstrip()
# print i, r
# if len(r) > 0:
# responses.append(r)
# if len(responses) > 0:
# print choice(responses)
# else:
# print ""
import sys
from subprocess import PIPE, Popen
# nb: -u ensures that the subprocess runs with no input/output buffering
# (Observed to be significant, running python 2.7.3 from the bash)
dates = Popen(["python", "-u", "uppercase.py"], stdout=PIPE, stdin=PIPE)
while True:
# Read a line of input
line = sys.stdin.readline()
if line == '':
sys.exit(0)
# Send the line to subprocess
dates.stdin.write(line)
dates.stdin.flush()
# Read the response
response = dates.stdout.readline()
# Print the response
print response.rstrip()
from datetime import datetime, timedelta
now = datetime.now()
while True:
print now.strftime("%A %d %B, %Y")
now = now + timedelta(1)
import sys
from datetime import datetime, timedelta
now = datetime.now()
while True:
line = sys.stdin.readline()
if line:
print now.strftime("%A %d %B, %Y")
# sys.stdout.flush()
now = now + timedelta(1)
import sys, re
from datetime import datetime
yearpat = re.compile(r"\b\d\d\d\d\b")
while True:
line = sys.stdin.readline()
m = yearpat.search(line)
if m != None:
day = datetime(int(m.group(0)), 1, 1)
print day.strftime("The first day of %Y was a %A")
else:
print ""
import sys
from datetime import datetime
while True:
line = sys.stdin.readline()
m = yearpat.search(line)
if "hello" in line.lower():
print "Hi there!"
else:
print ""
import sys
from subprocess import PIPE, Popen
from random import choice
# nb: -u ensures that the subprocess runs with no input/output buffering
# (Observed to be significant, running python 2.7.3 from the bash)
mods = ["uppercase.py", "greetbot.py", "firstday.py"]
def open_proc (p):
return Popen(["python", "-u", p], stdout=PIPE, stdin=PIPE)
procs = [open_proc(x) for x in mods]
while True:
# Read a line of input
line = sys.stdin.readline()
if line == '':
sys.exit(0)
responses = []
for i, p in enumerate(procs):
# Send the line to subprocesses
p.stdin.write(line)
p.stdin.flush()
# Read the response
r = p.stdout.readline().rstrip()
print i, r
if len(r) > 0:
responses.append(r)
if len(responses) > 0:
print choice(responses)
else:
print ""
from subprocess import PIPE, Popen
from time import sleep
p = Popen(["python", "dates.generator.py"], stdout=PIPE)
while True:
line = p.stdout.readline()
print line.rstrip()
sleep (1)
\ No newline at end of file
import sys
for line in sys.stdin:
sys.stdout.write(' '.join(line))
import sys
from subprocess import PIPE, Popen
from random import choice
import json
# nb: -u ensures that the subprocess runs with no input/output buffering
# (Observed to be significant, running python 2.7.3 from the bash)
## accept list of (named) pipes ... processes
## start each up...
## feed stdin to them...
## watch output,
## maintain a "spigot" that routes one at a time
from argparse import ArgumentParser
from thread import start_new_thread
p = ArgumentParser(description="Takes multiple inputs, and outputs one at a time")
p.add_argument("--input", action="append")
args = p.parse_args()
print args.input
# control protocol to select output...
class Reader ():
def __init__(self, pname):
self.pname = pname
def start (self):
start_new_thread(self.readloop, ())
def readloop (self):
with open(self.pname) as f:
while True:
line = f.readline()
if line == '':
break
print ("<{0}> read {1}".format(self.pname, line.strip()), file=sys.stderr)
def write (self, data):
if self.stdout == None:
self.stdout = open(self.pname, "w")
self.stdout.write(data)
def on_read (reader, data):
if reader == active_reader:
sys.stdout.write(data)
readers = []
active_reader = None
for i in args.input:
r = Reader(i, on_read)
readers.append(r)
if active_reader == None:
active_reader = r
r.start()
while True:
line = sys.stdin.readline()
if line == '':
break
for r in readers:
r.write(line)
# adapter to collect different input bundles and select on at random (after the fact?)
# def open_proc (p):
# return Popen(["python", "-u", p], stdout=PIPE, stdin=PIPE)
# procs = [open_proc(x) for x in mods]
# while True:
# # Read a line of input
# line = sys.stdin.readline()
# if line == '':
# sys.exit(0)
# responses = []
# for i, p in enumerate(procs):
# # Send the line to subprocesses
# p.stdin.write(line)
# p.stdin.flush()
# # Read the response
# r = p.stdout.readline().rstrip()
# print i, r
# if len(r) > 0:
# responses.append(r)
# if len(responses) > 0:
# print choice(responses)
# else:
# print ""
import sys
for line in sys.stdin:
sys.stdout.write(line.upper())
from __future__ import print_function
import sys
while True:
line = sys.stdin.readline()
if line == '':
break
# print ("uppercase"+line.upper(), file=sys.stderr)
sys.stdout.write(line.upper())
from __future__ import print_function
import sys
from thread import start_new_thread
from argparse import ArgumentParser
from random import randint
p = ArgumentParser(description="Channels output to a single output, 0 = stdout, n = routen given as parameter, -1 = random")
p.add_argument("--control", default=None, help="receive control signal from fifo/file (default: none)")
p.add_argument("--random", default=-1, type=int, help="value to use to route randomly, default: -1")
p.add_argument("--start", default=0, type=int, help="initial routing value, default: 0 (stdout)")
p.add_argument("--verbose", default=False, action="store_true", help="print messages to stderr")
p.add_argument("route", nargs="*", default=[], help="files to route to")
args = p.parse_args()
route = args.start
def listen (r):
global route
with open(r) as f:
if args.verbose: print ("route: listening for control signal...", file=sys.stderr)
while True:
line = f.readline()
if line == '':
break
route = int(line.rstrip())
if args.verbose:
sys.stderr.write("route: set to {0}\n".format(route))
sys.stderr.flush()
# listen for control signals
if args.control:
start_new_thread(listen, (args.control, ))
# open routes
routes = [sys.stdout]
for r in args.route:
f = open(r, "w")
routes.append(f)
useroute = route
while True:
line = sys.stdin.readline()
if line == '':
break
line = line.decode("utf-8")
if route == args.random:
useroute = randint(0, len(routes)-1)
else:
useroute = route
routes[useroute].write(line.encode("utf-8"))
routes[useroute].flush()
# cleanup
for r in routes[1:]:
r.close()
import sys
from argparse import ArgumentParser
p = ArgumentParser("map words to routes")
args = p.parse_args()
while True:
line = sys.stdin.readline()
if line == '':
break
line = line.decode("utf-8")
if "quiet" in line:
sys.stdout.write("0\n")
sys.stdout.flush()
elif "act one" in line:
sys.stdout.write("1\n")
sys.stdout.flush()
elif "act two" in line:
sys.stdout.write("2\n")
sys.stdout.flush()
elif "act three" in line:
sys.stdout.write("3\n")
sys.stdout.flush()
from __future__ import print_function
from whoosh.index import create_in, open_dir
from whoosh.fields import *
from whoosh.analysis import StemmingAnalyzer, StopFilter
from whoosh import qparser
from whoosh.highlight import WholeFragmenter, UppercaseFormatter
from argparse import ArgumentParser
import sys, os
from random import choice
from time import sleep
p = ArgumentParser(description="A Pipebot based on whoosh")
p.add_argument("--index", default="index", help="path of index")
p.add_argument("source", nargs="+", help="source paths of text files that are indexed by line")
args = p.parse_args()
indexdir = os.path.expanduser(args.index)
if os.path.exists(indexdir) and os.path.isdir(indexdir):
ix = open_dir(indexdir)
else:
# (RE)BUILD THE INDEX
s = StopFilter()
stop_words = set(s.stops) | set(["more", "which", "get", "did", "each", "that", "were", "about", "tell", "my", "his", "her", "after", "been", "me", "i", "wa", "you", "have", "there", "where", "what", "why", "how"])
custom_ana = StemmingAnalyzer(stoplist = stop_words ) # | StopFilter(stoplist = stop_words)
schema = Schema(
text=TEXT(stored=True, analyzer=custom_ana),
years=KEYWORD(stored=True),
source=ID(stored=True)
)
os.mkdir(indexdir)
ix = create_in(args.index, schema)
writer = ix.writer()
for source in args.source: