forked from j4rj4r/BotTwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retweet_giveaway.py
273 lines (232 loc) · 12.3 KB
/
retweet_giveaway.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
# Standard libraries
import logging
import random
import re
import time
# third party libraries
import tweepy
class RetweetGiveaway:
def __init__(self, api, user):
"""
RetweetGiveaway class constructor, requires api object and user object
:param api tweepy.API: api object from tweepy library
:param user tweepy.API.me() : User object for current bot
"""
self.user = user
self.api = api
self.bot_action = []
def check_retweet(self, words_to_search, accounts_to_blacklist, hashtag_to_blacklist, giveaway_to_blacklist,
comment_with_hashtag, max_giveaway):
"""
Check for useful tweets by filtering out blacklisted
:param words_to_search list: List of Keywords to Search tweet for
:param accounts_to_blacklist list: List of Blacklisted Accounts to Ignore
:param hashtag_to_blacklist list: List of Blacklisted Hashtags in tweets to ignore
:param giveaway_to_blacklist list: List of Blacklisted Giveaways to Ignore
:param comment_with_hashtag boolean: If we comment with hashtag
:param max_giveaway integer: Maximum number of giveaway retrieve for each word
"""
action = []
regex_detect_tag = [r"\b(\w*INVIT(E|É)\w*)\b",
r"\b(\w*IDENTIFI(E|É)\w*)\b",
r"\b(\w*TAG\w*)\b",
r"\b(\w*MENTIONN(E|É)\w*)\b"]
regex_detect_tag = re.compile('|'.join(regex_detect_tag), re.IGNORECASE)
for word in words_to_search:
logging.info("Searching giveaway with the word : %s", word)
for tweet in tweepy.Cursor(self.api.search,
q=word, since=time.strftime('%Y-%m-%d', time.localtime()),
lang="fr", tweet_mode="extended").items(max_giveaway):
if tweet.retweet_count > 5:
is_in_blacklist = [ele for ele in giveaway_to_blacklist if (ele in tweet.full_text)]
if is_in_blacklist:
pass
else:
# Check if it's a retweet
if hasattr(tweet, 'retweeted_status'):
screen_name = tweet.retweeted_status.author.screen_name
entities = tweet.retweeted_status.entities
full_text = tweet.retweeted_status.full_text
extra = 0
else:
screen_name = tweet.user.screen_name
entities = tweet.entities
full_text = tweet.full_text
extra = 3
# Check if Tweet Author is blacklisted or not
if screen_name not in accounts_to_blacklist:
# Check for INVITE/TAG/MENTIONNE in retweet text
if re.search(regex_detect_tag, full_text):
# Check if tweet has Hashtags
if len(entities['hashtags']) > 0:
# if comment with hashtag is enabled
if comment_with_hashtag:
# Clean Hastags
h_list = self.manage_hashtag(entities['hashtags'],
hashtag_to_blacklist)
# If we find Hashtags -> Record the tweet
if h_list:
action.append(tweet)
action.append(1 + extra)
self.bot_action.append(action)
else:
action.append(tweet)
action.append(2 + extra)
self.bot_action.append(action)
else:
action.append(tweet)
action.append(2 + extra)
self.bot_action.append(action)
# Else Select Action 2
else:
action.append(tweet)
action.append(2 + extra)
self.bot_action.append(action)
# If regex-tags not found, record the tweet without action number
else:
action.append(tweet)
self.bot_action.append(action)
action = []
return self.bot_action
def manage_giveaway(self, list_giveaway, sentence_for_tag, list_name, hashtag_to_blacklist, managefollow,
like_giveaway):
"""
Handle Give away tweets by following/commenting/tagging depending on the giveaway levels
:param list_giveaway list: List of Giveaways tweets and (optional) Giveaway levels
:param sentence_for_tag list: List of Random Sentences to use for commenting
:param list_name list: List of Names to Randomly Tag on giveaways
:param hashtag_to_blacklist list: List of hastags to blacklist
:param managefollow managefollow: Database management object from ManageFollow
:param like_giveaway boolean: If we like giveaway
"""
for giveaway in list_giveaway:
tweet = giveaway[0]
try:
if hasattr(tweet, 'retweeted_status'):
retweeted = tweet.retweeted_status.retweeted
id_ = tweet.retweeted_status.id
author_id = tweet.retweeted_status.author.id
entities = tweet.retweeted_status.entities
screen_name = tweet.retweeted_status.user.screen_name
else:
retweeted = tweet.retweeted
id_ = tweet.id
author_id = tweet.user.id
entities = tweet.entities
screen_name = tweet.user.screen_name
if not retweeted:
self.api.retweet(id_)
if like_giveaway:
self.api.create_favorite(id_)
self.api.create_friendship(author_id)
if len(giveaway) == 2:
comment_level = giveaway[1]
self.comment(tweet, sentence_for_tag, comment_level, list_name, hashtag_to_blacklist)
managefollow.update_table(author_id)
if len(entities['user_mentions']) > 0:
for mention in entities['user_mentions']:
self.api.create_friendship(mention['id'])
managefollow.update_table(mention['id'])
random_sleep_time = random.randrange(10, 20)
logging.info("You participated in the giveaway of : @%s. Sleeping for %ss...",
screen_name,
str(random_sleep_time))
time.sleep(random_sleep_time)
except tweepy.TweepError as e:
if e.api_code == 327:
pass
elif e.api_code == 161:
logging.warning("The account can no longer follow. We go to the next step.")
break
elif e.api_code == 136:
logging.info("You have been blocked by: ", screen_name)
break
elif e.api_code == 326:
logging.warning("You have to do a captcha on the account: ", screen_name)
break
else:
logging.error(e)
def comment(self, tweet, sentence_for_tag, hashtag, list_name, hashtag_to_blacklist):
"""
Add Comment to a given tweet using some rules.
:param tweet tweepy.tweet: Tweet object from tweepy library
:param sentence_for_tag list: List of random sentences
:param hashtag list: List of Hashtags
:param list_name list: List of user names
:param hashtag_to_blacklist list: List of Blacklisted Hastags to avoid
"""
random.shuffle(list_name)
nbrandom = random.randrange(0, len(sentence_for_tag))
randomsentence = sentence_for_tag[nbrandom]
# Random Sentence + Tag Comment + Hashtag Comment + Update Status
if hashtag == 1:
comment = "@" + tweet.retweeted_status.author.screen_name + " " + randomsentence + " "
comment = self.add_tag_comment(list_name, comment)
comment = self.add_hashtag_comment(comment, tweet.retweeted_status.entities['hashtags'],
hashtag_to_blacklist)
self.api.update_status(comment, tweet.retweeted_status.id)
# Random Sentence + Tag Comment + Update Status
elif hashtag == 2:
comment = "@" + tweet.retweeted_status.author.screen_name + " " + randomsentence + " "
comment = self.add_tag_comment(list_name, comment)
self.api.update_status(comment, tweet.retweeted_status.id)
# Hashtag Comment + Update Status
elif hashtag == 3:
comment = "@" + tweet.retweeted_status.author.screen_name + " "
comment = self.add_hashtag_comment(comment, tweet.retweeted_status.entities['hashtags'],
hashtag_to_blacklist)
self.api.update_status(comment, tweet.retweeted_status.id)
# User - Random Sentence + Tag Comment + Hashtag Comment + Update Status
elif hashtag == 4:
comment = "@" + tweet.user.screen_name + " " + randomsentence + " "
comment = self.add_tag_comment(list_name, comment)
comment = self.add_hashtag_comment(comment, tweet.entities['hashtags'],
hashtag_to_blacklist)
self.api.update_status(comment, tweet.id)
# User - Random Sentence + Tag Comment + Update Status
elif hashtag == 5:
comment = "@" + tweet.user.screen_name + " " + randomsentence + " "
comment = self.add_tag_comment(list_name, comment)
self.api.update_status(comment, tweet.id)
# User - Hashtag Comment + Update Status
elif hashtag == 6:
comment = "@" + tweet.user.screen_name + " "
comment = self.add_hashtag_comment(comment, tweet.entities['hashtags'],
hashtag_to_blacklist)
self.api.update_status(comment, tweet.id)
def manage_hashtag(self, hashtag_list, hashtag_to_blacklist):
"""
Filter Blacklisted Hastags
:param hashtag_list list: List of Hashtags from Tweet
:param hashtag_to_blacklist list: List of BlackListed Hashtags
"""
h_list = []
for h in hashtag_list:
h_list.append(h['text'].upper())
return list(set(h_list) - set(hashtag_to_blacklist))
def add_tag_comment(self, list_name, comment):
"""
Tag other users in comment.
:param list_name list: List of user names to add to comment
:param comment string: Tweet/text/Comment
"""
nbusernotif = 0
for username in list_name:
if nbusernotif < 2:
if username == "@" + self.user.screen_name:
pass
else:
comment = comment + username + " "
nbusernotif += 1
return comment
def add_hashtag_comment(self, comment, hashtag_list, hashtag_to_blacklist):
"""
Add hashtag in Comments
:param comment string: Comment to which to add hashtags
:param hashtag_list list: List of Hashtags
:param hashtag_to_blacklist list: List of Blacklisted Hashtags to avoid
"""
h_list = self.manage_hashtag(hashtag_list, hashtag_to_blacklist)
for hashtag in h_list:
comment = comment + "#" + hashtag + " "
return comment