-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollower_tw_slow.py
216 lines (179 loc) · 8.52 KB
/
follower_tw_slow.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
# Tweetbot follower
# by Tomislav Rozman
# Usage:
# 1. Put several keywords in Tweets.xlsx, worksheet Keywords, B Column
# 2. Run the program
# 3. This followerbot will search for twitter accounts which twit about <keywords> and
# it will follow them in slow pace, 288 per day
from openpyxl import load_workbook
from openpyxl.styles.styleable import NumberFormatDescriptor
from colorama import init, deinit # color text
from colorama import Fore, Back, Style
import tweepy
import datetime
import time
import traceback
#read twitter keys from the config file (tweerconfig.txt)
print("Reading config file with twitter keys...")
def get_pair(line): #transform string line to 2 vars: key & value
key, sep, value = line.strip().partition("=")
return key, value
with open("tweetconfig.txt") as config_file:
config_keys = dict(get_pair(line) for line in config_file) #read line from the file, convert it to key,value and insert it into dict
print("Done.")
# Your app's API/consumer key and secret can be found under the Consumer Keys
# section of the Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
consumer_key = str(config_keys["consumer_key"])
consumer_secret = str(config_keys["consumer_secret"])
# Your account's (the app owner's account's) access token and secret for your
# app can be found under the Authentication Tokens section of the
# Keys and Tokens tab of your app, under the
# Twitter Developer Portal Projects & Apps page at
# https://developer.twitter.com/en/portal/projects-and-apps
access_token = str(config_keys["access_token"])
access_token_secret = str(config_keys["access_token_secret"])
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
print("Authenticating to twitter again...")
# Authenticate Twitter
try:
TweetAPI = tweepy.API(auth,wait_on_rate_limit=False)
# If the authentication was successful, this should print the
# screen name / username of the account
print("Twitter username:"+TweetAPI.verify_credentials().screen_name)
print("Auth. successfull!")
except:
print("Auth. was not successfull!")
exit()
#end try auth
init() #init colorama for coloured text
XLSfilename="Tweets.xlsx"
Worksheetname="Keywords"
print("Opening "+XLSfilename+" and getting keywords...")
wb = load_workbook(filename = XLSfilename)
ws = wb[Worksheetname] #worksheet name
keywords=[]
row=tuple
#helper vars and counters
no_of_new_follows=0
no_of_new_per_kw=0
no_of_fails=0
no_of_fails_per_kw=0
keywords=ws.cell(2,2).value.split(",") #take from 'Used keywords' cell
print("Keywords from excel:",str(keywords))
print("Search tweets with keyword ")
#search for tweets
#for keyword in keywords:
# print(Fore.BLUE+"*** Keyword: "+keyword)
# for tweet in TweetAPI.search_tweets(q=keyword, lang="en", count=10):
# print(Fore.YELLOW+tweet.user.name+"(@"+tweet.user.screen_name+")"+":"+Fore.WHITE+tweet.text+Fore.CYAN+" at "+str(tweet.created_at))
# print("------------------------------")
print("Search tweet accounts with keyword ")
for keyword in keywords:
print(Fore.BLUE+"*** Keyword: "+keyword)
for tweets in TweetAPI.search_tweets(q=keyword, count=100):
print(Fore.BLUE+"*** Keyword: "+keyword)
print(Fore.WHITE+"Tweet text:"+tweets.text)
#print("Tweet author:"+str(tweets.user))
tweet_user=tweets.user
print("Author screen name:"+tweet_user.screen_name)
print(Fore.WHITE+"Trying to follow @"+tweet_user.screen_name+" ("+tweet_user.name+")")
print("At: "+str(datetime.datetime.now()))
for attempt in range(3): #retry if hit limit
try:
friendship=TweetAPI.get_friendship(source_screen_name="BICERO_Ltd",target_screen_name=tweet_user.screen_name)
except Exception as e:
print("Error getting friends, now waiting fot 16 mins. Attempt "+str(attempt))
print(e)
#exit()
time.sleep(16*60) #wait for 15 mins to overcome the rate limit
else:
print("Attempt to get tweet author was successful, continuing after 6 secs...")
time.sleep(6)
break
else:
#we failed all attempts to save, exiting
print("All attempts failed, exiting...")
exit()
#for - retry
if friendship[0].following == False:
print("BICERO_Ltd"+" is not following "+tweet_user.screen_name)
for follow_attempt in range(3):#retry following 3 times, wait after each retry
try:
print("User posted so many tweets:"+str(tweet_user.statuses_count))
if tweet_user.statuses_count<100:
print("Too little tweets, skipping...")
break
print("User has so many followers:"+str(tweet_user.followers_count))
if tweet_user.followers_count<50:
print("Too little followers, skipping...")
break
#else:
TweetAPI.create_friendship(user_id=tweet_user.id)
print(Fore.GREEN+"Created a new follow, waiting for 5 mins ...")
time.sleep(5*60) #avoid block: wait for 5 mins = 12 follows per hour = 288 per day
except tweepy.HTTPException as e:
print("Error code:"+str(e.api_codes))
print(e)
if e.api_codes==[160]: #follow request already sent
print("Follow request already sent, skipping! ")
print(e)
no_of_fails+=1
time.sleep(6)
break
if e.api_codes==[161]: #unable to follow more people
print(Fore.RED+"Follow NOT successfull, waiting for 24 hrs from "+str(datetime.datetime.now()))
print(e)
no_of_fails+=1
time.sleep(24*60*60) #wait for 24 hrs
if e.api_codes==[158]: #can't follow myself
print(Fore.RED+"Can't follow myself :) ")
print(e)
no_of_fails+=1
time.sleep(6)
break
if e.api_codes==[162]: #user does not want you to follow it
print(Fore.RED+"Blocked from following that user, nasty!")
print(e)
no_of_fails+=1
time.sleep(6)
break
if e.api_codes==[50]: #user does not want you to follow it
print(Fore.RED+"User not found, she dissapeared!")
print(e)
no_of_fails+=1
time.sleep(6)
break
except Exception as e1:
print("Unknow exception, waiting for 1 min! "+str(datetime.datetime.now()))
print(e1)
time.sleep(60)
break
else: #try except else
print(Fore.GREEN+"Follow successful, continuing..."+str(datetime.datetime.now()))
no_of_new_follows+=1
break
else: #for
#we failed all attempts to follow, exiting
print("All attempts to follow failed, exiting..."+str(datetime.datetime.now()))
exit()
#for retry
else: #if already followng
print(Fore.LIGHTRED_EX +"BICERO_Ltd"+" is ALREADY following "+tweet_user.screen_name)
no_of_fails+=1
print("------------------------------")
print(Fore.WHITE+keyword+" keyword done at "+ str(datetime.datetime.now()))
print(Fore.RED+"Fails per keyword:"+str(no_of_fails_per_kw))
print(Fore.GREEN+"New follows per keyword:"+str(no_of_new_per_kw))
#reset follows and fails per kw
no_of_fails_per_kw=0
no_of_new_per_kw=0
# for keyword loop end
#the main loop: repeat until keywords
print("")
print(Fore.WHITE+"All done at "+ str(datetime.datetime.now()))
print(Fore.RED+"Total Fails:"+str(no_of_fails))
print(Fore.GREEN+"Total New follows:"+str(no_of_new_follows))
deinit() #stop coloured output