Skip to content

Commit 3680dd8

Browse files
author
Yohei Yasukawa
committed
Implmenet OAuth authentification instead of Basic one.
1 parent ebb7e10 commit 3680dd8

File tree

6 files changed

+995
-37
lines changed

6 files changed

+995
-37
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*~
2+
*.pyc
3+
*.dat

README

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ and you can see how it behaves by checking the following account.
99

1010
http://twitter.com/cafemiyamabot
1111

12+
13+
HOW TO SETUP:
14+
1. Register your OAuth client on Twitter
15+
URL: http://twitter.com/oauth_clients
16+
* you MUST choose 'client' for Simple Twitter Bot.
17+
18+
2. Run 'python register_pin.py' and
19+
use CONSUMER_KEY and CONSUMER_SECRET you got from the registeration.
20+
21+
3. Change setting variables(e.g. CONSUMER_KEY, CONSUMER_SECRET) in main.py
22+
23+
4. Run Google App Engine dev_server and visit the following URL
24+
URL: http://localhost:8080/cron/update
25+
* If you finish all of your setting, set debug_flag in main.py off.
26+
27+
5. Done!
28+
1229
Enjoy developing your own twitter bot!
1330

1431
Yohei Yasukawa

main.py

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,25 @@
44
from appengine_twitter import AppEngineTwitter
55
from basehandler import BaseHandler, h
66
import twitter
7-
7+
import sys, os, pickle
8+
from oauthtwitter import *
9+
10+
# User Setting and Run Twitter Bot
11+
#debug_flag = True
12+
debug_flag = False
13+
MAX_LEN = 140
14+
SEARCH_TERM = u'"Cafe Miyama" OR カフェミヤマ'
15+
CONSUMER_KEY = "???"
16+
CONSUMER_SECRET = "???"
17+
KEY_FILE_API = "api_key.dat"
18+
KEY_FILE_TWITTER = "twitter_key.dat"
19+
BOT_USERNAME = "CafeMiyamaBot"
20+
BOT_PASSWORD = "???"
21+
22+
def oauth_twitter():
23+
access_token = pickle.load(file(KEY_FILE_API))
24+
return OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
25+
826
# twitter.Api.__init__ method for override.
927
def twitter_api_init_gae(self,
1028
username=None,
@@ -24,15 +42,26 @@ def twitter_api_init_gae(self,
2442
self.SetCredentials(username, password)
2543

2644
def run(name, pswd, search_term):
27-
gae_twitter = AppEngineTwitter(name, pswd)
45+
acc_token = pickle.load(file(KEY_FILE_API))
46+
gae_twitter = AppEngineTwitter()
47+
gae_twitter.set_oauth(CONSUMER_KEY,
48+
CONSUMER_SECRET,
49+
acc_token.key,
50+
acc_token.secret)
51+
2852
results = gae_twitter.search(search_term.encode('utf8'), {'rpp': 20})
29-
api = twitter.Api(username=bot_username, password=bot_password)
53+
api = oauth_twitter() #twitter.Api(username=bot_username, password=bot_password)
3054
escape_user_list = []
3155
escape_user_list.append(name)
32-
escape_user_list.append("milkcocoa")
3356

3457
# Get most corrently tweeted tweet
3558
status = api.GetUserTimeline()
59+
60+
if debug_flag:
61+
print "Debugging..."
62+
hoge = api.GetReplies()
63+
for h in hoge:
64+
print h
3665

3766
for s in status:
3867
if s.text.startswith("RT"):
@@ -50,7 +79,7 @@ def run(name, pswd, search_term):
5079
results.reverse()
5180
flag_enable = 0
5281
for i,result in enumerate(results):
53-
rt = "RT @" + result['from_user'] + " " + result['text']
82+
rt = "RT [at]" + result['from_user'] + " " + result['text']
5483
rt_len = len(rt)
5584
if debug_flag:
5685
print "[Debug] rt["+str(i)+"]: " + rt.encode('utf8')
@@ -76,7 +105,7 @@ def run(name, pswd, search_term):
76105
print "Result of my re-tweeting: " + str(gae_twitter.update(rt.encode('utf8')))
77106
exit()
78107

79-
if recent_tweet == rt:
108+
if recent_tweet.replace("@", "[at]") == rt.replace("@", "[at]"):
80109
if debug_flag:
81110
print "My Most Recent Tweet: " + recent_tweet.encode('utf8')
82111
print "-----------------------------------------------------"
@@ -85,44 +114,39 @@ def run(name, pswd, search_term):
85114
if flag_enable:
86115
print "There are no tweet found that I should tweet."
87116
exit()
88-
117+
print
89118
print "There are no tweets recently tweeted, so tweet the oldest tweet."
119+
print
120+
# print "results: ",
121+
# print str(results)
90122
for i,result in enumerate(results):
91-
rt = "RT @" + result['from_user'] + " " + result['text']
123+
rt = "RT [at]" + result['from_user'] + " " + result['text']
92124
rt_len = len(rt)
93125
if debug_flag:
94126
print "[Debug] rt["+str(i)+"]: " + rt.encode('utf8')
95127

96-
if flag_enable:
97-
print "I am going to tweet the tweet above."
98-
if rt_len > MAX_LEN:
99-
print "But, this tweet length is longer that 140 characters, so skipped it."
100-
continue
101-
if result['from_user'] in escape_user_list:
102-
print "But, this tweet above is tweeted by Escape User, so skipped it."
103-
continue
104-
if result['text'].startswith('@'):
105-
print "But, this tweet above starts with '@', so skipped it."
106-
continue
107-
"""
108-
Retweet and exit
109-
"""
110-
if debug_flag:
111-
print "Next Tweet: "+rt.encode('utf8')
112-
else:
113-
print "I have tweeted: "+rt.encode('utf8')
114-
print "Result of my re-tweeting: " + str(gae_twitter.update(rt.encode('utf8')))
115-
exit()
128+
print "I am going to tweet the tweet above."
129+
if rt_len > MAX_LEN:
130+
print "But, this tweet length is longer that 140 characters, so skipped it."
131+
continue
132+
if result['from_user'] in escape_user_list:
133+
print "But, this tweet above is tweeted by Escape User, so skipped it."
134+
continue
135+
if result['text'].startswith('@'):
136+
print "But, this tweet above starts with '@', so skipped it."
137+
continue
138+
"""
139+
Retweet and exit
140+
"""
141+
if debug_flag:
142+
print "Next Tweet: "+rt.encode('utf8')
143+
else:
144+
print "I have tweeted: "+rt.encode('utf8')
145+
print "Result of my re-tweeting: " + str(gae_twitter.update(rt.encode('utf8')))
146+
exit()
116147

117148
# overriding API __init__
118149
twitter.Api.__init__ = twitter_api_init_gae
119150

120-
# User Setting and Run Twitter Bot
121-
debug_flag = True
122-
#debug_flag = False
123-
bot_username = 'CafeMiyamaBot'
124-
bot_password = '???'
125-
br = "<br>"
126-
MAX_LEN = 140
127-
search_term = u'"Cafe Miyama" OR カフェミヤマ'
128-
run(bot_username, bot_password, search_term)
151+
# Start to run
152+
run(BOT_USERNAME, BOT_PASSWORD, SEARCH_TERM)

0 commit comments

Comments
 (0)