forked from arvid220u/invandringsbot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwythonaccess.py
More file actions
235 lines (193 loc) · 7.34 KB
/
twythonaccess.py
File metadata and controls
235 lines (193 loc) · 7.34 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# This module provides the api twython object, which is used to access the api
# import time, to enable the sleep function
import time
# Import twython
from twython import Twython
from twython import Twython
# import the api keys
import apikeys
# import threading, to schedule the reset
from threading import Timer
# import datetime
from datetime import datetime
# import setup to get the screen name
import setup
import random
import follow
import content
# time of ast request is used to be able to reset the requests,
# if more than 16 minutes have elapsed between the requests
# get the utc time to not be bothered by summer time
time_of_last_request = datetime.utcnow()
user_list = None
def get_followers_count():
followers_count = 0;
try:
profile = authorize().show_user(screen_name=apikeys.screen_name)
followers_count = int(profile['followers_count'])
except Exception, e:
print "Exception in twythonaccess.get_followers_count()"
#print e
time.sleep(60*16)
print "continuing..."
return followers_count
# The api variable is the way to access the api
def authorize():
# if more than 16 minutes have elapsed since last request, the requests can be reset
check_if_requests_can_be_reset()
# Increment number of requests made
global requests_since_last_sleep
requests_since_last_sleep += 1
# authorize
return Twython(apikeys.CONSUMER_KEY, apikeys.CONSUMER_SECRET, apikeys.ACCESS_TOKEN, apikeys.ACCESS_TOKEN_SECRET)
def follow_a_user(limit=3):
if get_followers_count() <= 1100:
follow.follow_a_user(authorize(), limit)
#global user_list
#print "user_list " + str(user_list)
#try:
# if user_list is None:
# user_list = follow.get_users(authorize())
# print "got user list"
#
# flag = False
# tries = 0
# while (not flag) and (tries < 3):
# time.sleep(30)
# next_user = user_list.next()
# print "going to follow " + next_user
# flag = follow.do_follow(authorize(), next_user)
# tries = tries + 1
#
# except Exception, e:
# print "Exception!"
# print e
# time.sleep(30)
#
def do_reply(tweet):
tweet = authorize().lookup_status(id=tweet['id'])[0]
reply = "@" + tweet["user"]["screen_name"] + " " + content.construct_tweet(random.choice(setup.callouts))
print reply
return send_rant(tweets = [reply], in_reply_to_status_id = tweet["id"])
# this method sends a tweet, by first checking with me
def send_tweet(tweet, in_reply_to_status_id=0):
# send tweet
if check_if_requests_are_maximum(14):
# if requests are maximum, then return directly
# this is to not build up a queue in massinvandring_streamer
return
# maybe send it in reply to another tweet
if in_reply_to_status_id == 0:
# standalone tweet
authorize().update_status(status=tweet)
else:
# tweet is a reply
# check original still exists
target = authorize().lookup_status(id=in_reply_to_status_id)[0]
target['id'] # don't do anything, just check it's there
time.sleep(3)
authorize().update_status(status=tweet, in_reply_to_status_id=in_reply_to_status_id)
print("sent tweet: " + tweet)
def post_content():
try:
string = content.timeline_content(authorize())
if string is not None:
print 'filler is ' + string
authorize().update_status(status=string)
except Exception, e:
print "Exception in twythonaccess.post_content()"
print e
time.sleep(60*16)
print "continuing..."
def seem_normal():
time.sleep((1+ random.random())* 30)
# check if https://twitter.com/AChristLife has a new
# tweet and RT it
post_content()
# check for new replies
# follow a user
time.sleep((1+ random.random())* 30)
follow_a_user(7)
def send_rant(tweets, in_reply_to_status_id=0):
print "in send_rant"
# send tweets with an interval of 30 secoonds
# if requests are above maximum minus number of tweets, then return directly
# either the entire rant will be sent, or no rant at all
if check_if_requests_are_maximum(6-len(tweets)):
# return false, so the caller will know that the rant wasn't sent this time
print "max requests"
return False
seem_normal()
# wait for 30-50 minutes to be more life-like
time.sleep(27 * 60 + (random.random()* 17 * 60))
#follow_a_user()
seem_normal()
time.sleep(3 * 60 + (random.random()* 3 * 60))
# check if we have enough followers to post this?
if setup.on_probation or (get_followers_count() < 501):
print "not enough followers or on probation"
time.sleep(60)
return False
time.sleep(60 + (random.random() *60))
last_status_id = in_reply_to_status_id
for tweet in tweets:
if last_status_id == 0:
# standalone tweet
authorize().update_status(status=tweet)
else:
# tweet is a reply
authorize().update_status(status=tweet, in_reply_to_status_id=last_status_id)
print("sent tweet: " + tweet)
# sleep for 30 seconds
time.sleep(20)
# get the status ad of the newly sent tweet
last_status_id = authorize().get_user_timeline(screen_name=apikeys.screen_name, count=1, trim_user=True, exclude_replies=False)[0]["id"]
# return true, since the rant was successfully sent
#time.sleep(60 * 60 * 4) # 4 hours
#reset_requests()
#content.timeline_content(authorize())
#time.sleep(60 * 60 * 4) # 4 hours
#reset_requests()
#content.timeline_content(authorize())
return True
# not sleeping by default
is_sleeping = False
# Store number of requests, so that they won't exceed the rate limit
requests_since_last_sleep = 0
# This method is called every time a request is to be made
# If the requests variable is over limit, the it returns true
# it also sets the bool is_sleeping
# finally, it schedules the bool to be set to false and the requests to be reset after 16 minutess
# if the requests variable isn't over limit, then do nothing
def check_if_requests_are_maximum(limit):
global requests_since_last_sleep
global is_sleeping
print("Requests since last sleep: " + str(requests_since_last_sleep))
if requests_since_last_sleep >= limit:
# set the is_sleeping to true
if not is_sleeping:
is_sleeping = True
# delay for 16 minutes
#Timer(16*60, reset_requests).start()
time.sleep((16 * 60) + (600 * random.random()))
reset_requests()
#return True
return False
def set_sleep(sleeping=True):
is_sleeping = sleeping
# if more than 16 minutes hace elapsed since the last request, reset the requests
def check_if_requests_can_be_reset():
global time_of_last_request, requests_since_last_sleep
# use utctime to not have to care about summer time
now_time = datetime.utcnow()
# compare the now_time and last time
# if more than 16 minutes have elapsed, reset the requests
if (now_time - time_of_last_request).total_seconds() > 16*60:
# reset requests
requests_since_last_sleep = 0
# update the last time
time_of_last_request = now_time
# this function resets the requests
def reset_requests():
requests_since_last_sleep = 0
is_sleeping = False