-
Notifications
You must be signed in to change notification settings - Fork 43
/
instafollow.py
131 lines (104 loc) · 5.13 KB
/
instafollow.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
import time
from random import randint
from datalayer import InstalikeDataLayer
class InstaFollow:
def __init__(self, operation, repository: InstalikeDataLayer, content_manager, configuration):
self.operation = operation
self.repository = repository
self.content_manager = content_manager
self.configuration = configuration
# configuration
self.max_follows_per_hour = self.configuration.instafollow_max_follows_per_hour
self.max_unfollows_per_hour = self.configuration.instafollow_max_unfollows_per_hour
self.unfollow_users = self.configuration.instafollow_unfollow_users # unfollow ppl?
self.unfollow_after_days = self.configuration.instafollow_unfollow_after_days # unfollow people after # days
# users
self.users = []
self.to_unfollow = []
# instance stats
self.follows = 0
self.unfollows = 0
self.failed_follows = 0
self.failed_unfollows = 0
self.hourly_follows = 0
self.hourly_unfollows = 0
# timing
self.next_follow_time = 0
self.next_unfollow_time = 0
self.follow_time_delta = 60 * 60 // (1 if self.max_follows_per_hour == 0 else self.max_follows_per_hour)
self.unfllow_time_delta = 60 * 60 // (1 if self.max_unfollows_per_hour == 0 else self.max_unfollows_per_hour)
self.next_unfollow_queue_update = 0 # when to update users that bot will unfollow
self.t0 = time.time()
self.t1 = 0
def follow(self):
if (time.time() < self.next_follow_time):
return
user = self.content_manager.get_next_user()
if (user != None):
response = self.operation.follow(user.id)
self.repository.persist_follow(user)
if (response.status_code != 200):
self.failed_follow()
else:
self.followed_successfully(user)
self.update_follow_timer()
def unfollow(self):
if not self.unfollow_users or (time.time() < self.next_unfollow_time):
return
if len(self.to_unfollow) == 0:
self.to_unfollow = self.repository.get_users_to_unfollow(self.configuration.instafollow_unfollow_after_days)
if len(self.to_unfollow) > 0:
user_id = self.to_unfollow.pop()
response = self.operation.unfollow(user_id)
self.repository.persist_unfollow(user_id, response.status_code)
if (response.status_code != 200):
self.failed_unfollow()
else:
self.unfollowed()
self.update_unfollow_timer()
def act(self):
self.follow()
self.unfollow()
def update_follow_timer(self):
self.next_follow_time = time.time() + randint(self.follow_time_delta - (self.follow_time_delta // 2),
self.follow_time_delta + (self.follow_time_delta // 2))
self.get_stats()
def update_unfollow_timer(self):
self.next_unfollow_time = time.time() + randint(self.unfllow_time_delta - (self.unfllow_time_delta // 2),
self.unfllow_time_delta + (self.unfllow_time_delta // 2))
self.get_stats()
def get_stats(self):
self.t1 = time.time()
per_hour_follows = ((self.follows + self.failed_follows) * 60 * 60) // (
1 if (self.t1 - self.t0) == 0 else self.t1 - self.t0)
per_hour_unfollows = ((self.unfollows + self.failed_unfollows) * 60 * 60) // (
1 if (self.t1 - self.t0) == 0 else self.t1 - self.t0)
self.log('#######################################')
self.log('---------------FOLLOWS-----------------')
self.log('total time: {0:.0f}s'.format(self.t1 - self.t0))
self.log('follows: {0}'.format(self.follows))
self.log('failed follows: {0}'.format(self.failed_follows))
if (self.unfollow_users):
self.log('unfollows: {0}'.format(self.unfollows))
self.log('failed unfollows: {0}'.format(self.failed_unfollows))
self.log('estimated follows per hour: {0:.0f}'.format(per_hour_follows))
if (self.unfollow_users):
self.log('estimated unfollows per hour: {0:.0f}'.format(per_hour_unfollows))
self.log('next follow in: {0:.0f}s'.format(self.next_follow_time - time.time()))
if (self.unfollow_users):
self.log('next unfollow in: {0:.0f}s'.format(
0 if self.next_unfollow_time == 0 else self.next_unfollow_time - time.time()))
self.log('users to follow: {0}'.format(self.content_manager.get_user_count()))
if (self.unfollow_users):
self.log('users to unfollow: {0}'.format(len(self.to_unfollow)))
def log(self, text):
print(text)
def failed_unfollow(self):
self.failed_unfollows += 1
def unfollowed(self):
self.unfollows += 1
def failed_follow(self):
self.failed_follows += 1
def followed_successfully(self, user):
self.follows += 1
self.hourly_follows += 1