Just some new banterbot trick and changes and things

This commit is contained in:
RussellChamp committed 2016-02-04 09:41:43 -05:00
1 parent 10d2aef8d0
commit 5bd1f60a0b
24 files changed
+784 -40

No files matched your search

+42 -2
View File
@@ -19,6 +19,10 @@ from rhymesWith import getRhymes
from rhymesWith import rhymeZone
from defineWord import defWord
from rainbow import makeRainbow
import welch
import evil
import tumblr
import xkcdApropos
parser = OptionParser()
@@ -129,14 +133,35 @@ def define_word(channel, user, text):
ircsock.send("PRIVMSG " + channel + " :" + user + ": Couldn't find the definition of '" + word + "' :(\n")
else:
for entry in defs:
ircsock.send("PRIVMSG " + channel + " :" + user + ": Define '" + word + "'" + entry[0:200] + "\n")
ircsock.send("PRIVMSG " + channel + " :" + user + ": Define '" + word + "'" + entry[0:400] + "\n")
def make_rainbow(channel, user, text):
rbword = makeRainbow(text[9:])
ircsock.send("PRIVMSG " + channel + " :" + rbword + "\n")
def get_welch(channel):
ircsock.send("PRIVMSG " + channel + " :" + welch.get_thing()[0:400] + "\n")
def get_evil(channel):
evilThing = evil.get_thing();
for line in [evilThing[i:i+400] for i in range(0, len(evilThing), 400)]:
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
def get_tumble(url, channel):
tumble = tumblr.tumble(url)
for line in [tumble[i:i+400] for i in range(0, len(tumble), 400)]:
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
def get_xkcd(channel, text):
links = xkcdApropos.xkcd_links(text[6:])
joined_links = ', '.join(links)
for line in [joined_links[i:i+400] for i in range(0, len(joined_links), 400)]:
ircsock.send("PRIVMSG " + channel + " :" + line + "\n")
#res = xkcdApropos.xkcd(text[6:])
#ircsock.send("PRIVMSG " + channel + " :" + res + "\n")
def rollcall(channel):
ircsock.send("PRIVMSG "+ channel +" :U wot m8? I score all the top drawer #banter and #bantz on this channel! Find new top-shelf banter with !newbanter, !rhymes, and !define. Make your chatter #legend with !rainbow\n")
ircsock.send("PRIVMSG "+ channel +" :U wot m8? I score all the top drawer #banter and #bantz on this channel! Find new top-shelf banter with !newbanter, !rhymes, and !define. Make your chatter #legend with !rainbow and get jokes with !welch and !evil\n")
def connect(server, channel, botnick):
ircsock.connect((server, 6667))
@@ -192,6 +217,21 @@ def listen():
if ircmsg.find(":!rainbow") != -1:
make_rainbow(channel, user, messageText)
if ircmsg.find("!welch") != -1:
get_welch(channel)
if ircmsg.find("!evil") != -1:
get_evil(channel)
if ircmsg.find("!kjp") != -1:
get_tumble('http://kingjamesprogramming.tumblr.com', channel)
if ircmsg.find("!help") != -1:
get_tumble('http://thedoomthatcametopuppet.tumblr.com', channel)
if ircmsg.find("!xkcd") != -1:
get_xkcd(channel, messageText)
if ircmsg.find(":!rollcall") != -1:
rollcall(channel)
+179
View File
@@ -0,0 +1,179 @@
import urllib
import urllib2
import json as j
import sys
__version__ = 0.242
def query(query, useragent='python-duckduckgo '+str(__version__), safesearch=True, html=False, meanings=True, **kwargs):
"""
Query DuckDuckGo, returning a Results object.
Here's a query that's unlikely to change:
>>> result = query('1 + 1')
>>> result.type
'nothing'
>>> result.answer.text
'1 + 1 = 2'
>>> result.answer.type
'calc'
Keword arguments:
useragent: UserAgent to use while querying. Default: "python-duckduckgo %d" (str)
safesearch: True for on, False for off. Default: True (bool)
html: True to allow HTML in output. Default: False (bool)
meanings: True to include disambiguations in results (bool)
Any other keyword arguments are passed directly to DuckDuckGo as URL params.
""" % __version__
safesearch = '1' if safesearch else '-1'
html = '0' if html else '1'
meanings = '0' if meanings else '1'
params = {
'q': query,
'o': 'json',
'kp': safesearch,
'no_redirect': '1',
'no_html': html,
'd': meanings,
}
params.update(kwargs)
encparams = urllib.urlencode(params)
url = 'http://api.duckduckgo.com/?' + encparams
request = urllib2.Request(url, headers={'User-Agent': useragent})
response = urllib2.urlopen(request)
json = j.loads(response.read())
response.close()
return Results(json)
class Results(object):
def __init__(self, json):
self.type = {'A': 'answer', 'D': 'disambiguation',
'C': 'category', 'N': 'name',
'E': 'exclusive', '': 'nothing'}.get(json.get('Type',''), '')
self.json = json
self.api_version = None # compat
self.heading = json.get('Heading', '')
self.results = [Result(elem) for elem in json.get('Results',[])]
self.related = [Result(elem) for elem in
json.get('RelatedTopics',[])]
self.abstract = Abstract(json)
self.redirect = Redirect(json)
self.definition = Definition(json)
self.answer = Answer(json)
self.image = Image({'Result':json.get('Image','')})
class Abstract(object):
def __init__(self, json):
self.html = json.get('Abstract', '')
self.text = json.get('AbstractText', '')
self.url = json.get('AbstractURL', '')
self.source = json.get('AbstractSource')
class Redirect(object):
def __init__(self, json):
self.url = json.get('Redirect', '')
class Result(object):
def __init__(self, json):
self.topics = json.get('Topics', [])
if self.topics:
self.topics = [Result(t) for t in self.topics]
return
self.html = json.get('Result')
self.text = json.get('Text')
self.url = json.get('FirstURL')
icon_json = json.get('Icon')
if icon_json is not None:
self.icon = Image(icon_json)
else:
self.icon = None
class Image(object):
def __init__(self, json):
self.url = json.get('Result')
self.height = json.get('Height', None)
self.width = json.get('Width', None)
class Answer(object):
def __init__(self, json):
self.text = json.get('Answer')
self.type = json.get('AnswerType', '')
class Definition(object):
def __init__(self, json):
self.text = json.get('Definition','')
self.url = json.get('DefinitionURL')
self.source = json.get('DefinitionSource')
def get_zci(q, web_fallback=True, priority=['answer', 'abstract', 'related.0', 'definition'], urls=True, **kwargs):
'''A helper method to get a single (and hopefully the best) ZCI result.
priority=list can be used to set the order in which fields will be checked for answers.
Use web_fallback=True to fall back to grabbing the first web result.
passed to query. This method will fall back to 'Sorry, no results.'
if it cannot find anything.'''
ddg = query('\\'+q, **kwargs)
response = ''
for p in priority:
ps = p.split('.')
type = ps[0]
index = int(ps[1]) if len(ps) > 1 else None
result = getattr(ddg, type)
if index is not None:
if not hasattr(result, '__getitem__'): raise TypeError('%s field is not indexable' % type)
result = result[index] if len(result) > index else None
if not result: continue
if result.text: response = result.text
if result.text and hasattr(result,'url') and urls:
if result.url: response += ' (%s)' % result.url
if response: break
# if there still isn't anything, try to get the first web result
if not response and web_fallback:
if ddg.redirect.url:
response = ddg.redirect.url
# final fallback
if not response:
response = 'Sorry, no results.'
return response
def main():
if len(sys.argv) > 1:
q = query(' '.join(sys.argv[1:]))
keys = q.json.keys()
keys.sort()
for key in keys:
sys.stdout.write(key)
if type(q.json[key]) in [str,unicode]: print(':', q.json[key])
else:
sys.stdout.write('\n')
for i in q.json[key]: print('\t',i)
else:
print('Usage: %s [query]' % sys.argv[0])
+6
View File
@@ -0,0 +1,6 @@
import random
def get_thing():
file = "/home/krowbar/logs/evildata.txt"
thing = ""
return "If I Ever Become an Evil Overlord: " + random.choice(list(open(file))).rstrip()
+2 -2
View File
@@ -37,8 +37,8 @@ def make_puzzle():
elif roll == 5:
p1 = primes[random.randrange(0,len(primes))]
p2 = primes[random.randrange(0,len(primes))]
answer = str(p1) + ',' + str(p2)
puzzle += p.number_to_words(p1 * p2) + " when factored into its two primes (answer in the form of the two primes with a comman between)"
answer = str(min(p1,p2)) + ',' + str(max(p1,p2))
puzzle += p.number_to_words(p1 * p2) + " when factored into its two primes (answer in the form of the two primes with a comma between)"
puzzle += "? (Answer with numbers)"
+8 -6
View File
@@ -1,14 +1,14 @@
krowbar&^%336&^%1452540446
krowbar&^%399&^%1454593921
karlen&^%272&^%1449500011
endorphant&^%682&^%1444775660
jumblesale&^%24&^%1426171214
marcus&^%834&^%1452521269
marcus&^%877&^%1454436882
papa&^%179&^%1438878499
epicmorphism&^%5&^%1421937744
audy&^%78&^%1442345315
kc&^%18&^%1422326056
vilmibm&^%9&^%1445468087
cmr&^%1074&^%1451719260
cmr&^%1131&^%1454335852
imt&^%519&^%1424087616
cndorphant&^%788&^%1424094192
rain&^%17&^%1422310975
@@ -28,11 +28,13 @@ reppard&^%11&^%1437512059
jesse&^%6&^%1437569027
khoi&^%3&^%1438044039
sanqui&^%4&^%1438184911
endorphan&^%57&^%1450367628
endorphan&^%64&^%1452614950
cndorphbo&^%2&^%1447862524
santi&^%3&^%1447873216
insom&^%1&^%1450346059
tahnok&^%3&^%1450457276
nilsding&^%8&^%1452449452
nilsding&^%12&^%1454100197
vypr&^%3&^%1452470872
synergian&^%1&^%1452541847
synergian&^%21&^%1454583808
dheeraj&^%2&^%1454438936
demobot&^%6&^%1454439605
+2
View File
@@ -18,3 +18,5 @@
1444203916&^%cmr&^%http://tilde.town/~wiki/ircbots.html | cndorphbot:
1447252215&^%krowbar&^%#bots, the last bastion of the free
1451487092&^%krowbar&^%Bots rule! Users drool!
1453394948&^%krowbar&^%We'll leave it to the future to resolve the present
1453924207&^%krowbar&^%The best things happen when you're parsing
+1
View File
@@ -130,3 +130,4 @@
1449698715&^%vilmibm&^%hack the planet (with feels)
1451971379&^%vilmibm&^%let's talk about html & feels | welcome new users!
1452017977&^%vilmibm&^%let's talk about html & feels! | add yrself to the map of townies! http://tilde.town/~bear/where.html
1453427743&^%vilmibm&^%WHAT MOVIE SHOULD WE WATCH IN ASCII ON MOVIE NIGHT? | add yrself to the map http://tilde.town/~bear/where.html
+2 -2
View File
@@ -1,5 +1,5 @@
krowbar&^%7&^%17
vilmibm&^%0&^%19
krowbar&^%9&^%19
vilmibm&^%0&^%20
hardmath123&^%0&^%6
joe&^%1&^%1jumblesale
audiodude&^%1&^%0
+23
View File
@@ -0,0 +1,23 @@
import urllib
from bs4 import BeautifulSoup
import random
import re
def tumble(url):
#Find the max pages
soup = BeautifulSoup(urllib.urlopen(url).read(), 'html.parser')
pages = soup.findAll('span', 'page-numbers')[0].text.split('/')[1] #this could totally fail several ways
page = random.randrange(1, int(pages)+1)
#Parse a page
soup = BeautifulSoup(urllib.urlopen(url + '/page/' + str(page)).read(), 'html.parser')
article = random.choice(soup.findAll('article'))
quote = article.find('blockquote').text.replace('\n','');
if len(article.find('footer').findAll('ul')) > 1:
quote += re.sub('\n+', ' ', article.find('footer').findAll('ul')[0].text); #the hash tags
quote += '(' + re.sub('\n+', ' ', article.find('footer').findAll('ul')[1].text) + ')'; #and the date and notes
else:
quote += '(' + re.sub('\n+', ' ', article.find('footer').findAll('ul')[0].text) + ')'; #just the date and notes
return quote.encode('ascii', 'ignore')
+6
View File
@@ -0,0 +1,6 @@
import random
def get_thing():
file = "/home/krowbar/logs/welchdata.txt"
thing = ""
return "Thing Mr. Welch can no longer do in a RPG #" + random.choice(list(open(file))).rstrip()
+24
View File
@@ -0,0 +1,24 @@
import duckduckgo
import urllib
from bs4 import BeautifulSoup
def xkcd(query):
res = duckduckgo.get_zci('site:xkcd.com ' + query);
title = "";
try: #ddg returns a url for these searches. i want a title as well
title = BeautifulSoup(urllib.urlopen(res).read(), 'html.parser').title.text
except:
pass #just swallow the error. maybe the result wasn't a url or something else bad happened
return (((title + ' - ') if title else '') + res).encode('ascii', 'ignore')
def xkcd_links(query):
url = "https://duckduckgo.com/html/?q=site%3Axkcd.com+" + query.replace(' ', '+')
soup = BeautifulSoup(urllib.urlopen(url).read(), 'html.parser')
links = filter(lambda a: a[0:8] == 'xkcd.com', [a.text.strip() for a in soup.find_all("div", class_="url")])
def pretty_link(url):
data = BeautifulSoup(urllib.urlopen('http://'+url).read(), 'html.parser')
title = data.title.text if data.title else ''
return (title + ' - ' + url) if title else url
links = map(lambda url: pretty_link(url), links)
return links
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/python
import fileinput
import json
import time
import calendar
import shutil
import re
import math
import operator
MAX_NODES = 3
logfile = "/home/jumblesale/Code/irc/log"
outfile = "/home/krowbar/logs/chatBesties.json"
outCircle = "/home/krowbar/logs/chatcircle.json"
userData = {} #hash keyed by "user" that contains a hash of mentioned other users with count
nameFix = {
'jumblesal': 'jumblesale',
'hardmath1': 'kc',
'hardmath123': 'kc',
'bendorphan': 'endorphant',
'endorphan': 'endorphant',
'synergian': 'synergiance'
}
users = []
#Get a list of all user names by checking the logs for people who have said things
with open(logfile, "r") as log:
for line in log:
try:
time, user, message = line.split("\t", 3)
if nameFix.has_key(user):
user = nameFix[user]
else:
user = user.lower()
if user not in users:
users.append(user)
except ValueError:
continue #There are some bad lines in the log file that we'll ignore if we can't parse
d3data = {}
d3data['nodes'] = []
#re-read the log and this time look for instances of user names in messages
with open(logfile, "r") as log:
for line in log:
try:
time, user, message = line.split("\t", 3)
if nameFix.has_key(user):
user = nameFix[user]
else:
user = user.lower()
except ValueError:
continue #There are some bad lines in the log file that we'll ignore if we can't parse
for word in message.split(' '):
word = re.sub('[^A-Za-z0-9]+', '', word)
if word in users: #SOMEONE MENTIONED SOMEONE
if userData.has_key(user): #This user is already set up
if userData[user]['data'].has_key(word): #This user has mentioned this person before
userData[user]['data'][word] += 1
else: #This user never mentioned this person before
userData[user]['data'][word] = 1
else: #This user was never set up
userData[user] = {} #make it a dictionary!
userData[user]['data'] = {}
userData[user]['data'][word] = 1
userData[user]['id'] = len(d3data['nodes']) #so we know how to match people during the links phase
d3data['nodes'].append({"name": user, "group": 1})
if not userData.has_key(word): #check if the target has not been set up
userData[word] = {}
userData[word]['data'] = {}
userData[word]['id'] = len(d3data['nodes'])
d3data['nodes'].append({"name": word, "group": 1})
d3data['links'] = []
#Now connect all the pople to their stuff
for user, values in userData.iteritems():
besties = sorted(values['data'].items(), key=operator.itemgetter(1), reverse=True)[0:MAX_NODES] #ONLY the top 5 besties
for target, score in besties:
try:
print "Adding link for " + user + " (" + str(values['id']) + ") to " + target + " (" + str(userData[target]['id']) + ") for " + str(score)
d3data['links'].append({"source": values['id'], "target": userData[target]['id'], "value": math.ceil(math.sqrt(score))})
except KeyError:
print "Error when trying to link " + user + " to " + target
continue
if len(values['data']) > MAX_NODES:
print "...ignoring " + str(len(values['data']) - MAX_NODES) + " more connections"
d3Circle = {}
d3Circle['names'] = [''] * len(userData)
d3Circle['matrix'] = [[0] * len(userData)] * len(userData)
for user, values in userData.iteritems():
d3Circle['names'][values['id']] = user
for name, score in values['data'].iteritems():
d3Circle['matrix'][values['id']][userData[name]['id']] = score if score > 1 else 0
with open(outfile + ".tmp", "w") as tmpFile:
tmpFile.write(json.dumps(d3data))
shutil.move(outfile + ".tmp", outfile)
with open(outCircle + ".tmp", "w") as tmpFile:
tmpFile.write(json.dumps(d3Circle))
shutil.move(outCircle + ".tmp", outCircle)