8000 ✨ New feature · Correia-jpv/github-follow-bot@5c60c63 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c60c63

Browse files
author
João PV Correia
committed
✨ New feature
Schedule - Bot sleeps at set time for set amount of hours
1 parent fb61a2f commit 5c60c63

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

GithubAPIBot.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from base64 import b64encode
2+
import datetime
23
import random
34
import requests
45
from requests.adapters import HTTPAdapter
@@ -19,6 +20,9 @@ def __init__(
1920
sleepSecondsActionMax: int,
2021
sleepSecondsLimitedMin: int,
2122
sleepSecondsLimitedMax: int,
23+
sleepHour=None,
24+
sleepMinute=None,
25+
sleepTime=None,
2226
maxAction=None,
2327
):
2428
if not isinstance(username, str):
@@ -32,6 +36,9 @@ def __init__(
3236
self.__sleepSecondsActionMax = sleepSecondsActionMax
3337
self.__sleepSecondsLimitedMin = sleepSecondsLimitedMin
3438
self.__sleepSecondsLimitedMax = sleepSecondsLimitedMax
39+
self.__sleepHour = sleepHour
40+
self.__sleepMinute = sleepMinute
41+
self.__sleepTime = sleepTime
3542
self.__maxAction = maxAction
3643
self.__usersToAction = []
3744
self.__followings = []
@@ -109,6 +116,30 @@ def sleepSecondsLimitedMax(self):
109116
def sleepSecondsLimitedMax(self, value):
110117
self.__sleepSecondsLimitedMax = value
111118

119+
@property
120+
def sleepHour(self):
121+
return self.__sleepHour
122+
123+
@sleepHour.setter
124+
def sleepHour(self, value):
125+
self.__sleepHour = value
126+
127+
@property
128+
def sleepMinute(self):
129+
return self.__sleepMinute
130+
131+
@sleepMinute.setter
132+
def sleepMinute(self, value):
133+
self.__sleepMinute = value
134+
135+
@property
136+
def sleepTime(self):
137+
return self.__sleepTime
138+
139+
@sleepTime.setter
140+
def sleepTime(self, value):
141+
self.__sleepTime = value
142+
112143
@property
113144
def maxAction(self):
114145
return self.__maxAction
@@ -212,6 +243,10 @@ def run(self, action):
212243
if self.maxAction != None:
213244
self.usersToAction = self.usersToAction[: min(len(self.usersToAction), int(self.maxAction))]
214245

246+
# Time for the bot to go to sleep
247+
if self.sleepHour != None and self.sleepMinute != None and self.sleepTime != None:
248+
sleepTime = nextSleepTime(int(self.__sleepHour), int(self.sleepMinute))
249+
215250
# Start follow/unfollow
216251
print(f"\nStarting to {action}.\n")
217252
users = tqdm(
@@ -224,6 +259,14 @@ def run(self, action):
224259
leave=False,
225260
)
226261
for user in users:
262+
263+
# Set the bot to sleep at the set time
264+
if self.sleepHour != None and self.sleepMinute != None and self.sleepTime != None:
265+
timeNow = datetime.datetime.now()
266+
if timeNow.timestamp() > sleepTime.timestamp():
267+
sleepTime = nextSleepTime(int(self.__sleepHour), int(self.__sleepMinute))
268+
timeNow += datetime.timedelta(hours=int(self.__sleepTime))
269+
sleepUntil(timeNow.hour, random.randint(0, 59))
227270

228271
# Follow/unfollow user
229272
try:
@@ -261,3 +304,35 @@ def follow(self):
261304

262305
def unfollow(self):
263306
self.run("unfollow")
307+
308+
def nextSleepTime(hour, minute):
309+
timeNow = datetime.datetime.now()
310+
future = datetime.datetime(timeNow.year, timeNow.month, timeNow.day, hour, minute)
311+
312+
if timeNow.timestamp() > future.timestamp():
313+
future += datetime.timedelta(days=1)
314+
return future
315+
316+
def sleepUntil(hour, minute):
317+
t = datetime.datetime.today()
318+
future = datetime.datetime(t.year, t.month, t.day, hour, minute)
319+
320+
if t.timestamp() >= future.timestamp():
321+
future += datetime.timedelta(days=1)
322+
323+
print(f'\nSleeping... Waking up at {future.hour}:{future.minute}')
324+
325+
sleepSeconds = int((future-t).total_seconds())
326+
sleepSecondsObj = list(range(0, sleepSeconds))
327+
sleepSecondsBar = tqdm(
328+
sleepSecondsObj,
329+
dynamic_ncols=True,
330+
smoothing=True,
331+
bar_format="[SLEEPING] {n_fmt}s/{total_fmt}s |{l_bar}{bar}|",
332+
position=2,
333+
leave=False,
334+
)
335+
for second in sleepSecondsBar:
336+
time.sleep(1)
337+
338+
print(f'\nWaking up...')

bot_follow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
parser.add_argument(
2121
"-slmax", "--sleep-max-limited", help="Max number of range to randomize sleep seconds when account limited"
2222
)
23+
parser.add_argument("-sh", "--sleep-hour", help="Hour for the bot to go to sleep")
24+
parser.add_argument("-sm", "--sleep-minute", help="Minute for the bot to go to sleep")
25+
parser.add_argument("-st", "--sleep-time", help="Total time (in hours) for the bot to sleep")
2326
args = parser.parse_args()
2427

2528
sleepSecondsActionMin = int(args.sleep_min or 20)
@@ -39,6 +42,9 @@
3942
sleepSecondsActionMax,
4043
sleepSecondsLimitedMin,
4144
sleepSecondsLimitedMax,
45+
args.sleep_hour,
46+
args.sleep_minute,
47+
args.sleep_time,
4248
args.max_follow,
4349
)
4450

bot_unfollow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
parser.add_argument(
2121
"-slmax", "--sleep-max-limited", help="Max Number of range to randomize sleep seconds when account limited"
2222
)
23+
parser.add_argument("-sh", "--sleep-hour", help="Hour for the bot to go to sleep")
24+
parser.add_argument("-sm", "--sleep-minute", help="Minute for the bot to go to sleep")
25+
parser.add_argument("-st", "--sleep-time", help="Total time (in hours) for the bot to sleep")
2326
args = parser.parse_args()
2427

2528
sleepSecondsActionMin = int(args.sleep_min or 3)
@@ -39,6 +42,9 @@
3942
sleepSecondsActionMax,
4043
sleepSecondsLimitedMin,
4144
sleepSecondsLimitedMax,
45+
args.sleep_hour,
46+
args.sleep_minute,
47+
args.sleep_time,
4248
args.max_unfollow,
4349
)
4450

0 commit comments

Comments
 (0)
0