-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
231 lines (195 loc) · 9.95 KB
/
bot.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
from twitter import Twitter, OAuth, TwitterHTTPError
import os
import sys
import time
import random
import datetime
class Bot:
def __init__(self):
self.connection = 0
self.name = "Iamnotabot4";
self.logFile = open("log.txt",'a')
self.logFile.write("\n###########################################################\n")
self.logFile.write("## "+self.getDateTime()+"New Sesion has been created ##\n")
self.logFile.write("###########################################################\n")
random.seed()
def connect(self):
try:
self.connection = Twitter(auth=OAuth("PURGED CREDENTIALS WILL NOT WORK NOW."))
self.logFile.write(self.getDateTime() + "\tConncetion succeeded")
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
def delay (self):
min_time = 5
max_time = 20
toWait = random.randint(min_time, max_time)
if toWait > 0:
time.sleep(toWait)
def getDateTime(self):
return str(datetime.datetime.now())
def tweet(self,text):
try:
self.connection.statuses.update(status=text)
self.logFile.write('\n'+self.getDateTime()+"\tTweet has been send with text: " + text)
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
def getFollowers(self, userId = ""):
try:
if userId == "":
userId = self.name
followers_status = self.connection.followers.ids(screen_name = userId)
followers = set(followers_status["ids"])
string = ""
for follower in followers:
string+=str(follower)
string+=","
string = string[:-1]
lookedUp = self.connection.users.lookup(user_id=string)
with open("followersLookedUp.txt", "w") as out_file:
for follower in lookedUp:
out_file.write("%s\n" % (follower["screen_name"]))
self.logFile.write('\n'+self.getDateTime()+"\tFollowers table has been filled in 'followersLookedUp.txt'")
with open("followers.txt", "w") as out_file:
for follower in followers:
out_file.write("%s\n" % (follower))
self.logFile.write('\n'+self.getDateTime()+"\tFollowers table by ID has been filled in 'followers.txt'")
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
def getFollowed(self, userId = ""):
if userId == "":
userId = self.name
followers_status = self.connection.friends.ids(screen_name = userId)
followers = set(followers_status["ids"])
string = ""
for follower in followers:
string+=str(follower)
string+=","
string = string[:-1]
lookedUp = self.connection.users.lookup(user_id=string)
with open("followedLookedUp.txt", "w") as out_file:
for follower in lookedUp:
#print follower
out_file.write("%s\n" % (follower["screen_name"]))
self.logFile.write('\n'+self.getDateTime()+"\tFollowed table has been filled in 'followedLookedUp.txt'")
with open("followed.txt", "w") as out_file:
for follower in followers:
out_file.write("%s\n" % (follower))
self.logFile.write('\n'+self.getDateTime()+"\tFollowed table by ID has been filled in 'followed.txt'")
def getTweetsByPhrase(self,phrase,count,result_type):
self.logFile.write('\n'+self.getDateTime()+"\tSearching "+str(count)+' '+result_type+" tweets by phrase - "+phrase+ "...")
result = self.connection.search.tweets(q=phrase, result_type=result_type, count=count)
with open("foundTweets.txt","a") as out_file:
out_file.write("----- Phrase: %s -----\n" % (phrase))
for i in range(0,count):
try:
author = result["statuses"][i]["user"]["screen_name"]
text = result["statuses"][i]["text"]
tweet = "Autor: "+author+"\tTekst: "+text.encode('ascii','ignore')
out_file.write("%s\n" % tweet)
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
out_file.write("----------------------------------------------\n")
self.logFile.write('\n'+self.getDateTime()+"\t'foundTweets.txt' has been updated")
return result
# DO NOT OVERUSE IT. IT IS WORKING
# DO NOT PUT HIGH VALUE IN COUNT
# followed.txt and followers.txt Must be valid and up to date
#
# Function will autofollow first (count) number of people that follows you but you do not follow yet
def returnFollowed (self):
followedList = []
with open("followed.txt", "r") as in_file:
for line in in_file:
followedList.append(int(line))
return set(followedList)
def returnFollowers (self):
followedList = []
with open("followers.txt", "r") as in_file:
for line in in_file:
followedList.append(int(line))
return set(followedList)
def autoRefollow (self, count = 1):
self.logFile.write('\n'+self.getDateTime()+"\tRefollow "+str(count)+" users that follow you")
diff = self.returnFollowers() - self.returnFollowed()
diff = list(diff)[:count]
for user in diff:
try:
self.connection.friendships.create(user_id=user, follow=False)
self.delay()
self.logFile.write('\n'+self.getDateTime()+"\tUser with ID: "+str(user)+" is now followed")
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
return
# SAME AS autoRefollow fnc, do not overuse
def autoFollowFollowersOfUser (self, user, count = 2):
self.logFile.write('\n'+self.getDateTime()+"\t'Follow "+str(count)+ " random users that follow "+user)
followedList = self.returnFollowed()
followersOfUser = set(self.connection.followers.ids(screen_name=user)["ids"][:count])
followersOfUser = followersOfUser - followedList
for user_id in followersOfUser:
try:
self.delay()
self.connection.friendships.create(user_id=user_id, follow=False)
self.logFile.write('\n'+self.getDateTime()+"\tUser with ID: "+str(user_id)+" is now followed")
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
return
def unfollowUsersNonFollowU(self,count = 2):
self.logFile.write('\n'+self.getDateTime()+"\tUnfollow "+str(count)+" users that not followed u back")
followedUsers = set(self.connection.friends.ids(screen_name = "")["ids"][:count])
followersList = self.returnFollowers()
followedUsers = followedUsers-followersList
for user_id in followedUsers:
try:
self.delay()
self.connection.friendships.destroy(user_id = user_id)
self.logFile.write('\n'+self.getDateTime()+"\tUser with ID: "+str(user_id)+" is no more followed")
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
return
def getTrendsByLocation(self,LocationId):
self.logFile.write('\n'+self.getDateTime()+"\tSearching trends by location ID - "+str(LocationId)+ "...")
results = self.connection.trends.place(_id = LocationId)
location = results[0]["locations"][0]["name"]
with open("trends.txt", "a") as out_file:
out_file.write("%s Location: %s\n" % (str(datetime.datetime.now()),location))
for trend in results[0]["trends"]:
print " - %s" % trend["name"].encode('ascii','ignore')
out_file.write("%s\t" % trend["name"].encode('ascii','ignore'))
self.logFile.write('\n'+self.getDateTime()+"\t'trends.txt' has been updated")
def retweetByPhrase(self, phrase, count = 2, result_type="recent"):
self.logFile.write('\n'+self.getDateTime()+"\tRetweet "+str(count)+' '+result_type+" tweets by phrase - "+phrase+" ...")
result = self.getTweetsByPhrase(phrase, count, result_type)
for tweet in result["statuses"]:
try:
self.delay()
self.connection.statuses.retweet(id=tweet["id"])
self.logFile.write('\n'+self.getDateTime()+"\tTweet with text: '"+tweet["text"]+"' has been retweeted")
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
# def MostFollowFromFollowed(self,count = 2):
# AllFollowedUsers = self.returnFollowed()
# for followedUser in AllFollowedUsers:
# print followedUser
# print "-----"
# followedOfUser = set(self.connection.friends.ids(user_id = followedUser)["ids"][:count])
# for user in followedOfUser:
# print "count:"
# self.delay();
# print self.connection.users.lookup(user_id=followedOfUser)
def retweetMostPopular (self, toFind = 10):
self.logFile.write('\n'+self.getDateTime()+"\t'Searching "+str(toFind)+" most recent tweets on wall ...")
tweetsOnTimeline = self.connection.statuses.home_timeline(count = toFind)
maxRetweets = 0
for i in range(0,toFind):
if maxRetweets < tweetsOnTimeline[i]["retweet_count"]:
tweet = tweetsOnTimeline[i]
maxRetweets = tweetsOnTimeline[i]["retweet_count"]
self.logFile.write('\n'+self.getDateTime()+"\tMost retweetet has "+str(maxRetweets["retweet_count"]))
try:
self.delay()
self.connection.statuses.retweet(id=tweet["id"])
self.logFile.write('\n'+self.getDateTime()+"\tThs tweet has been rewteeted")
except Exception,e:
self.logFile.write('\n'+self.getDateTime()+'\t'+str(e))
return