forked from yasulab/SimpleTwitterBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
156 lines (136 loc) · 4.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
#! -*- coding: utf-8 -*-
from appengine_twitter import AppEngineTwitter
from basehandler import BaseHandler, h
import twitter
import sys, os, pickle
from oauthtwitter import *
# User Setting and Run Twitter Bot
#debug_flag = True
debug_flag = False
MAX_LEN = 140
SEARCH_TERM = u'engfordev'
CONSUMER_KEY = "** REPLACE HERE **"
CONSUMER_SECRET = "** REPLACE HERE **"
KEY_FILE_API = "api_key.dat"
BOT_USERNAME = "engfordev"
def oauth_twitter():
access_token = pickle.load(file(KEY_FILE_API))
return OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
# twitter.Api.__init__ method for override.
def twitter_api_init_gae(self,
username=None,
password=None,
input_encoding=None,
request_headers=None):
import urllib2
from twitter import Api
self._cache = None
self._urllib = urllib2
self._cache_timeout = Api.DEFAULT_CACHE_TIMEOUT
self._InitializeRequestHeaders(request_headers)
self._InitializeUserAgent()
self._InitializeDefaultParameters()
self._input_encoding = input_encoding
self.SetCredentials(username, password)
def run(search_term):
acc_token = pickle.load(file(KEY_FILE_API))
gae_twitter = AppEngineTwitter()
gae_twitter.set_oauth(CONSUMER_KEY,
CONSUMER_SECRET,
acc_token.key,
acc_token.secret)
results = gae_twitter.search(search_term.encode('utf8'), {'rpp': 20})
api = oauth_twitter() #twitter.Api(username=bot_username, password=bot_password)
escape_user_list = []
escape_user_list.append(BOT_USERNAME)
# Get most corrently tweeted tweet
status = api.GetUserTimeline()
if debug_flag:
print "Debugging..."
hoge = api.GetReplies()
for h in hoge:
print h
for s in status:
if s.text.startswith("RT"):
recent_tweet = s.text
break
else:
recent_tweet = ""
print "The following tweet would be posted by hand, so skipped it."
print "Tweet: " + s.text.encode('utf8')
print
print "Recent Tweet: "+recent_tweet.encode('utf8')
print
# Search Most Recent Tweet
results.reverse()
flag_enable = 0
for i,result in enumerate(results):
rt = "RT @" + result['from_user'] + " " + result['text']
rt_len = len(rt)
if debug_flag:
print "[Debug] rt["+str(i)+"]: " + rt.encode('utf8')
if flag_enable:
print "I am going to tweet the tweet above."
if rt_len > MAX_LEN:
print "But, this tweet length is longer that 140 characters, so skipped it."
continue
if result['from_user'] in escape_user_list:
print "But, this tweet above is tweeted by Escape User, so skipped it."
continue
if (not result['text'].startswith('@'+BOT_USERNAME)) and result['text'].startswith('@'):
print "But, this tweet above starts with '@', so skipped it."
continue
"""
Retweet and exit
"""
if debug_flag:
print "Next Tweet: "+rt.encode('utf8')
else:
print "I have re-tweeted: "+rt.encode('utf8')
print "Result of my re-tweeting: " + str(gae_twitter.update(rt.encode('utf8')))
exit()
if recent_tweet == rt:
if debug_flag:
print "My Most Recent Tweet: " + recent_tweet.encode('utf8')
print "-----------------------------------------------------"
flag_enable = 1
if flag_enable:
print "There are no tweet found that I should tweet."
exit()
print
print "There are no tweets recently tweeted, so tweet the oldest tweet."
print
# print "results: ",
# print str(results)
for i,result in enumerate(results):
rt = "RT @" + result['from_user'] + " " + result['text']
rt_len = len(rt)
if debug_flag:
print "[Debug] rt["+str(i)+"]: " + rt.encode('utf8')
print "I am going to tweet the tweet above."
if rt_len > MAX_LEN:
print "But, this tweet length is longer that 140 characters, so skipped it."
continue
if result['from_user'] in escape_user_list:
print "But, this tweet above is tweeted by Escape User, so skipped it."
continue
if (not result['text'].startswith('@'+BOT_USERNAME)) and result['text'].startswith('@'):
print "But, this tweet above starts with '@', so skipped it."
continue
"""
Retweet and exit
"""
if debug_flag:
print "Next Tweet: "+rt.encode('utf8')
else:
print "I have tweeted: "+rt.encode('utf8')
status = str(gae_twitter.update(rt.encode('utf8')))
print "Result of my re-tweeting: " + status
if status != "200":
continue
exit()
# overriding API __init__
twitter.Api.__init__ = twitter_api_init_gae
# Start to run
run(SEARCH_TERM)