summaryrefslogtreecommitdiff
path: root/JKTwitterBot/src/thebot.py
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2011-05-23 20:26:41 +0000
committerJesse Morgan <jesse@jesterpm.net>2011-05-23 20:26:41 +0000
commit615d9389e77819403794b30348d1a373f11b2232 (patch)
treedfae17db65bcea6266551816a84205fba9bdc6a6 /JKTwitterBot/src/thebot.py
parent7472ff42106549836fe6305b0bda695c07ef0f18 (diff)
Started to organize a bit. Added eliza.
Diffstat (limited to 'JKTwitterBot/src/thebot.py')
-rw-r--r--JKTwitterBot/src/thebot.py203
1 files changed, 127 insertions, 76 deletions
diff --git a/JKTwitterBot/src/thebot.py b/JKTwitterBot/src/thebot.py
index 30d3866..0a01ec0 100644
--- a/JKTwitterBot/src/thebot.py
+++ b/JKTwitterBot/src/thebot.py
@@ -15,9 +15,15 @@ import os
import time
import json
import urllib2
+import eliza
+
CONSUMER_KEY='uS6hO2sV6tDKIOeVjhnFnQ'
CONSUMER_SECRET='MEYTOS97VvlHX7K1rwHPEqVpTSqZ71HtvoK4sVuYk'
+# Setting this to true causes tweets to be printed to the screen
+# instead of tweeted.
+TEST_MODE = True
+
DEFAULT_USERNAME = 'ziggster00' # "this will need to be changed"
DEFAULT_AUTH_FILENAME = '.twitter_oauth'
DEFAULT_LASTID_FILENAME = '.twitter_lastid'
@@ -27,6 +33,106 @@ questions_status_update_File = 'question_status.txt'
make_Friends_File = 'friends.txt'
count = 0
response_count = 0
+
+def status_update(outgoing_text):
+ if not TEST_MODE:
+ print '====> Resp =', outgoing_text
+ try:
+ poster.statuses.update(status=outgoing_text)
+ except TwitterError as e:
+ print '*** Twitter returned an error:\n***%s' % e
+
+ else:
+ print '====> (TEST MODE) Resp =', outgoing_text
+
+def follow_user(user):
+ try:
+ poster.friendships.create(id=user)
+ except TwitterError as e:
+ print e
+ poster.friendships.destroy(id=user)
+ poster.friendships.create(id=user)
+
+
+def reply_to_tweets():
+ results = reader.search(q=username, since_id=lastid)['results']
+ for result in reversed(results):
+ asker = result['from_user']
+ msgid = str(result['id'])
+ incoming_text = result['text']
+
+ print " <<< " + asker + ": " + incoming_text
+
+ if incoming_text.lower().find(username) != 0:
+ print '====> No response (not directed to %s)' % (username,)
+ elif (lastid != '') and (long(msgid) <= long(lastid)):
+ print '====> No response (%s < %s)' % (msgid, lastid)
+ else:
+ # Found a tweet directed to us.
+ usedoctor = True
+
+ if usedoctor:
+ doctor_response = doctor.respond(incoming_text.strip())
+ outgoing_text = "@%s %s" % (str(asker), doctor_response)
+
+ else:
+ outgoing_text = '@%s %s' % (str(asker), str(response[response_count]))
+ response_count = (response_count + 1) % len(response);
+
+ print '====> Resp = %s' % outgoing_text
+ try:
+ status_update(outgoing_text)
+
+ except TwitterError as e:
+ print '*** Twitter returned an error:\n***%s' % e
+
+ else:
+ lastid = msgid
+ print 'Last id replied = ', lastid
+ with open(lastid_filename, 'w') as f:
+ f.write(lastid)
+
+ # Sleep for a second
+ time.sleep(1)
+
+
+def ask_questions():
+ # may want to add ask questions to friends too
+ # Ask questions specific to followers
+ connection = urllib2.urlopen('http://api.twitter.com/1/statuses/followers.json?screen_name=' + username)
+ friendship = urllib2.urlopen('http://api.twitter.com/1/statuses/friends.json?screen_name=' + username)
+ following_str = connection.read()
+ following_obj = json.loads(following_str)
+ friend_str = friendship.read()
+ friend_obj = json.loads(friend_str)
+ following_list = []
+ friend_list = []
+ for i in range(len(following_obj)):
+ followers = following_obj[i]
+ following_list.append(followers[u'screen_name'])
+ for x in range(len(friend_obj)):
+ supfriend = friend_obj[x]
+ friend_list.append(supfriend[u'screen_name'])
+ if count == len(question):
+ postnumber = 0
+ else: postnumber = count
+
+ for follow_me in friend_list:
+ if not (follow_me in following_list):
+ post = follow_me + ' ' + question[postnumber]
+ #status_update(post) #May want to ask everyone questions regardless of friendship
+ time.sleep(1)
+ postnumber = postnumber + 1
+ if (follow_me in following_list):#Left open if we want to make specific questions to friends
+ print follow_me
+ if postnumber == len(question):
+ postnumber = 0
+
+
+
+#########################
+# Execution starts here #
+#########################
if __name__ == '__main__':
parser = OptionParser()
@@ -81,89 +187,34 @@ if __name__ == '__main__':
with open(make_Friends_File, 'r') as f:
friends = pickle.load(f)
- def status_update(outgoing_text):
- print '====> Resp =', outgoing_text
- try:
- poster.statuses.update(status=outgoing_text)
- except TwitterError as e:
- print '*** Twitter returned an error:\n***%s' % e
- def follow_user(user):
- try:
- poster.friendships.create(id=user)
- except TwitterError as e:
- print e
- poster.friendships.destroy(id=user)
- poster.friendships.create(id=user)
+ # Prepare Eliza (code from the_shrink.py)
+ doctor = eliza.eliza()
+
# commented out so all friends are not added
#for friend in friends: # adds all friends from friends.txt
# follow_user(friend)
while True:
- results = reader.search(q=username, since_id=lastid)['results']
- for result in reversed(results):
- if (response_count == len(response)-1):
- response_count = 0
- asker = result['from_user']
- msgid = str(result['id'])
- incoming_text = result['text']
- print " <<< " + asker + ": " + incoming_text
- if incoming_text.lower().find(username) != 0:
- print '====> No response (not directed to %s)' % (username,)
- elif (lastid != '') and (long(msgid) <= long(lastid)):
- print '====> No response (%s < %s)' % (msgid, lastid)
- else:
- outgoing_text = 'RT @' + str(asker)+' '+ str(response[response_count])
- response_count = response_count+1
- print '====> Resp = %s' % outgoing_text
- try:
- print outgoing_text
- #status_update(outgoing_text)
- except TwitterError as e:
- print '*** Twitter returned an error:\n***%s' % e
- else:
- lastid = msgid
- print 'Last id replied = ', lastid
- with open(lastid_filename, 'w') as f:
- f.write(lastid)
- time.sleep(1)
- if (count == len(tweet)-1):
- count = 0
+ # Reply to tweets directed to us
+ reply_to_tweets()
+
+ # Send out a generic tweet
print count
- print tweet[count]
- #status_update(tweet[count]) # post a status update
+ status_update(tweet[count]) # post a status update
+ count = (count + 1) % len(tweet);
+
+ # Sleep for a bit to add some realism.
print 'Now sleeping... \n'
time.sleep(1) # set at 5min but is at 2min
- print question[count]
- #status_update(question[count])
+
+ # Pose a question
+ status_update(question[count])
+
+ # Sleep for a bit to add some realism.
print 'Now sleeping... \n'
time.sleep(1) # set for 2min.
- # may want to add ask questions to friends too
- # Ask questions specific to followers
- connection = urllib2.urlopen('http://api.twitter.com/1/statuses/followers.json?screen_name=' + username)
- friendship = urllib2.urlopen('http://api.twitter.com/1/statuses/friends.json?screen_name=' + username)
- following_str = connection.read()
- following_obj = json.loads(following_str)
- friend_str = friendship.read()
- friend_obj = json.loads(friend_str)
- following_list = []
- friend_list = []
- for i in range(len(following_obj)):
- followers = following_obj[i]
- following_list.append(followers[u'screen_name'])
- for x in range(len(friend_obj)):
- supfriend = friend_obj[x]
- friend_list.append(supfriend[u'screen_name'])
- if count == len(question):
- postnumber = 0
- else: postnumber = count
- for follow_me in friend_list:
- if not (follow_me in following_list):
- post = follow_me + ' ' + question[postnumber]
- #status_update(post) #May want to ask everyone questions regardless of friendship
- time.sleep(1)
- postnumber = postnumber + 1
- if (follow_me in following_list):#Left open if we want to make specific questions to friends
- print follow_me
- if postnumber == len(question):
- postnumber = 0
- count = count + 1 \ No newline at end of file
+
+ ask_questions()
+
+
+ \ No newline at end of file