__module_name__ = "pyChuckBot" __module_version__ = "1.0" __module_description__ = "Parses A website for 5 random chuck norris facts" import xchat, pycurl, StringIO, re, time, os 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""" h = os.popen("cat pylock") if h.read(1): os.popen2("echo 0 > pylock") p = re.compile('!chuck', 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>") 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(3) os.popen2("rm pylock") xchat.hook_print("Channel Message", reply)