From 5c60c6381b7629c270f6c49d84f81b1d3e2d4d8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20PV=20Correia?= Date: Mon, 28 Feb 2022 15:59:17 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20New=20feature?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Schedule - Bot sleeps at set time for set amount of hours --- GithubAPIBot.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ bot_follow.py | 6 ++++ bot_unfollow.py | 6 ++++ 3 files changed, 87 insertions(+) diff --git a/GithubAPIBot.py b/GithubAPIBot.py index f1fa475..6856461 100644 --- a/GithubAPIBot.py +++ b/GithubAPIBot.py @@ -1,4 +1,5 @@ from base64 import b64encode +import datetime import random import requests from requests.adapters import HTTPAdapter @@ -19,6 +20,9 @@ def __init__( sleepSecondsActionMax: int, sleepSecondsLimitedMin: int, sleepSecondsLimitedMax: int, + sleepHour=None, + sleepMinute=None, + sleepTime=None, maxAction=None, ): if not isinstance(username, str): @@ -32,6 +36,9 @@ def __init__( self.__sleepSecondsActionMax = sleepSecondsActionMax self.__sleepSecondsLimitedMin = sleepSecondsLimitedMin self.__sleepSecondsLimitedMax = sleepSecondsLimitedMax + self.__sleepHour = sleepHour + self.__sleepMinute = sleepMinute + self.__sleepTime = sleepTime self.__maxAction = maxAction self.__usersToAction = [] self.__followings = [] @@ -109,6 +116,30 @@ def sleepSecondsLimitedMax(self): def sleepSecondsLimitedMax(self, value): self.__sleepSecondsLimitedMax = value + @property + def sleepHour(self): + return self.__sleepHour + + @sleepHour.setter + def sleepHour(self, value): + self.__sleepHour = value + + @property + def sleepMinute(self): + return self.__sleepMinute + + @sleepMinute.setter + def sleepMinute(self, value): + self.__sleepMinute = value + + @property + def sleepTime(self): + return self.__sleepTime + + @sleepTime.setter + def sleepTime(self, value): + self.__sleepTime = value + @property def maxAction(self): return self.__maxAction @@ -212,6 +243,10 @@ def run(self, action): if self.maxAction != None: self.usersToAction = self.usersToAction[: min(len(self.usersToAction), int(self.maxAction))] + # Time for the bot to go to sleep + if self.sleepHour != None and self.sleepMinute != None and self.sleepTime != None: + sleepTime = nextSleepTime(int(self.__sleepHour), int(self.sleepMinute)) + # Start follow/unfollow print(f"\nStarting to {action}.\n") users = tqdm( @@ -224,6 +259,14 @@ def run(self, action): leave=False, ) for user in users: + + # Set the bot to sleep at the set time + if self.sleepHour != None and self.sleepMinute != None and self.sleepTime != None: + timeNow = datetime.datetime.now() + if timeNow.timestamp() > sleepTime.timestamp(): + sleepTime = nextSleepTime(int(self.__sleepHour), int(self.__sleepMinute)) + timeNow += datetime.timedelta(hours=int(self.__sleepTime)) + sleepUntil(timeNow.hour, random.randint(0, 59)) # Follow/unfollow user try: @@ -261,3 +304,35 @@ def follow(self): def unfollow(self): self.run("unfollow") + +def nextSleepTime(hour, minute): + timeNow = datetime.datetime.now() + future = datetime.datetime(timeNow.year, timeNow.month, timeNow.day, hour, minute) + + if timeNow.timestamp() > future.timestamp(): + future += datetime.timedelta(days=1) + return future + +def sleepUntil(hour, minute): + t = datetime.datetime.today() + future = datetime.datetime(t.year, t.month, t.day, hour, minute) + + if t.timestamp() >= future.timestamp(): + future += datetime.timedelta(days=1) + + print(f'\nSleeping... Waking up at {future.hour}:{future.minute}') + + sleepSeconds = int((future-t).total_seconds()) + sleepSecondsObj = list(range(0, sleepSeconds)) + sleepSecondsBar = tqdm( + sleepSecondsObj, + dynamic_ncols=True, + smoothing=True, + bar_format="[SLEEPING] {n_fmt}s/{total_fmt}s |{l_bar}{bar}|", + position=2, + leave=False, + ) + for second in sleepSecondsBar: + time.sleep(1) + + print(f'\nWaking up...') \ No newline at end of file diff --git a/bot_follow.py b/bot_follow.py index 5de4711..66d6b72 100644 --- a/bot_follow.py +++ b/bot_follow.py @@ -20,6 +20,9 @@ parser.add_argument( "-slmax", "--sleep-max-limited", help="Max number of range to randomize sleep seconds when account limited" ) +parser.add_argument("-sh", "--sleep-hour", help="Hour for the bot to go to sleep") +parser.add_argument("-sm", "--sleep-minute", help="Minute for the bot to go to sleep") +parser.add_argument("-st", "--sleep-time", help="Total time (in hours) for the bot to sleep") args = parser.parse_args() sleepSecondsActionMin = int(args.sleep_min or 20) @@ -39,6 +42,9 @@ sleepSecondsActionMax, sleepSecondsLimitedMin, sleepSecondsLimitedMax, + args.sleep_hour, + args.sleep_minute, + args.sleep_time, args.max_follow, ) diff --git a/bot_unfollow.py b/bot_unfollow.py index f6aa91d..890c7cf 100644 --- a/bot_unfollow.py +++ b/bot_unfollow.py @@ -20,6 +20,9 @@ parser.add_argument( "-slmax", "--sleep-max-limited", help="Max Number of range to randomize sleep seconds when account limited" ) +parser.add_argument("-sh", "--sleep-hour", help="Hour for the bot to go to sleep") +parser.add_argument("-sm", "--sleep-minute", help="Minute for the bot to go to sleep") +parser.add_argument("-st", "--sleep-time", help="Total time (in hours) for the bot to sleep") args = parser.parse_args() sleepSecondsActionMin = int(args.sleep_min or 3) @@ -39,6 +42,9 @@ sleepSecondsActionMax, sleepSecondsLimitedMin, sleepSecondsLimitedMax, + args.sleep_hour, + args.sleep_minute, + args.sleep_time, args.max_unfollow, )