Skip to content

Commit

Permalink
implemented replies
Browse files Browse the repository at this point in the history
  • Loading branch information
prehensile committed Mar 13, 2012
1 parent 86c9cfb commit d1f537d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 42 deletions.
60 changes: 47 additions & 13 deletions brains.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def digest_user( api, deadline, mm_twitteruser ):

return statuses_digested

def post_tweet( api, tweet ):
if tweet is not None:
try:
# api.update_status( status=tweet )
# print tweet
logging.debug( tweet )
except Exception, err:
logging.debug( "brains.run(): error from twitter api: %s" % err )

def run( creds, force_tweet=False ):

logging.debug( "brains.run(), force_tweet is %s" % force_tweet )
Expand Down Expand Up @@ -96,27 +105,52 @@ def run( creds, force_tweet=False ):
logging.debug( "brains.run(): didn't meet tweet_chance of %2.1f" % bot_settings.tweet_chance )
return

queen = verbivorejr.VerbivoreQueen()
tweet = None
do_tweet = False

if force_tweet:
logging.debug( "brains.run(): force_tweet is set" )
tweet = queen.secrete( 130, deadline )
do_tweet = True
elif bot_settings.locquacity_onschedule:
logging.debug( "brains.run(): will tweet on schedule" )
tweet = queen.secrete( 130, deadline )
do_tweet = True
elif bot_settings.locquacity_speakonnew and statuses_digested > 0 :
logging.debug( "brains.run(): locquacity_speakonnew, statuses_digested: %s" % statuses_digested )
tweet = queen.secrete( 130, deadline )

do_tweet = True

if tweet is not None:
try:
# api.update_status( status=tweet )
# print tweet
logging.debug( tweet )
except Exception, err:
logging.debug( "brains.run(): error from twitter api: %s" % err )
if do_tweet:
queen = verbivorejr.VerbivoreQueen()
tweet = None
safety = 3
while tweet is None and safety > 0:
tweet = queen.secrete( 130, deadline )
safety = safety - 1
if tweet is not None:
post_tweet( api, tweet )

if bot_settings.locquacity_reply:

last_replied_id = creds.last_replied_id
last_replied_id = None
mentions = api.mentions( since_id=last_replied_id )

for mention in mentions:

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

if tweet is not None:
reply = "%s %s" % (reply, tweet)
post_tweet( api, reply )
last_replied_id = mention.id_str

creds.last_replied_id = last_replied_id
creds.put()

now = datetime.datetime.now()
elapsed = now - then
Expand Down
1 change: 1 addition & 0 deletions twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MMTwitterCreds( db.Model ):
token_secret = db.StringProperty()
screen_name = db.StringProperty()
owner = db.UserProperty()
last_replied_id = db.StringProperty()

class MMTwitterUser( db.Model ):
screen_name = db.StringProperty()
Expand Down
89 changes: 60 additions & 29 deletions verbivorejr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class VBWord( db.Model ):
word = db.StringProperty( required=True )
lc_word = db.StringProperty( required=True )
frequency = db.IntegerProperty( required=True, default=0 )

class VBWordForwardLink( db.Model ):
Expand All @@ -20,17 +21,22 @@ def tokenise( text ):
return tokens

def vbword_for_word( word ):
db_word = memcache.get( word )
lc_word = word.lower()
db_word = memcache.get( lc_word )
if db_word is None:
db_word = VBWord.all()
db_word.filter( "word = ", word )
db_word.filter( "lc_word = ", lc_word )
db_word = db_word.get()
if db_word is None:
db_word = VBWord( word=word )
db_word = VBWord( word=word, lc_word=lc_word )
db_word.put()
memcache.set( word, db_word, 0 )
db_word.word = word
memcache.set( lc_word, db_word, 0 )
return db_word

def word_is_special( cw ):
return cw[:1] == "@" or cw[:1] == "#" or cw[:7] == "http://"

class VerbivoreWorker:

def __init__( self ):
Expand Down Expand Up @@ -108,31 +114,33 @@ def ucfirst( string ):

class VerbivoreQueen:

def secrete( self, length, deadline ):

out = ""
done = False
db_word = vbword_for_word( "." )

def candidates_for_dbword( self, db_word ):
q = VBWordForwardLink.all()
q.filter( "root_word = ", db_word )
q.order( "-frequency" )
return q.fetch( 1000 )

def secrete_from_dbword( self, db_word, length, deadline, include_dbword=False ):
out = None
if db_word is not None:
done = False
if include_dbword:
out = db_word.word
else:
out = ""
while not done:

q = VBWordForwardLink.all()
q.filter( "root_word = ", db_word )
q.order( "-frequency" )
candidates = q.fetch( 1000 )

random.shuffle( candidates )

next_word = None
for link_candidate in candidates:
candidate_word = link_candidate.next_word
cw = candidate_word.word
if cw[:1] == "@" or cw[:1] == "#" or cw[:7] == "http://":
pass
else:
next_word = candidate_word
break
candidates = self.candidates_for_dbword( db_word )
if candidates is not None and len( candidates ) > 0:
random.shuffle( candidates )
for link_candidate in candidates:
candidate_word = link_candidate.next_word
cw = candidate_word.word
if word_is_special( cw ):
pass
else:
next_word = candidate_word
break

if next_word is not None:
word = next_word.word
Expand Down Expand Up @@ -161,10 +169,33 @@ def secrete( self, length, deadline ):
if word == ".":
done = True

# finish with a full stop
if out[-1:] != ".":
out += "."
# finish with a full stop
if out[-1:] != ".":
out += "."

return out

def secrete_reply( self, text, length, deadline):

logging.debug( "VerbivoreQueen.secrete_reply()" )

tokens = tokenise( text )
tokens.reverse()
pivot_dbword = None
for token in tokens:
logging.debug( "-> looking for matches for token: %s" % token )
db_word = vbword_for_word( token )
candidates = self.candidates_for_dbword( db_word )
if len( candidates ) > 0:
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 )
else:
return self.secrete( length, deadline )

def secrete( self, length, deadline ):
db_word = vbword_for_word( "." )
return self.secrete_from_dbword( db_word, length, deadline )

0 comments on commit d1f537d

Please sign in to comment.