All pastes #42688 Raw Edit

Anonymous

public text v1 · immutable
#42688 ·published 2006-02-21 19:28 UTC
rendered paste body
__module_name__ = "pyNorrisBot"
__module_version__ = "1.0"
__module_description__ = "Parses A website for 5 random chuck norris facts"

import xchat, pycurl, StringIO, re, time

def reply(word, word_eol, userdata):
    """Takes everything that is said in the chat and checks for the word google, then curls for the search terms and prints the top 5 URLS"""
    
    # Set up the regex for starting the google
    p = re.compile('!norris', re.IGNORECASE)

    # Search for google within the text
    m = p.search(word[1])

    # If triggered then parse the website
    if m:

		  c = pycurl.Curl()
        c.setopt(pycurl.URL, "http://66.111.201.60/chuck/rss.php" )
        b = StringIO.StringIO()
        c.setopt(pycurl.WRITEFUNCTION, b.write)
        c.setopt(pycurl.FOLLOWLOCATION, 1)
        c.setopt(pycurl.MAXREDIRS, 5)
        c.perform()

        # Find the links within the html
        h = re.compile("<title>(.*?\\.)<\\/title>")
        top5 = h.findall(b.getvalue())


        for i in range(1):
            try:
                xchat.command("say " + top5[i])
            except IndexError:
                if i == 0:
                    xchat.command("say I cant find a result for that, sorry")
                
            if i == 1:
                time.sleep(10)
        
xchat.hook_print("Channel Message", reply)