Exemple #1
0
def on_message(message):
    r = re.search(r"\/rtd ([\w._\-]+) ?([\w._\-]+)?", message[u'message'])
    if not r: return

    project = r.group(1)
    term = r.group(2) or ""

    link = "http://%s.rtfd.org/%s" % (quote(project), quote(term))

    send(message['topic']['id'], link)
Exemple #2
0
def on_message(message):
    r = re.search(r"\/pep (\d+)", message['message'])
    if not r: return

    pep = r.group(1)

    link = "http://www.python.org/dev/peps/pep-%s/" % pep.rjust(4, '0')

    r = requests.get(link)
    if r.status_code == 404:
        send(message['topic']['id'], "unable to find pep %s" % pep)
    else:
        fields = re.findall('class="field-name">(.*?):.*?</th><td.*?>(.*?)<', r.content)
        msg = "\n".join("%s: %s" % tuple(map(unescape, (k,v))) for k,v in fields)
        msg += "\n" + link
        send(message['topic']['id'], msg)
Exemple #3
0
def on_message(message):
    r = re.search(r"\/karma ?([\w\-_]+)?", message['message'])
    if not r: return

    if not r.group(1):
        msg = "The 5 users with the highest karma are:\n"
        for user, karma in query("SELECT user_name, karma FROM karma ORDER BY karma DESC LIMIT 5"):
            msg += "%s: %s\n" % (user, karma)
    else:
        karma = query("SELECT karma FROM karma WHERE user_name=?", r.group(1))
        if not karma:
            msg = "Unable to find user %s" % r.group(1)
        else:
            msg = "%s: %s" % (r.group(1), karma[0][0])

    send(message['topic']['id'], msg)
Exemple #4
0
def on_message(message):
    r = re.search(r"\/eval (.+)", message['message'])
    if not r: return

    try:
        ns = {'__result': None}
        sandbox.execute("import math; __result = " + r.group(1), locals=ns)
        res = unicode(ns['__result'])
        if len(res) > 500:
            res = res[:500] + " ..."
        send(message['topic']['id'], res)
    #we're running code, any error could throw an exception
    except:
        send(message['topic']['id'], "Error running code")
        p("%s" % sys.exc_info()[0])
        p("%s" % traceback.format_exc())
Exemple #5
0
def on_message(message):
    r = re.search(r"\/karma ?([\w\-_]+)?", message['message'])
    if not r: return

    if not r.group(1):
        msg = "The 5 users with the highest karma are:\n"
        for user, karma in query(
                "SELECT user_name, karma FROM karma ORDER BY karma DESC LIMIT 5"
        ):
            msg += "%s: %s\n" % (user, karma)
    else:
        karma = query("SELECT karma FROM karma WHERE user_name=?", r.group(1))
        if not karma:
            msg = "Unable to find user %s" % r.group(1)
        else:
            msg = "%s: %s" % (r.group(1), karma[0][0])

    send(message['topic']['id'], msg)
Exemple #6
0
def on_message(message):
    r = re.search(r"\/pep (\d+)", message['message'])
    if not r: return

    pep = r.group(1)

    link = "http://www.python.org/dev/peps/pep-%s/" % pep.rjust(4, '0')

    r = requests.get(link)
    if r.status_code == 404:
        send(message['topic']['id'], "unable to find pep %s" % pep)
    else:
        fields = re.findall('class="field-name">(.*?):.*?</th><td.*?>(.*?)<',
                            r.content)
        msg = "\n".join("%s: %s" % tuple(map(unescape, (k, v)))
                        for k, v in fields)
        msg += "\n" + link
        send(message['topic']['id'], msg)
Exemple #7
0
def on_message(message):
    r = re.search(r"\/doc ([\w.]+)", message[u'message'])
    if not r: return

    thing = r.group(1)
    try:
        docs = pydoc.render_doc(bytes(thing))
        #truncate to 20 lines and note that it's been truncated
        docs = "\n".join(docs.split("\n")[:20]) + "\n<truncated>  http://readthedocs.org/search/?q=" + quote(thing)
        send(message['topic']['id'], docs)
    except ImportError:
        send(message['topic']['id'], "unable to find help on %s" % thing)
    except pydoc.ErrorDuringImport as e:
        send(message['topic']['id'], "error importing %s: %s" % (thing, e.value.message))
Exemple #8
0
def on_message(message):
    r = re.search(r"\/doc ([\w.]+)", message[u'message'])
    if not r: return

    thing = r.group(1)
    try:
        docs = pydoc.render_doc(bytes(thing))
        #truncate to 20 lines and note that it's been truncated
        docs = "\n".join(
            docs.split("\n")[:20]
        ) + "\n<truncated>  http://readthedocs.org/search/?q=" + quote(thing)
        send(message['topic']['id'], docs)
    except ImportError:
        send(message['topic']['id'], "unable to find help on %s" % thing)
    except pydoc.ErrorDuringImport as e:
        send(message['topic']['id'],
             "error importing %s: %s" % (thing, e.value.message))
Exemple #9
0
def on_message(message):
    if message[u"message"] == u"/zen":
        send(
            message['topic']['id'],
            u"The Zen of Python, by Tim Peters\n\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!"
        )
Exemple #10
0
def on_message(message):
    if message[u"message"] == u"/zen":
        send(message['topic']['id'], u"The Zen of Python, by Tim Peters\n\nBeautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.\nFlat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.\nAlthough practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.\nThere should be one-- and preferably only one --obvious way to do it.\nAlthough that way may not be obvious at first unless you're Dutch.\nNow is better than never.\nAlthough never is often better than *right* now.\nIf the implementation is hard to explain, it's a bad idea.\nIf the implementation is easy to explain, it may be a good idea.\nNamespaces are one honking great idea -- let's do more of those!")
Exemple #11
0
def text_reply(msg):
    if msg['isAt']:
        print msg
        logging.info('%s At me:%s' % (msg['ActualNickName'], msg['Text']))
        content = tuling_auto_reply(msg['Content'])
        chatbot.send(u'@%s %s' % (msg['ActualNickName'], content), msg['FromUserName'])