4
4
from appengine_twitter import AppEngineTwitter
5
5
from basehandler import BaseHandler , h
6
6
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
+
8
26
# twitter.Api.__init__ method for override.
9
27
def twitter_api_init_gae (self ,
10
28
username = None ,
@@ -24,15 +42,26 @@ def twitter_api_init_gae(self,
24
42
self .SetCredentials (username , password )
25
43
26
44
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
+
28
52
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)
30
54
escape_user_list = []
31
55
escape_user_list .append (name )
32
- escape_user_list .append ("milkcocoa" )
33
56
34
57
# Get most corrently tweeted tweet
35
58
status = api .GetUserTimeline ()
59
+
60
+ if debug_flag :
61
+ print "Debugging..."
62
+ hoge = api .GetReplies ()
63
+ for h in hoge :
64
+ print h
36
65
37
66
for s in status :
38
67
if s .text .startswith ("RT" ):
@@ -50,7 +79,7 @@ def run(name, pswd, search_term):
50
79
results .reverse ()
51
80
flag_enable = 0
52
81
for i ,result in enumerate (results ):
53
- rt = "RT @ " + result ['from_user' ] + " " + result ['text' ]
82
+ rt = "RT [at] " + result ['from_user' ] + " " + result ['text' ]
54
83
rt_len = len (rt )
55
84
if debug_flag :
56
85
print "[Debug] rt[" + str (i )+ "]: " + rt .encode ('utf8' )
@@ -76,7 +105,7 @@ def run(name, pswd, search_term):
76
105
print "Result of my re-tweeting: " + str (gae_twitter .update (rt .encode ('utf8' )))
77
106
exit ()
78
107
79
- if recent_tweet == rt :
108
+ if recent_tweet . replace ( "@" , "[at]" ) == rt . replace ( "@" , "[at]" ) :
80
109
if debug_flag :
81
110
print "My Most Recent Tweet: " + recent_tweet .encode ('utf8' )
82
111
print "-----------------------------------------------------"
@@ -85,44 +114,39 @@ def run(name, pswd, search_term):
85
114
if flag_enable :
86
115
print "There are no tweet found that I should tweet."
87
116
exit ()
88
-
117
+ print
89
118
print "There are no tweets recently tweeted, so tweet the oldest tweet."
119
+ print
120
+ # print "results: ",
121
+ # print str(results)
90
122
for i ,result in enumerate (results ):
91
- rt = "RT @ " + result ['from_user' ] + " " + result ['text' ]
123
+ rt = "RT [at] " + result ['from_user' ] + " " + result ['text' ]
92
124
rt_len = len (rt )
93
125
if debug_flag :
94
126
print "[Debug] rt[" + str (i )+ "]: " + rt .encode ('utf8' )
95
127
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 ()
116
147
117
148
# overriding API __init__
118
149
twitter .Api .__init__ = twitter_api_init_gae
119
150
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