Skip to content

Commit

Permalink
✨ New feature
Browse files Browse the repository at this point in the history
Schedule - Bot sleeps at set time for set amount of hours
  • Loading branch information
João PV Correia committed Feb 28, 2022
1 parent fb61a2f commit 5c60c63
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
75 changes: 75 additions & 0 deletions GithubAPIBot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from base64 import b64encode
import datetime
import random
import requests
from requests.adapters import HTTPAdapter
Expand All @@ -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):
Expand All @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down Expand Up @@ -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...')
6 changes: 6 additions & 0 deletions bot_follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,6 +42,9 @@
sleepSecondsActionMax,
sleepSecondsLimitedMin,
sleepSecondsLimitedMax,
args.sleep_hour,
args.sleep_minute,
args.sleep_time,
args.max_follow,
)

Expand Down
6 changes: 6 additions & 0 deletions bot_unfollow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,6 +42,9 @@
sleepSecondsActionMax,
sleepSecondsLimitedMin,
sleepSecondsLimitedMax,
args.sleep_hour,
args.sleep_minute,
args.sleep_time,
args.max_unfollow,
)

Expand Down

0 comments on commit 5c60c63

Please sign in to comment.