def __init__(self, Borg): #begin copypasta thanks http://github.com/ketralnis/redditron/blob/master/redditron.py url = 'http://www.reddit.com/comments.json?limit=100' while True: s = urlopen(url).read().decode('utf8') js = json.loads(s) cms = js['data']['children'] bodies = {} for cm in cms: cm = cm['data'] print type(cms), type(cm) if cm.get('body', None): bodies[cm['id']] = cm['body'] #end copypasta print "I knew " + ` Borg.settings.num_words ` + " words (" + ` len( Borg.lines) ` + " lines) before reading Reddit.com" # cm['body'] = buffer for k in bodies: #print cm['id'], k buff = pyborg.filter_message(bodies[cm['id']], Borg) # Learn from input try: print buff Borg.learn(buff) except KeyboardInterrupt, e: # Close database cleanly print "Premature termination :-(" print "I know " + ` Borg.settings.num_words ` + " words (" + ` len( Borg.lines) ` + " lines) now."
def __init__(self, Borg): #begin copypasta thanks http://github.com/ketralnis/redditron/blob/master/redditron.py url = 'http://www.reddit.com/comments.json?limit=100' while True: s = urlopen(url).read().decode('utf8') js = json.loads(s) cms = js['data']['children'] bodies = {} for cm in cms: cm = cm['data'] print type(cms), type(cm) if cm.get('body', None): bodies[cm['id']] = cm['body'] #end copypasta print "I knew "+`Borg.settings.num_words`+" words ("+`len(Borg.lines)`+" lines) before reading Reddit.com" # cm['body'] = buffer for k in bodies: #print cm['id'], k buff = pyborg.filter_message(bodies[cm['id']], Borg) # Learn from input try: print buff Borg.learn(buff) except KeyboardInterrupt, e: # Close database cleanly print "Premature termination :-(" print "I know "+`Borg.settings.num_words`+" words ("+`len(Borg.lines)`+" lines) now."
def __init__(self, Borg, args): print "I knew " + ` Borg.settings.num_words ` + " words (" + ` len( Borg.lines) ` + " lines) before reading " + sys.argv[1] #f = open(args[1], "r") #buffer = f.read() #f.close() counter = 0 # Grab save frequency from argv #limit = args[2] #if limit == 0: # limit = 40 for line in fileinput.input(args[1]): buff = pyborg.filter_message(line, Borg) counter = counter + 1 try: Borg.learn(buff) except KeyboardInterrupt: # Close database cleanly print "Premature termination :-(" if counter > 1000000: my_pyborg.save_all() counter = 0 # Learn from input print "I know " + ` Borg.settings.num_words ` + " words (" + ` len( Borg.lines) ` + " lines) now." del Borg
def __init__(self, Borg, args): print "I knew " + ` Borg.settings.num_words ` + " words (" + ` len( Borg.lines ) ` + " lines) before reading " + sys.argv[1] # f = open(args[1], "r") # buffer = f.read() # f.close() counter = 0 # Grab save frequency from argv # limit = args[2] # if limit == 0: # limit = 40 for line in fileinput.input(args[1]): buff = pyborg.filter_message(line, Borg) counter = counter + 1 try: Borg.learn(buff) except KeyboardInterrupt: # Close database cleanly my_pyborg.save_all() print "Premature termination :-(" if counter > 1000000: my_pyborg.save_all() counter = 0 # Learn from input print "I know " + ` Borg.settings.num_words ` + " words (" + ` len(Borg.lines) ` + " lines) now." del Borg
def handle_post(self, post): if self.settings["reddit"]["learning"]: if not self.multiplexing: post_clean = pyborg.filter_message(post["body"], self.pyborg) self.pyborg.learn(post_clean) else: raise NotImplementedError
def __init__(self, Borg, args): f = open(args[1], "r") buffer = f.read() f.close() print "I knew "+`Borg.settings.num_words`+" words ("+`len(Borg.lines)`+" lines) before reading "+sys.argv[1] buffer = pyborg.filter_message(buffer, Borg) # Learn from input try: print buffer Borg.learn(buffer) except KeyboardInterrupt, e: # Close database cleanly print "Premature termination :-("
def __init__(self, Borg, args): f = open(args[1], "r") buffer = f.read() f.close() print "I knew " + ` Borg.settings.num_words ` + " words (" + ` len( Borg.lines) ` + " lines) before reading " + sys.argv[1] buffer = pyborg.filter_message(buffer, Borg) # Learn from input try: print buffer Borg.learn(buffer) except KeyboardInterrupt, e: # Close database cleanly print "Premature termination :-("
def irc_commands(self, body, source, target, c, e): """ Special IRC commands. """ msg = "" command_list = body.split() command_list[0] = command_list[0].lower() ### User commands # Query replyrate if command_list[0] == "!replyrate" and len(command_list)==1: msg = "Reply rate is "+`self.settings.reply_chance`+"%." if command_list[0] == "!owner" and len(command_list) > 1 and source not in self.owners: if command_list[1] == self.settings.password: self.owners.append(source) self.output("You've been added to owners list", ("", source, target, c, e)) else: self.output("Try again", ("", source, target, c, e)) ### Owner commands if source in self.owners and e.source() in self.owner_mask: # Change nick if command_list[0] == "!nick": try: self.connection.nick(command_list[1]) self.settings.myname = command_list[1] except: pass # stealth mode elif command_list[0] == "!stealth": msg = "Stealth mode " if len(command_list) == 1: if self.settings.stealth == 0: msg = msg + "off" else: msg = msg + "on" else: toggle = command_list[1].lower() if toggle == "on": msg = msg + "on" self.settings.stealth = 1 else: msg = msg + "off" self.settings.stealth = 0 # filter mirc colours out? elif command_list[0] == "!nocolor" or command_list[0] == "!nocolour": msg = "obsolete command " # Allow/disallow replying to ignored nicks # (they will never be learnt from) elif command_list[0] == "!reply2ignored": msg = "Replying to ignored users " if len(command_list) == 1: if self.settings.reply2ignored == 0: msg = msg + "off" else: msg = msg + "on" else: toggle = command_list[1] if toggle == "on": msg = msg + "on" self.settings.reply2ignored = 1 else: msg = msg + "off" self.settings.reply2ignored = 0 elif command_list[0] == "!replycommandprivmsg": msg = "Replying to commands via privmsg " if len(command_list) == 1: if self.settings.replycommandprivmsg == 0: msg = msg + "off" else: msg = msg + "on" else: toggle = command_list[1] if toggle == "on": msg = msg + "on" self.settings.replycommandprivmsg = 1 else: msg = msg + "off" self.settings.replycommandprivmsg = 0 elif command_list[0] == "!loadurlmaxsize": msg = "loadurl max size " if len(command_list) == 1: msg = "loadurl max size = " + `self.settings.loadurlmaxsize` + " bytes" else: try: self.settings.loadurlmaxsize = int(command_list[1]) msg = "loadurl max size set to " + `self.settings.loadurlmaxsize` except Exception: msg = "Usage: !loadurlmaxsize <number of bytes>" elif command_list[0] == "!loadurl": if len(command_list) == 1: msg = "Usage: !loadurl <URL>" else: try: changelinecount = self.pyborg.lines changewordcount = self.pyborg.settings.num_words request = urllib2.urlopen(command_list[1]) linecount = 0 chunk = '' for line in request: chunk += line linecount += 1 if linecount > 10000: chunk += strip_tags(chunk) chunk.replace("\n", ' ') sentences = chunk.split('. ') for sentence in sentences: sentence += '.' buff = pyborg.filter_message(sentence, self.pyborg) try: self.pyborg.learn(buff) except Exception as pyborgfail: msg = "Exception: Pyborg unable to learn that URL " msg = msg + str(loadurlfail) self.pyborg.save_all() linecount = 0 chunk = '' except Exception as loadurlfail: msg = "Exception: unable to load that URL " msg = msg + str(loadurlfail) msg = "Loaded " + command_list[1] + " and learned " + `self.pyborg.settings.num_words - changewordcount` + " words" # Stop talking elif command_list[0] == "!shutup": if self.settings.speaking == 1: msg = "I'll be quiet :-(" self.settings.speaking = 0 else: msg = ":-x" # Wake up again elif command_list[0] == "!wakeup": if self.settings.speaking == 0: self.settings.speaking = 1 msg = "Whoohoo!" else: msg = "But i'm already awake..." # Join a channel or list of channels elif command_list[0] == "!join": for x in xrange(1, len(command_list)): if not command_list[x] in self.chans: msg = "Attempting to join channel %s" % command_list[x] self.chans.append(command_list[x]) c.join(command_list[x]) # Part a channel or list of channels elif command_list[0] == "!part": for x in xrange(1, len(command_list)): if command_list[x] in self.chans: msg = "Leaving channel %s" % command_list[x] self.chans.remove(command_list[x]) c.part(command_list[x]) # List channels currently on elif command_list[0] == "!chans": if len(self.channels.keys())==0: msg = "I'm currently on no channels" else: msg = "I'm currently on " channels = self.channels.keys() for x in xrange(0, len(channels)): msg = msg+channels[x]+" " # add someone to the ignore list elif command_list[0] == "!ignore": # if no arguments are given say who we are # ignoring if len(command_list) == 1: msg = "I'm ignoring " if len(self.settings.ignorelist) == 0: msg = msg + "nobody" else: for x in xrange(0, len(self.settings.ignorelist)): msg = msg + self.settings.ignorelist[x] + " " # Add everyone listed to the ignore list # eg !ignore tom dick harry else: for x in xrange(1, len(command_list)): self.settings.ignorelist.append(command_list[x].lower()) msg = "done" # remove someone from the ignore list elif command_list[0] == "!unignore": # Remove everyone listed from the ignore list # eg !unignore tom dick harry for x in xrange(1, len(command_list)): try: self.settings.ignorelist.remove(command_list[x].lower()) msg = "done" except: pass # set the quit message elif command_list[0] == "!quitmsg": if len(command_list) > 1: self.settings.quitmsg = body.split(" ", 1)[1] msg = "New quit message is \"%s\"" % self.settings.quitmsg else: msg = "Quit message is \"%s\"" % self.settings.quitmsg # make the pyborg quit elif command_list[0] == "!quit": sys.exit() # Change reply rate elif command_list[0] == "!replyrate": try: self.settings.reply_chance = int(command_list[1]) msg = "Now replying to %d%% of messages." % int(command_list[1]) except: msg = "Reply rate is %d%%." % self.settings.reply_chance #make the bot talk elif command_list[0] == "!talk": if len(command_list) >= 2: phrase="" for x in xrange (2, len (command_list)): phrase = phrase + str(command_list[x]) + " " self.output(phrase, ("", command_list[1], "", c, e)) # Save changes self.pyborg.settings.save() self.settings.save() if msg == "": return 0 else: self.output(msg, ("<none>", source, target, c, e)) return 1
if doPublic: print print "---------- doPublic" #learn some public stauff try: tweets = api.GetPublicTimeline() except Exception: print "-- couldnt get public tweets" tweets = [] for tweet in tweets: print "-- public" print tweet.text if not dontReplyPublic: replytweets[tweet.GetCreatedAtInSeconds()]=tweet.text cleantweet = pyborg.filter_message(tweet.text) pybot.learn(cleantweet) if doFriends: print print "---------- doFriends" # learns my friends words try: tweets = api.GetFriendsTimeline() except Exception: print "-- couldnt get tweets from friends" tweets = [] for tweet in tweets: if tweet.user.screen_name!=me.screen_name:
def irc_commands(self, body, source, target, c, e): """ Special IRC commands. """ msg = "" command_list = body.split() command_list[0] = command_list[0].lower() ### User commands # Query replyrate if command_list[0] == "!replyrate" and len(command_list) == 1: msg = "Reply rate is " + ` self.settings.reply_chance ` + "%." if command_list[0] == "!owner" and len( command_list) > 1 and source not in self.owners: if command_list[1] == self.settings.password: self.owners.append(source) self.output("You've been added to owners list", ("", source, target, c, e)) else: self.output("Try again", ("", source, target, c, e)) ### Owner commands if source in self.owners and e.source() in self.owner_mask: # Change nick if command_list[0] == "!nick": try: self.connection.nick(command_list[1]) self.settings.myname = command_list[1] except: pass # stealth mode elif command_list[0] == "!stealth": msg = "Stealth mode " if len(command_list) == 1: if self.settings.stealth == 0: msg = msg + "off" else: msg = msg + "on" else: toggle = command_list[1].lower() if toggle == "on": msg = msg + "on" self.settings.stealth = 1 else: msg = msg + "off" self.settings.stealth = 0 # filter mirc colours out? elif command_list[0] == "!nocolor" or command_list[ 0] == "!nocolour": msg = "obsolete command " # Allow/disallow replying to ignored nicks # (they will never be learnt from) elif command_list[0] == "!reply2ignored": msg = "Replying to ignored users " if len(command_list) == 1: if self.settings.reply2ignored == 0: msg = msg + "off" else: msg = msg + "on" else: toggle = command_list[1] if toggle == "on": msg = msg + "on" self.settings.reply2ignored = 1 else: msg = msg + "off" self.settings.reply2ignored = 0 elif command_list[0] == "!replycommandprivmsg": msg = "Replying to commands via privmsg " if len(command_list) == 1: if self.settings.replycommandprivmsg == 0: msg = msg + "off" else: msg = msg + "on" else: toggle = command_list[1] if toggle == "on": msg = msg + "on" self.settings.replycommandprivmsg = 1 else: msg = msg + "off" self.settings.replycommandprivmsg = 0 elif command_list[0] == "!loadurlmaxsize": msg = "loadurl max size " if len(command_list) == 1: msg = "loadurl max size = " + ` self.settings.loadurlmaxsize ` + " bytes" else: try: self.settings.loadurlmaxsize = int(command_list[1]) msg = "loadurl max size set to " + ` self.settings.loadurlmaxsize ` except Exception: msg = "Usage: !loadurlmaxsize <number of bytes>" elif command_list[0] == "!loadurl": if len(command_list) == 1: msg = "Usage: !loadurl <URL>" else: try: changelinecount = self.pyborg.lines changewordcount = self.pyborg.settings.num_words request = urllib2.urlopen(command_list[1]) linecount = 0 chunk = '' for line in request: chunk += line linecount += 1 if linecount > 10000: chunk += strip_tags(chunk) chunk.replace("\n", ' ') sentences = chunk.split('. ') for sentence in sentences: sentence += '.' buff = pyborg.filter_message( sentence, self.pyborg) try: self.pyborg.learn(buff) except Exception as pyborgfail: msg = "Exception: Pyborg unable to learn that URL " msg = msg + str(loadurlfail) self.pyborg.save_all() linecount = 0 chunk = '' except Exception as loadurlfail: msg = "Exception: unable to load that URL " msg = msg + str(loadurlfail) msg = "Loaded " + command_list[ 1] + " and learned " + ` self.pyborg.settings.num_words - changewordcount ` + " words" # Stop talking elif command_list[0] == "!shutup": if self.settings.speaking == 1: msg = "I'll be quiet :-(" self.settings.speaking = 0 else: msg = ":-x" # Wake up again elif command_list[0] == "!wakeup": if self.settings.speaking == 0: self.settings.speaking = 1 msg = "Whoohoo!" else: msg = "But i'm already awake..." # Join a channel or list of channels elif command_list[0] == "!join": for x in xrange(1, len(command_list)): if not command_list[x] in self.chans: msg = "Attempting to join channel %s" % command_list[x] self.chans.append(command_list[x]) c.join(command_list[x]) # Part a channel or list of channels elif command_list[0] == "!part": for x in xrange(1, len(command_list)): if command_list[x] in self.chans: msg = "Leaving channel %s" % command_list[x] self.chans.remove(command_list[x]) c.part(command_list[x]) # List channels currently on elif command_list[0] == "!chans": if len(self.channels.keys()) == 0: msg = "I'm currently on no channels" else: msg = "I'm currently on " channels = self.channels.keys() for x in xrange(0, len(channels)): msg = msg + channels[x] + " " # add someone to the ignore list elif command_list[0] == "!ignore": # if no arguments are given say who we are # ignoring if len(command_list) == 1: msg = "I'm ignoring " if len(self.settings.ignorelist) == 0: msg = msg + "nobody" else: for x in xrange(0, len(self.settings.ignorelist)): msg = msg + self.settings.ignorelist[x] + " " # Add everyone listed to the ignore list # eg !ignore tom dick harry else: for x in xrange(1, len(command_list)): self.settings.ignorelist.append( command_list[x].lower()) msg = "done" # remove someone from the ignore list elif command_list[0] == "!unignore": # Remove everyone listed from the ignore list # eg !unignore tom dick harry for x in xrange(1, len(command_list)): try: self.settings.ignorelist.remove( command_list[x].lower()) msg = "done" except: pass # set the quit message elif command_list[0] == "!quitmsg": if len(command_list) > 1: self.settings.quitmsg = body.split(" ", 1)[1] msg = "New quit message is \"%s\"" % self.settings.quitmsg else: msg = "Quit message is \"%s\"" % self.settings.quitmsg # make the pyborg quit elif command_list[0] == "!quit": sys.exit() # Change reply rate elif command_list[0] == "!replyrate": try: self.settings.reply_chance = int(command_list[1]) msg = "Now replying to %d%% of messages." % int( command_list[1]) except: msg = "Reply rate is %d%%." % self.settings.reply_chance #make the bot talk elif command_list[0] == "!talk": if len(command_list) >= 2: phrase = "" for x in xrange(2, len(command_list)): phrase = phrase + str(command_list[x]) + " " self.output(phrase, ("", command_list[1], "", c, e)) # Save changes self.pyborg.settings.save() self.settings.save() if msg == "": return 0 else: self.output(msg, ("<none>", source, target, c, e)) return 1