rewrote remember.py, improved WA scraping

This commit is contained in:
Ryan Hitchman 2010-02-01 22:41:51 -07:00
parent 30f6ead292
commit 8f2c15b730
2 changed files with 51 additions and 73 deletions

View File

@ -1,88 +1,66 @@
""" """
remember.py: written by Scaevolus 2009 remember.py: written by Scaevolus 2010
""" """
import os
import thread
import codecs
from util import hook from util import hook
def db_init(db):
db.execute("create table if not exists memory(chan, word, data, nick,"
" primary key(chan, word))")
db.commit()
lock = thread.allocate_lock() def get_memory(db, chan, word):
memory = {} row = db.execute("select data from memory where chan=? and word=lower(?)",
(chan, word)).fetchone()
if row:
def load_memory(filename, mtimes={}): return row[0].encode('utf8')
if not os.path.exists(filename): else:
return {} return None
mtime = os.stat(filename).st_mtime
if mtimes.get(filename, 0) != mtime:
mtimes[filename] = mtime
return dict((x.split(None, 1)[0].lower(), x.strip()) for x in
codecs.open(filename, 'r', 'utf-8'))
def save_memory(filename, memory):
out = codecs.open(filename, 'w', 'utf-8')
out.write('\n'.join(sorted(memory.itervalues())))
out.flush()
out.close()
def make_filename(dir, chan):
return os.path.join(dir, 'memory')
@hook.command @hook.command
def remember(bot, input): def remember(inp, nick='', chan='', db=None):
".remember <word> <data> -- maps word to data in the memory" ".remember <word> <data> -- maps word to data in the memory"
with lock: db_init(db)
filename = make_filename(bot.persist_dir, input.chan)
memory.setdefault(filename, load_memory(filename))
try: try:
head, tail = input.inp.split(None, 1) head, tail = inp.split(None, 1)
except ValueError: except ValueError:
return remember.__doc__ return remember.__doc__
low = head.lower() data = get_memory(db, chan, head)
if low not in memory[filename]: db.execute("replace into memory(chan, word, data, nick) values"
input.reply("done.") " (?,lower(?),?,?)", (chan, head, head + ' ' + tail, nick))
db.commit()
if data:
return 'forgetting that %r, remembering this instead.' % data
else: else:
input.reply('forgetting that "%s", remembering this instead.' % return 'done.'
memory[filename][low])
memory[filename][low] = input.inp
save_memory(filename, memory[filename])
@hook.command @hook.command
def forget(bot, input): def forget(inp, chan='', db=None):
".forget <word> -- forgets the mapping that word had" ".forget <word> -- forgets the mapping that word had"
with lock: if not inp:
filename = make_filename(bot.persist_dir, input.chan)
memory.setdefault(filename, load_memory(filename))
if not input.inp:
return forget.__doc__ return forget.__doc__
low = input.inp.lower() db_init(db)
if low not in memory[filename]: data = get_memory(db, chan, inp)
return "I don't know about that."
if not hasattr(input, 'chan'):
return "I won't forget anything in private."
input.say("Forgot that %s" % memory[filename][low])
del memory[filename][low]
save_memory(filename, memory[filename])
if not chan.startswith('#'):
return "I won't forget anything in private."
if data:
db.execute("delete from memory where chan=? and word=lower(?)",
(chan, inp))
db.commit()
return 'forgot that %r' % data
else:
return "I don't know about that."
@hook.command(hook='\?(.+)', prefix=False) @hook.command(hook='\?(.+)', prefix=False)
def question(bot, input): def question(inp, chan='', say=None, db=None):
"?<word> -- shows what data is associated with word" "?<word> -- shows what data is associated with word"
with lock: db_init(db)
filename = make_filename(bot.persist_dir, input.chan)
memory.setdefault(filename, load_memory(filename))
word = input.inp.split()[0].lower() data = get_memory(db, chan, inp)
if word in memory[filename]: if data:
input.say("%s" % memory[filename][word]) say(data)

View File

@ -24,7 +24,9 @@ def wolframalpha(inp):
for pod in pods: for pod in pods:
heading = pod.find('h1/span') heading = pod.find('h1/span')
if heading is not None: if heading is not None:
heading = text_content().strip() heading = heading.text_content().strip()
if heading.startswith('Input'):
continue
else: else:
continue continue
@ -38,12 +40,10 @@ def wolframalpha(inp):
if results: if results:
pod_texts.append(heading + ' ' + '|'.join(results)) pod_texts.append(heading + ' ' + '|'.join(results))
ret = '. '.join(pod_texts[1:]) # first pod is the input ret = '. '.join(pod_texts) # first pod is the input
if not ret:
if not pod_texts: if not pod_texts:
return 'no results' return 'no results'
ret = pod_texts[0] # definite integrals have only the result pod first
if len(ret) > 430: if len(ret) > 430:
ret = ret[:ret.rfind(' ', 0, 430)] ret = ret[:ret.rfind(' ', 0, 430)]