Skip to content

Commit

Permalink
fixed replies, added learn from followers
Browse files Browse the repository at this point in the history
  • Loading branch information
prehensile committed Mar 18, 2012
1 parent dff8829 commit 0bcc763
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
49 changes: 28 additions & 21 deletions brains.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 )

Expand Down Expand Up @@ -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()
Expand Down
20 changes: 15 additions & 5 deletions verbivorejr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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 )
Expand All @@ -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( "." )
Expand Down

0 comments on commit 0bcc763

Please sign in to comment.