-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitbot.py
458 lines (355 loc) · 15.7 KB
/
twitbot.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#coding: utf-8
from twython import Twython, TwythonError
from random import choice, randint, shuffle
from datetime import datetime
from threading import Thread
from time import time, sleep
from text import replays, night, afternoon, morning
from decorators import wrapper
CONSUMER_KEY = "Dc8GNbgcOklJie3TV2V0A"
CONSUMER_SECRET = "QducleApTwunlqZmNM0AdhwlhWpoDJ44agk6UnPs"
OAUTH_TOKEN = "1964645443-mmEaq9TWNGoXoZ9glFrE96Yx9ktHHkCRUxCFfms"
OAUTH_TOKEN_SECRET = "2PbSnYpJdGJdv1TF1F3eCF5SRdigCcwVYRpvsrfELta0t"
QUERYS = [u"хахол", u"хохол"]
TEXT = [morning, afternoon, night]
## map(shuffle,TEXT)
## for c in range(3):
## shuffle(replays)
class TwitBot(object):
def __init__(self,c_key, c_secret,
o_token, o_token_secret):
self.twitter = Twython(c_key, c_secret, o_token, o_token_secret)
#last tweet id for query "xохол"
self.id = [None]
#last tweet id for query "xахол"
self.jd = [None]
#last mentions id
self.m_id = 0
#last timelines tweet id
self.t_id = 0
#list of time_of_days, when bot should update status
self.time = [(4, 55), (17, 55), (19, 55)]
#count of replies of day
self.replies_limit = 0
self.replies_count = 35
self.flag = True
#users victims
self.users = (u"feeling_so_real", u"captein_treniki",
u"fe_city_boy", u"drunktwi",
u"Doppler_Effectt", u"koffboy")
def get_users(self, result, debug = 0):
#users = [(tweet id, user screen_name)]
users = [(int(c["id"]), c["user"]["screen_name"])
for c in result["statuses"] if
(c["user"]["screen_name"] != u"ghohol" and
u"ghohol" not in c["text"])
]
retweets = self.my_reetwets()
users = [x for x in users if x[0] not in retweets]
#sorted tweets from older to younger
users = sorted(users)
if not debug:
print "last user", users[-1]
print "#" * 40,'\n'
return users
################################## RUN SEARCH ############################################
def run_search(self, query, text):
self.query = query
result = self.twitter.search(q=query)
if result:
users = self.get_users(result)
for user in users:
if self.replies_count < self.replies_limit:
self.update_status(text,user)
else:
break
sleep(randint(180,210))
else:
sleep(randint(240, 480))
def update_status(self, text, user):
q_id = self.id if self.query == u"хохол" else self.jd
#replayed tweet or not
if user[0] > q_id[0]:
self.twitter.update_status(status= u"@{0} {1}".format(
user[1], choice(text)),
in_reply_to_status_id=user[0])
self.replies_count +=1
#save last tweet id
q_id[0] = user[0]
sleep(randint(400,560))
@wrapper(n=40)
def big_search(self):
for query in QUERYS:
self.run_search(query, replays)
sleep(70)
sleep(randint(60,120))
################################################################################
################################ HOME TIMELINE ######################################
@wrapper()
def home_timeline(self):
""" update count of replies
update timeline(random rt or fav in homeline)
"""
hour = self.data.hour
#debug
print self.replies_count, "count"
print self.replies_limit, "limit"
#end debug
def update_replies_count():
"""update flags and replies limit"""
if self.flag:
if hour in (10,17):
self.replies_limit = randint(3,12)
self.flag = False
self.replies_count = 0
def timeline_updates():
"""check time and run if time rt and fav in homeline"""
if (hour in range(0, 3) or hour in range(9, 24)):
print "CHECK TIMELINE UPDATES"
result = self.get_home_timeline(count=30,exclude_replies=1,
since_id=self.t_id)
if result:
tw = [tweet["id"] for tweet in result if (tweet['user']['screen_name'] != u"ghohol" and
not tweet['entities']['user_mentions'])]
if tw:
self.t_id = tw[0]
rt_or_fav = lambda func : map(func, filter(lambda x:x % randint(21,27) == 0, tw))
if (randint(0,7) == randint(0,7)):
map(rt_or_fav,(self.retweet, self.create_favorite))
else:
print "NO TIMELINE UPDATES"
update_replies_count()
timeline_updates()
################################### Tools #############################################
for name in ("retweet", "create_favorite", "destroy_status",
"destroy_friendship"):
exec("""def {0}(self, id):
self.twitter.{0}(id=id)""".format(name))
for name in ("get_home_timeline", "get_direct_messages", "get_friends_ids",
"get_followers_ids", "get_user_timeline", "show_status"):
exec("""def {0}(self, *args, **kwargs):
return self.twitter.{0}(*args, **kwargs)""".format(name))
@property
def data(self):
return datetime.now()
def my_reetwets(self):
"""return id of my retweets"""
retweets = self.twitter.retweeted_of_me()
retweets_id = [c["id"] for c in retweets]
return retweets_id
@wrapper(n=30)
def tw_reader(self, tw):
for c in tw:
print "Tweet from @{0} ID: {1}".format(c[1].encode('utf-8'), c[0])
print c[2].encode('utf-8'), '\n'
############################### GET REPLAYS #############################################
@wrapper(n=0)
def get_replays(self):
dirty_list = (u"хуй", u"пидор",u"пидар", u"пидр",
u"бля", u"блядь", u"сука",u"ебень",
u"гондон", u"гандон", u"шлюха",
u"чмо", u"залупа", u"eблан",
u"козел", u"казел", u"козлина",
u"тварь", u"долбоеб", u"бандеровец",
u"блять", u"уеби", u"бендеровец",
u"дебил", u"дибил", u"петух",
u"питух", u"петушок", u"ебали",
u"хуя"
)
def is_shit(tweet):
t = tweet[2].lower()
for word in dirty_list:
if word in t:
return word
return False
repl = self.twitter.get_mentions_timeline(include_rts = 0,
since_id = self.m_id)
print repl, "repl"
if repl:
print "IF REPL"
repl =[(c["id"],c["user"]["screen_name"],c["text"]) for c
in repl if u"RT" not in c["text"]]
self.m_id = repl[0][0]
repl = filter(is_shit, repl)
if repl:
print "IF REPL INNER"
print "Try send mention for dirty reply"
for tw in repl:
(self.retweet(tw[0]) if randint(0,1)
else self.update_dirty_status(tw[1],tw[0]))
#return repl
print repl, "repl"
return repl
def update_dirty_status(self, name, id):
answers = [ u"Ты еще и ругаешься",
u"Попросил бы без оскорблений",
u"Очень грубо",
u"А можно без мата?",
u"Кто так обзывается, сам так называется",
u"Сколько мата тьфу ты, бля"
]
self.update_status(status= u"@{0} {1}".format(
name, choice(answers)),
in_reply_to_status_id=id)
sleep(randint(140,280))
######################################## STEAL TWEET ################################
@wrapper(n=0)
def steal_tweets(self, d):
hour = d.hour
print hour, "FGGGGGGGGGGGGGGGGJJGJGJGJGJJJJJJJJJJJJJJJJJ"
if hour in (5, 16):
print "TIME TO STEAL TWEETS"
#get my tweets
my_tweets = self.twitter.get_user_timeline(count=200, exclude_replies=1)
my_tweets = [c["text"] for c in my_tweets]
#get victim's tweets
get_victims_timeline = self.get_victims_timeline(my_tweets)
victims_tweets = map(get_victims_timeline, self.users)
for tweet in victims_tweets:
print tweet.encode('utf-8')
if tweet is not None:
self.twitter.update_status(status=tweet)
sleep(3600)
print "STEALING TWEETS IS OVER"
else:
sleep(3600)
def get_victims_timeline(self, text):
def victims_tweets(name):
raw_tweets = self.twitter.get_user_timeline(screen_name=name, exclude_replies=1, count=60)
print "dddd"
print "raw_tweets", len(raw_tweets)
tweets = raw_tweets[-1:-len(raw_tweets):-1]
#print tweets
tweets = [c for c in tweets if (not c["entities"]['urls'] and
len(c['entities'])==4 and
not c['entities']['user_mentions']
)]
sample_tweet = [c['text'] for c in tweets if c['text'] not in text]
if sample_tweet:
sample_tweet = sample_tweet[-1]
print sample_tweet.encode('utf-8'), "sample_tweet"
return sample_tweet
else:
return None
return victims_tweets
######################################## START ########################################
@wrapper(n=0)
def start(self):
starters = ("jd", "id", "t_id", "m_id")
d = dict(zip(QUERYS,(self.jd, self.id)))
def complete():
while True:
s = yield
if s:
print "##### COMPLETE #####\n"
print "{0} = {1} {2} complete\n".format(s, getattr(self, s) ,'.' * 25)
gen = complete()
gen.next()
while not all([eval("self." + c) for c in starters]):
for query in QUERYS:
result = self.twitter.search(q=query)
d[query][0] = self.get_users(result,debug=1)[-1][0]
self.t_id = self.twitter.get_home_timeline(exclude_replies = 1)[0]["id"]
self.m_id = self.twitter.get_mentions_timeline(count=30, include_rts = 0)[0]["id"]
[gen.send(c) for c in starters]
###################################### DELETE REPLIES ##############################################
@wrapper()
def delete_replies(self):
if self.data.hour == 23:
print "START CLEAR TWEETS"
tweets = self.get_user_timeline(count=100)
tweets = (c["id"] for c in tweets if c["in_reply_to_user_id"] )
sleep(15)
map(self.destroy_status, tweets)
else:
print "NO CLEAN"
################################## UNFOLLOW ######################################
@wrapper()
def unfollow_who_not_follow_back(self):
data = self.data
#clean followers every odd day
if data.day % 2 == 0 and data.hour == 1:
# "stars" id's
stars_id = (462792965, 14774424, 344512640,
265008715, 412493190, 254655960,
179484444, 90647948, 408437933,
98422492, 81297044, 250801581)
print "UNFOLLOWING START"
#get friends id's
friends_ids = self.get_friends_ids()[u"ids"]
friends_ids = (id for id in friends_ids if id not in stars_id)
#get followers ids
followers_ids = self.twitter.get_followers_ids()[u"ids"]
#unfollowing list
destroy_list = (user_id for user_id in friends_ids
if user_id not in followers_ids)
map(self.destroy_friendship, destroy_list)
print "UNFOLLOWING END"
############################### DATE STATUS ############################################
def date_status(self, text):
current_time = self.data
print "current time = {0}".format(str(current_time))
#this part for count_replies
if current_time.hour in (9, 16):
self.flag = True
sleep(randint(1600, 2400))
## def part_of_day(time_of_day):
## """return True if current time equal time_of_day"""
## return (current_time.hour == time_of_day[0] and
## current_time.minute in range(time_of_day[1]))
## check_time = filter(part_of_day, self.time)
## if check_time:
## print "check_time"
## message = choice(text[self.time.index(check_time[0])])
## try:
## self.twitter.update_status(status= u"{0}".format(message))
## sleep(randint(1600, 2400))
## except TwythonError as err:
## print err
## sleep(480)
else:
sleep(randint (1200, 1800))
################################################################################
class T_date(Thread):
"""Class for update status in certain time"""
def __init__(self, twitter):
Thread.__init__(self)
self.twitter= twitter
def run(self):
while True:
self.twitter.date_status(TEXT)
################################################################################
class D(Thread):
def __init__(self, twitter):
Thread.__init__(self)
self.twitter= twitter
def p(self):
a = datetime.now()
print "CURRENT TIME ==> {0}".format(a)
self.twitter.steal_tweets(a)
def run(self):
while True:
self.p()
def main():
#connection and login
twitter = TwitBot(CONSUMER_KEY,CONSUMER_SECRET,
OAUTH_TOKEN,OAUTH_TOKEN_SECRET)
## #start twiter
twitter.start()
#init thread
d = D(twitter)
d.daemon = True
d.start()
#init thread
t = T_date(twitter)
t.daemon = True
t.start()
while True:
twitter.unfollow_who_not_follow_back()
twitter.delete_replies()
twitter.home_timeline()
twitter.big_search()
repls = twitter.get_replays()
twitter.tw_reader(repls)
if __name__ == "__main__":
main()