-
Notifications
You must be signed in to change notification settings - Fork 43
/
instalike.py
102 lines (80 loc) · 3.12 KB
/
instalike.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
import time
import datetime
import random
class InstaLike:
def __init__(self, operation, repository, content_manager, configuration):
self.operation = operation
self.content_manager = content_manager
self.repository = repository
self.configuration = configuration
self.max_likes_per_hour = self.configuration.instalike_max_likes_per_hour
# Timing.
self.next_like_time = 0
self.like_time_delta = (60 * 60) // self.max_likes_per_hour
self.working_day = datetime.date.today().day
# Instance stats.
self.likes = 0
self.failed_likes = 0
self.hourly_likes = 0
self.t0 = time.time()
self.t1 = 0
def like(self, media):
response = self.operation.like(media.id)
if (not response):
self.failed_to_like()
return False
if (response.status_code != 200):
self.failed_to_like()
return False
self.repository.persist_like(media)
self.photo_liked()
return True
def act(self):
if (not self.can_act()):
return
if (time.time() < self.next_like_time):
return
media = self.content_manager.get_next_media()
if (media):
self.like(media)
else:
self.log('Could not get media.')
self.like_timeout()
# Has to be reworked.
def can_act(self):
self.t1 = time.time()
# hour elapsed
if ((self.t1 - self.t0) >= 60 * 60):
print('# of likes in last hour: {0}'.format(self.hourly_likes))
self.t0 = time.time()
self.hourly_likes = 0
return True
elif self.hourly_likes > self.max_likes_per_hour:
return False
return True
def like_timeout(self):
min_timeout = self.like_time_delta - (self.like_time_delta // 2)
max_timeout = self.like_time_delta + (self.like_time_delta // 2)
next_in = random.randint(min_timeout, max_timeout)
self.next_like_time = time.time() + next_in
self.get_stats()
def photo_liked(self):
self.likes += 1
self.hourly_likes += 1
def failed_to_like(self):
self.failed_likes += 1
self.hourly_likes += 1
def get_stats(self):
self.t1 = time.time()
per_hour = ((self.likes + self.failed_likes) * 60 * 60) // (
1 if (self.t1 - self.t0) == 0 else self.t1 - self.t0)
self.log('#######################################')
self.log('----------------LIKES------------------')
self.log('total time: {0:.0f}s'.format(self.t1 - self.t0))
self.log('likes: {0}'.format(self.likes))
self.log('failed likes: {0}'.format(self.failed_likes))
self.log('estimated likes per hour: {0:.0f}'.format(per_hour))
self.log('next like in: {0:.0f}s'.format(self.next_like_time - time.time()))
self.log('photos to like: {0}'.format(self.content_manager.get_media_count()))
def log(self, text):
print(text)