diff --git a/brains.py b/brains.py index 6ce98d2..88a80f7 100644 --- a/brains.py +++ b/brains.py @@ -54,6 +54,11 @@ def post_tweet( api, tweet, in_reply_to_status_id=None ): except Exception, err: logging.debug( "brains.run(): error from twitter api: %s" % err ) +def learn_from_ids( api, guru_ids, deadline ): + for guru_id in guru_ids: + guru = twitter.get_user( id_str=guru_id ) + statuses_digested += digest_user( api, deadline, guru ) + def run( creds, force_tweet=False, debug=False ): if not debug: @@ -89,9 +94,10 @@ def run( creds, force_tweet=False, debug=False ): statuses_digested = digest_user( api, deadline, guru ) elif learning_style == constants.learning_style_following: guru_ids = api.friends_ids( stringify_ids=True ) - for guru_id in guru_ids: - guru = twitter.get_user( id_str=guru_id ) - statuses_digested += digest_user( api, deadline, guru ) + learn_from_ids( api, guru_ids, deadline ) + elif learning_style == constants.learning_style_followers: + guru_ids = api.followers_ids( stringify_ids=True ) + learn_from_ids( api, guru_ids, deadline ) logging.debug( "brains.run(): digested %d new statuses" % statuses_digested ) @@ -144,24 +150,25 @@ def run( creds, force_tweet=False, debug=False ): for mention in mentions: # only reply when we've been directly addressed - if mention.text[:len(my_name)] == my_name: - - reply = "@%s" % mention.author.screen_name - tweet = None - safety = 3 - while tweet is None and safety > 0: - if datetime.datetime.now() >= deadline: - break - tweet = queen.secrete_reply( mention.text, 130 - len(reply), deadline ) - safety = safety -1 - last_replied_id = mention.id_str - - if tweet is not None: - reply = "%s %s" % (reply, tweet) - if debug: - logging.debug( "brains.run()[DEBUG MODE]: would post: %s" % reply ) - else: - post_tweet( api, reply, last_replied_id ) + #if mention.text[:len(my_name)] != my_name: + # break + + reply = "@%s" % mention.author.screen_name + tweet = None + safety = 3 + while tweet is None and safety > 0: + if datetime.datetime.now() >= deadline: + break + tweet = queen.secrete_reply( mention.text, 130 - len(reply), deadline ) + safety = safety -1 + last_replied_id = mention.id_str + + if tweet is not None: + reply = "%s %s" % (reply, tweet) + if debug: + logging.debug( "brains.run()[DEBUG MODE]: would post: %s" % reply ) + else: + post_tweet( api, reply, last_replied_id ) creds.last_replied_id = last_replied_id creds.put() diff --git a/verbivorejr.py b/verbivorejr.py index 3cd680c..197631e 100644 --- a/verbivorejr.py +++ b/verbivorejr.py @@ -182,6 +182,8 @@ def secrete_from_dbword( self, db_word, length, deadline, include_dbword=False, else: arr_out.append( next_dbword ) rightmost_dbword = next_dbword + if next_dbword.word == ".": + finished_right = True if bidirectional: # extend leftwards @@ -205,6 +207,8 @@ def secrete_from_dbword( self, db_word, length, deadline, include_dbword=False, else: arr_out.insert( 0, next_dbword ) leftmost_dbword = next_dbword + if next_dbword.word == ".": + finished_left = True # check doneness if bidirectional: @@ -235,6 +239,7 @@ def secrete_reply( self, text, length, deadline): tokens = tokenise( text ) tokens.reverse() pivot_dbword = None + reply = None for token in tokens: if not word_is_special( token ): logging.debug( "-> looking for matches for token: %s" % token ) @@ -244,11 +249,16 @@ def secrete_reply( self, text, length, deadline): pivot_dbword = db_word break - if pivot_dbword is not None: - logging.debug( "-> pivot_dbword is %s" % pivot_dbword.word ) - return self.secrete_from_dbword( pivot_dbword, length, deadline, include_dbword=True, bidirectional=True ) - else: - return self.secrete( length, deadline ) + if pivot_dbword is not None: + logging.debug( "-> pivot_dbword is %s" % pivot_dbword.word ) + reply = self.secrete_from_dbword( pivot_dbword, length, deadline, include_dbword=True, bidirectional=True ) + + if reply is not None: + break + + if reply is None: + reply = self.secrete( length, deadline ) + return reply def secrete( self, length, deadline ): db_word = vbword_for_word( "." )